-
Notifications
You must be signed in to change notification settings - Fork 24
Embeds Redesign #3868
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
Open
ncarazon
wants to merge
23
commits into
main
Choose a base branch
from
feat/embeds-redesign
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Embeds Redesign #3868
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
5be5c4a
feat: embed modal sizing
ncarazon 18ecdba
feat: set up embed screen
ncarazon a47ad4d
feat: add basic binary question card support
ncarazon df0203e
feat: add dynamic chart height adaptation
ncarazon ba02dbe
feat: update truncatable text
ncarazon 3a73dfd
feat: add adaptation for continuous questions
ncarazon bd040ec
feat: mc questions
ncarazon dee03df
feat: add time series adaptation
ncarazon 03921e7
feat: add binary group charts adaptation
ncarazon b10848d
feat: add continuous group support
ncarazon c919097
feat: height considerations
ncarazon f46e668
feat: add theme via search params support
ncarazon 4000c90
feat: update chart valude styling
ncarazon 7cfc9ca
feat: add back zoom picker options
ncarazon e1b34aa
refactor: preview page
ncarazon 8d46e09
feat: addjust for og mode
ncarazon d073115
feat: right y axis for embeds
ncarazon f7622db
feat: add common unit display
ncarazon 096a31d
feat: remove forecast timeline title in embed
ncarazon 759f616
fix: theming issue
ncarazon 00661b0
feat: pr review udpates
ncarazon 0361247
feat: qa feedback
ncarazon 6b310f2
feat: qa updates
ncarazon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
97 changes: 97 additions & 0 deletions
97
front_end/src/app/(embed)/questions/components/embed_question_card.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { useEffect, useMemo, useState } from "react"; | ||
|
|
||
| import { PostWithForecasts } from "@/types/post"; | ||
|
|
||
| import EmbedQuestionFooter from "./embed_question_footer"; | ||
| import EmbedQuestionHeader from "./embed_question_header"; | ||
| import EmbedQuestionPlot from "./embed_question_plot"; | ||
| import { QuestionViewModeProvider } from "./question_view_mode_context"; | ||
| import { EmbedTheme } from "../constants/embed_theme"; | ||
| import { EmbedSize, getEmbedChartHeight } from "../helpers/embed_chart_height"; | ||
|
|
||
| type Props = { | ||
| post: PostWithForecasts; | ||
| ogMode?: boolean; | ||
| size: EmbedSize; | ||
| theme?: EmbedTheme; | ||
| titleOverride?: string; | ||
| isDynamicMcHeight?: boolean; | ||
| }; | ||
|
|
||
| const EmbedQuestionCard: React.FC<Props> = ({ | ||
| post, | ||
| ogMode, | ||
| size, | ||
| theme, | ||
| titleOverride, | ||
| isDynamicMcHeight = false, | ||
| }) => { | ||
| const [headerHeight, setHeaderHeight] = useState(0); | ||
| const [legendHeight, setLegendHeight] = useState(0); | ||
| const [ogReady, setOgReady] = useState(!ogMode); | ||
|
|
||
| const chartHeight = useMemo(() => { | ||
| if (ogMode) { | ||
| return getEmbedChartHeight({ | ||
| post, | ||
| ogMode, | ||
| size, | ||
| headerHeight, | ||
| legendHeight, | ||
| }); | ||
| } | ||
|
|
||
| if (isDynamicMcHeight) return undefined; | ||
| return getEmbedChartHeight({ | ||
| post, | ||
| ogMode, | ||
| size, | ||
| headerHeight, | ||
| legendHeight, | ||
| }); | ||
| }, [post, ogMode, size, headerHeight, legendHeight, isDynamicMcHeight]); | ||
|
|
||
| useEffect(() => { | ||
| if (!ogMode) return; | ||
|
|
||
| if (!headerHeight) { | ||
| setOgReady(false); | ||
| return; | ||
| } | ||
|
|
||
| let raf1 = 0; | ||
| let raf2 = 0; | ||
|
|
||
| raf1 = requestAnimationFrame(() => { | ||
| raf2 = requestAnimationFrame(() => { | ||
| setOgReady(true); | ||
| }); | ||
| }); | ||
|
|
||
| return () => { | ||
| cancelAnimationFrame(raf1); | ||
| cancelAnimationFrame(raf2); | ||
| }; | ||
| }, [ogMode, headerHeight, legendHeight]); | ||
|
|
||
| return ( | ||
| <QuestionViewModeProvider mode="embed"> | ||
| <EmbedQuestionHeader | ||
| post={post} | ||
| onHeightChange={setHeaderHeight} | ||
| titleStyle={theme?.title} | ||
| titleOverride={titleOverride} | ||
| theme={theme} | ||
| /> | ||
| <EmbedQuestionPlot | ||
| post={post} | ||
| chartHeight={chartHeight} | ||
| onLegendHeightChange={setLegendHeight} | ||
| theme={theme} | ||
| /> | ||
| <EmbedQuestionFooter ogReady={ogReady} post={post} /> | ||
| </QuestionViewModeProvider> | ||
| ); | ||
| }; | ||
|
|
||
| export default EmbedQuestionCard; |
54 changes: 54 additions & 0 deletions
54
front_end/src/app/(embed)/questions/components/embed_question_footer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import Image from "next/image"; | ||
| import React from "react"; | ||
|
|
||
| import ForecastersCounter from "@/app/(main)/questions/components/forecaster_counter"; | ||
| import CommentStatus from "@/components/post_card/basic_post_card/comment_status"; | ||
| import { PostWithForecasts } from "@/types/post"; | ||
|
|
||
| import metaculusDarkLogo from "../assets/metaculus-dark.png"; | ||
| import metaculusLightLogo from "../assets/metaculus-light.png"; | ||
|
|
||
| type Props = { | ||
| post: PostWithForecasts; | ||
| ogReady?: boolean; | ||
| }; | ||
|
|
||
| const EmbedQuestionFooter: React.FC<Props> = ({ post, ogReady }) => { | ||
| return ( | ||
| <div className="flex flex-wrap items-center justify-between gap-y-3"> | ||
| <div className="flex items-center gap-2"> | ||
| <ForecastersCounter | ||
| className="py-1 pl-0 pr-1.5 [&_strong]:font-normal" | ||
| forecasters={post.nr_forecasters} | ||
| /> | ||
| <CommentStatus | ||
| className="!px-1.5 py-1 [&_strong]:font-normal [&_svg]:text-gray-400 [&_svg]:dark:text-gray-400-dark" | ||
| totalCount={post.comment_count ?? 0} | ||
| unreadCount={post.unread_comment_count ?? 0} | ||
| url={""} | ||
| /> | ||
| </div> | ||
|
|
||
| {ogReady && ( | ||
| <div id="id-logo-used-by-screenshot-donot-change"> | ||
| <Image | ||
| className="dark:hidden" | ||
| src={metaculusDarkLogo} | ||
| alt="Metaculus Logo" | ||
| width={74} | ||
| height={15} | ||
| /> | ||
| <Image | ||
| className="hidden dark:block" | ||
| src={metaculusLightLogo} | ||
| alt="Metaculus Logo" | ||
| width={74} | ||
| height={15} | ||
| /> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default EmbedQuestionFooter; |
128 changes: 128 additions & 0 deletions
128
front_end/src/app/(embed)/questions/components/embed_question_header.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { CSSProperties, useEffect, useMemo, useRef } from "react"; | ||
|
|
||
| import QuestionHeaderCPStatus from "@/app/(main)/questions/[id]/components/question_view/forecaster_question_view/question_header/question_header_cp_status"; | ||
| import { ContinuousQuestionTypes } from "@/constants/questions"; | ||
| import { PostWithForecasts } from "@/types/post"; | ||
| import { QuestionType, QuestionWithForecasts } from "@/types/question"; | ||
| import cn from "@/utils/core/cn"; | ||
| import { | ||
| isContinuousQuestion, | ||
| isGroupOfQuestionsPost, | ||
| isQuestionPost, | ||
| } from "@/utils/questions/helpers"; | ||
|
|
||
| import { useIsEmbedMode } from "./question_view_mode_context"; | ||
| import TruncatableQuestionTitle from "./truncatable_question_title"; | ||
| import { EmbedTheme } from "../constants/embed_theme"; | ||
| import { getEmbedAccentColor } from "../helpers/embed_theme"; | ||
|
|
||
| type Props = { | ||
| post: PostWithForecasts; | ||
| onHeightChange?: (height: number) => void; | ||
| titleStyle?: CSSProperties; | ||
| titleOverride?: string; | ||
| theme?: EmbedTheme; | ||
| }; | ||
|
|
||
| const EmbedQuestionHeader: React.FC<Props> = ({ | ||
| post, | ||
| onHeightChange, | ||
| titleStyle, | ||
| titleOverride, | ||
| theme, | ||
| }) => { | ||
| const containerRef = useRef<HTMLDivElement | null>(null); | ||
| const isEmbed = useIsEmbedMode(); | ||
|
|
||
| useEffect(() => { | ||
| if (!onHeightChange) return; | ||
| const el = containerRef.current; | ||
| if (!el) return; | ||
|
|
||
| const update = () => { | ||
| onHeightChange(el.getBoundingClientRect().height); | ||
| }; | ||
|
|
||
| update(); | ||
|
|
||
| const observer = new ResizeObserver(() => { | ||
| update(); | ||
| }); | ||
| observer.observe(el); | ||
|
|
||
| return () => observer.disconnect(); | ||
| }, [onHeightChange]); | ||
|
|
||
| const maxLines = useMemo(() => { | ||
| if (isGroupOfQuestionsPost(post)) { | ||
| const firstType = post.group_of_questions.questions[0]?.type; | ||
| const isBinaryGroup = firstType === QuestionType.Binary; | ||
| const isContinuousGroup = ContinuousQuestionTypes.some( | ||
| (t) => t === firstType | ||
| ); | ||
|
|
||
| if (isBinaryGroup || isContinuousGroup) return 2; | ||
| return 3; | ||
| } | ||
|
|
||
| if (!isQuestionPost(post)) return 3; | ||
| const q = post.question; | ||
|
|
||
| if (q.type === QuestionType.MultipleChoice) return 2; | ||
| return q.type === QuestionType.Binary || isContinuousQuestion(q) ? 4 : 3; | ||
| }, [post]); | ||
|
|
||
| const titleMinHeightClass = useMemo(() => { | ||
| if (isGroupOfQuestionsPost(post)) { | ||
| const firstType = post.group_of_questions.questions[0]?.type; | ||
| const isBinaryGroup = firstType === QuestionType.Binary; | ||
| const isContinuousGroup = ContinuousQuestionTypes.some( | ||
| (t) => t === firstType | ||
| ); | ||
|
|
||
| return isBinaryGroup || isContinuousGroup ? "min-h-[2.5em]" : ""; | ||
| } | ||
|
|
||
| if (!isQuestionPost(post)) return ""; | ||
| const q = post.question; | ||
|
|
||
| const needsMinHeight = | ||
| q.type === QuestionType.MultipleChoice || | ||
| q.type === QuestionType.Binary || | ||
| isContinuousQuestion(q); | ||
|
|
||
| return needsMinHeight ? "min-h-[2.5em]" : ""; | ||
| }, [post]); | ||
|
|
||
| const predictionColor = getEmbedAccentColor(theme); | ||
|
|
||
| return ( | ||
| <div | ||
| ref={containerRef} | ||
| className={cn("flex items-center gap-3", isEmbed && "items-start")} | ||
| > | ||
| <TruncatableQuestionTitle | ||
| className={cn( | ||
| "!text-[20px] !leading-[125%] [@container(max-width:375px)]:!text-[16px]", | ||
| titleMinHeightClass | ||
| )} | ||
| maxLines={maxLines} | ||
| revealOnHoverOrTap={true} | ||
| style={titleStyle} | ||
| > | ||
| {titleOverride ?? post.title} | ||
| </TruncatableQuestionTitle> | ||
| {isQuestionPost(post) && ( | ||
| <QuestionHeaderCPStatus | ||
| question={post.question as QuestionWithForecasts} | ||
| size="md" | ||
| hideLabel={isContinuousQuestion(post.question)} | ||
| colorOverride={predictionColor} | ||
| chartTheme={theme?.chart} | ||
| /> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default EmbedQuestionHeader; |
50 changes: 50 additions & 0 deletions
50
front_end/src/app/(embed)/questions/components/embed_question_plot.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import DetailedGroupCard from "@/components/detailed_question_card/detailed_group_card"; | ||
| import DetailedQuestionCard from "@/components/detailed_question_card/detailed_question_card"; | ||
| import { PostWithForecasts } from "@/types/post"; | ||
| import { | ||
| isGroupOfQuestionsPost, | ||
| isQuestionPost, | ||
| } from "@/utils/questions/helpers"; | ||
|
|
||
| import { EmbedTheme } from "../constants/embed_theme"; | ||
| import { getEmbedAccentColor } from "../helpers/embed_theme"; | ||
|
|
||
| type Props = { | ||
| post: PostWithForecasts; | ||
| chartHeight?: number; | ||
| onLegendHeightChange?: (height: number) => void; | ||
| theme?: EmbedTheme; | ||
| }; | ||
|
|
||
| const EmbedQuestionPlot: React.FC<Props> = ({ | ||
| post, | ||
| chartHeight, | ||
| onLegendHeightChange, | ||
| theme, | ||
| }) => { | ||
| const isGroup = isGroupOfQuestionsPost(post); | ||
| const accent = getEmbedAccentColor(theme); | ||
| return ( | ||
| <> | ||
| {isQuestionPost(post) && ( | ||
| <DetailedQuestionCard | ||
| post={post} | ||
| embedChartHeight={chartHeight} | ||
| onLegendHeightChange={onLegendHeightChange} | ||
| chartTheme={theme?.chart} | ||
| colorOverride={accent} | ||
| /> | ||
| )} | ||
| {isGroup && ( | ||
| <DetailedGroupCard | ||
| post={post} | ||
| embedChartHeight={chartHeight} | ||
| onLegendHeightChange={onLegendHeightChange} | ||
| chartTheme={theme?.chart} | ||
| /> | ||
| )} | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default EmbedQuestionPlot; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for not having this as an SVG?