-
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
22
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
22 commits
Select commit
Hold shift + click to select a range
6b9b4a5
feat: embed modal sizing
ncarazon be2bf59
feat: set up embed screen
ncarazon 630f9c3
feat: add basic binary question card support
ncarazon c55e53a
feat: add dynamic chart height adaptation
ncarazon ab0904b
feat: update truncatable text
ncarazon 463553d
feat: add adaptation for continuous questions
ncarazon 489bee2
feat: mc questions
ncarazon eb24491
feat: add time series adaptation
ncarazon 1cd8b12
feat: add binary group charts adaptation
ncarazon b503194
feat: add continuous group support
ncarazon 318c895
feat: height considerations
ncarazon 7903121
feat: add theme via search params support
ncarazon f57c95d
feat: update chart valude styling
ncarazon dd9c28d
feat: add back zoom picker options
ncarazon 1c81d6b
refactor: preview page
ncarazon e090527
feat: addjust for og mode
ncarazon d7ea72f
feat: right y axis for embeds
ncarazon 70b40ba
feat: add common unit display
ncarazon 89c5ade
feat: remove forecast timeline title in embed
ncarazon 95cdd68
fix: theming issue
ncarazon f51c4c5
feat: pr review udpates
ncarazon 2b69144
feat: qa feedback
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.
86 changes: 86 additions & 0 deletions
86
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,86 @@ | ||
| 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; | ||
| }; | ||
|
|
||
| const EmbedQuestionCard: React.FC<Props> = ({ | ||
| post, | ||
| ogMode, | ||
| size, | ||
| theme, | ||
| titleOverride, | ||
| }) => { | ||
| const [headerHeight, setHeaderHeight] = useState(0); | ||
| const [legendHeight, setLegendHeight] = useState(0); | ||
| const [ogReady, setOgReady] = useState(!ogMode); | ||
|
|
||
| const chartHeight = useMemo( | ||
| () => | ||
| getEmbedChartHeight({ | ||
| post, | ||
| ogMode, | ||
| size, | ||
| headerHeight, | ||
| legendHeight, | ||
| }), | ||
| [post, ogMode, size, headerHeight, legendHeight] | ||
| ); | ||
|
|
||
| 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, chartHeight]); | ||
|
|
||
| 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 items-center justify-between"> | ||
| <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; |
125 changes: 125 additions & 0 deletions
125
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,125 @@ | ||
| 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%]", 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?