Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed question page 2 #90

Merged
merged 4 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"postcss-preset-env": "^7.3.2",
"prisma": "^3.11.1",
"query-string": "^7.1.1",
"re-resizable": "^6.9.9",
"react": "^17.0.2",
"react-component-export-image": "^1.0.6",
"react-compound-slider": "^3.3.1",
Expand Down
23 changes: 23 additions & 0 deletions src/web/common/BoxedLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { FaExternalLinkAlt } from "react-icons/fa";

type Props = {
url: string;
size?: "normal" | "small";
};

export const BoxedLink: React.FC<Props> = ({
url,
size = "normal",
children,
}) => (
<a
className={`px-2 py-1 border-2 border-gray-400 rounded-lg text-black no-underline text-normal hover:bg-gray-100 inline-flex flex-nowrap space-x-1 items-center ${
size === "small" ? "text-sm" : ""
}`}
href={url}
target="_blank"
>
<span>{children}</span>
<FaExternalLinkAlt className="text-gray-400 inline" />
</a>
);
85 changes: 57 additions & 28 deletions src/web/questions/components/CaptureQuestion.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import domtoimage from "dom-to-image"; // https://github.com/tsayen/dom-to-image
import { Resizable } from "re-resizable";
import { useEffect, useRef, useState } from "react";

import { Button } from "../../common/Button";
import { CopyParagraph } from "../../common/CopyParagraph";
import { QuestionFragment } from "../../fragments.generated";
Expand Down Expand Up @@ -79,6 +79,15 @@ const MetaculusSource: React.FC<{
);
};

const GrayContainer: React.FC<{ title: string }> = ({ title, children }) => (
<div className="bg-gray-100 p-2 space-y-1">
<div className="uppercase text-xs font-bold tracking-wide text-gray-600">
{title}:
</div>
<div>{children}</div>
</div>
);

interface Props {
question: QuestionFragment;
}
Expand Down Expand Up @@ -118,35 +127,55 @@ export const CaptureQuestion: React.FC<Props> = ({ question }) => {
await exportAsPictureAndCode();
};

return (
<div className="grid grid-cols-1 md:grid-cols-2 gap-4 place-items-center">
<div ref={containerRef}>
<QuestionCard
question={question}
showTimeStamp={true}
showExpandButton={false}
expandFooterToFullWidth={true}
/>
</div>
<div>
<Button onClick={onCaptureButtonClick}>{mainButtonText}</Button>
</div>
{imgSrc ? (
<>
<div>
if (imgSrc) {
return (
<div className="space-y-4">
<GrayContainer title="Generated image">
<a href={imgSrc} target="_blank">
<img src={imgSrc} />
</a>
</GrayContainer>
<div>
<ImageSource question={question} imgSrc={imgSrc} />
</div>
{question.platform.id === "metaculus" ? (
<>
<div className="justify-self-stretch">
<MetaculusEmbed question={question} />
</div>
<div>
<MetaculusSource question={question} />
</div>
</>
) : null}
</div>
);
}

return (
<div className="space-y-2 flex flex-col">
<GrayContainer title="Resizable preview">
<Resizable
minWidth={320}
bounds="window"
enable={{ right: true, left: true }}
>
<div ref={containerRef}>
<QuestionCard
container={(children) => (
<div className="px-4 py-3 bg-white">{children}</div>
)}
question={question}
showTimeStamp={true}
showExpandButton={false}
expandFooterToFullWidth={true}
/>
</div>
<div>
<ImageSource question={question} imgSrc={imgSrc} />
</div>
<div className="justify-self-stretch">
<MetaculusEmbed question={question} />
</div>
<div>
<MetaculusSource question={question} />
</div>
</>
) : null}
</Resizable>
</GrayContainer>
<Button onClick={onCaptureButtonClick} size="small">
{mainButtonText}
</Button>
</div>
);
};
Expand Down
14 changes: 2 additions & 12 deletions src/web/questions/components/PlatformLink.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import { FaExternalLinkAlt } from "react-icons/fa";

import { BoxedLink } from "../../common/BoxedLink";
import { QuestionFragment } from "../../fragments.generated";

export const PlatformLink: React.FC<{ question: QuestionFragment }> = ({
question,
}) => (
<a
className="px-2 py-1 border-2 border-gray-400 rounded-lg text-black no-underline text-normal hover:bg-gray-100 flex flex-nowrap space-x-1 items-center"
href={question.url}
target="_blank"
>
<span>{question.platform.label}</span>
<FaExternalLinkAlt className="text-gray-400 inline sm:text-md text-md" />
</a>
);
}) => <BoxedLink url={question.url}>{question.platform.label}</BoxedLink>;
124 changes: 62 additions & 62 deletions src/web/questions/components/QuestionCard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Link from "next/link";
import { ReactElement, ReactNode } from "react";
import { FaExpand } from "react-icons/fa";
import ReactMarkdown from "react-markdown";

import { Card } from "../../../common/Card";
import { CopyText } from "../../../common/CopyText";
import { QuestionFragment } from "../../../fragments.generated";
Expand Down Expand Up @@ -63,6 +63,7 @@ const LastUpdated: React.FC<{ timestamp: Date }> = ({ timestamp }) => (
// Main component

interface Props {
container?: (children: ReactNode) => ReactElement;
question: QuestionFragment;
showTimeStamp: boolean;
expandFooterToFullWidth: boolean;
Expand All @@ -71,6 +72,7 @@ interface Props {
}

export const QuestionCard: React.FC<Props> = ({
container = (children) => <Card>{children}</Card>,
question,
showTimeStamp,
expandFooterToFullWidth,
Expand All @@ -82,72 +84,70 @@ export const QuestionCard: React.FC<Props> = ({

const isBinary = isQuestionBinary(question);

return (
<Card>
<div className="h-full flex flex-col space-y-4">
<div className="flex-grow space-y-4">
{showIdToggle ? (
<div className="mx-10">
<CopyText text={question.id} displayText={`[${question.id}]`} />
</div>
) : null}
<div>
{showExpandButton ? (
<Link href={`/questions/${question.id}`} passHref>
<a className="float-right block ml-2 mt-1.5">
<FaExpand
size="18"
className="text-gray-400 hover:text-gray-700"
/>
</a>
</Link>
) : null}
<Card.Title>
<a
className="text-black no-underline"
href={question.url}
target="_blank"
>
{question.title}
</a>
</Card.Title>
</div>
<div className={isBinary ? "flex justify-between" : "space-y-4"}>
<QuestionOptions question={question} maxNumOptions={5} />
<div className={`hidden ${showTimeStamp ? "sm:block" : ""}`}>
<LastUpdated timestamp={lastUpdated} />
</div>
return container(
<div className="h-full flex flex-col space-y-4">
<div className="flex-grow space-y-4">
{showIdToggle ? (
<div className="mx-10">
<CopyText text={question.id} displayText={`[${question.id}]`} />
</div>

{question.platform.id !== "guesstimate" && options.length < 3 && (
<div className="text-gray-500">
<DisplayMarkdown description={question.description} />
</div>
)}

{question.platform.id === "guesstimate" && question.visualization && (
<img
className="rounded-sm"
src={question.visualization}
alt="Guesstimate Screenshot"
/>
)}
) : null}
<div>
{showExpandButton ? (
<Link href={`/questions/${question.id}`} passHref>
<a className="float-right block ml-2 mt-1.5">
<FaExpand
size="18"
className="text-gray-400 hover:text-gray-700"
/>
</a>
</Link>
) : null}
<Card.Title>
<a
className="text-black no-underline"
href={question.url}
target="_blank"
>
{question.title}
</a>
</Card.Title>
</div>
<div
className={`sm:hidden ${!showTimeStamp ? "hidden" : ""} self-center`}
>
{/* This one is exclusively for mobile*/}
<LastUpdated timestamp={lastUpdated} />
<div className={isBinary ? "flex justify-between" : "space-y-4"}>
<QuestionOptions question={question} maxNumOptions={5} />
<div className={`hidden ${showTimeStamp ? "sm:block" : ""}`}>
<LastUpdated timestamp={lastUpdated} />
</div>
</div>
<div className="w-full">
<div className="mb-2 mt-1">
<QuestionFooter
question={question}
expandFooterToFullWidth={expandFooterToFullWidth}
/>

{question.platform.id !== "guesstimate" && options.length < 3 && (
<div className="text-gray-500">
<DisplayMarkdown description={question.description} />
</div>
)}

{question.platform.id === "guesstimate" && question.visualization && (
<img
className="rounded-sm"
src={question.visualization}
alt="Guesstimate Screenshot"
/>
)}
</div>
<div
className={`sm:hidden ${!showTimeStamp ? "hidden" : ""} self-center`}
>
{/* This one is exclusively for mobile*/}
<LastUpdated timestamp={lastUpdated} />
</div>
<div className="w-full">
<div className="mb-2 mt-1">
<QuestionFooter
question={question}
expandFooterToFullWidth={expandFooterToFullWidth}
/>
</div>
</div>
</Card>
</div>
);
};
Loading