From 9ee2e813cee6421211e6e0ba0fcea5722637615d Mon Sep 17 00:00:00 2001 From: Chris Bongers Date: Fri, 8 May 2026 16:34:50 +0200 Subject: [PATCH 1/3] feat: add lucky button to feed header Adds a Sparkle-icon button in the main feed control header that fetches a random trending post via the existing randomTrendingPosts resolver and navigates to it. Reuses the same backend random-post logic that powers the "I'm feeling lucky" CTA in BestDiscussions. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../src/components/filters/LuckyButton.tsx | 60 +++++++++++++++++++ .../shared/src/components/layout/common.tsx | 2 + packages/shared/src/graphql/luckyPost.ts | 14 +++++ 3 files changed, 76 insertions(+) create mode 100644 packages/shared/src/components/filters/LuckyButton.tsx create mode 100644 packages/shared/src/graphql/luckyPost.ts diff --git a/packages/shared/src/components/filters/LuckyButton.tsx b/packages/shared/src/components/filters/LuckyButton.tsx new file mode 100644 index 0000000000..101121d0ba --- /dev/null +++ b/packages/shared/src/components/filters/LuckyButton.tsx @@ -0,0 +1,60 @@ +import type { ReactElement } from 'react'; +import React, { useState } from 'react'; +import { useRouter } from 'next/router'; +import { Button } from '../buttons/Button'; +import { ButtonSize, ButtonVariant } from '../buttons/common'; +import { SparkleIcon } from '../icons'; +import { IconSize } from '../Icon'; +import { gqlClient } from '../../graphql/common'; +import { LUCKY_POST_QUERY } from '../../graphql/luckyPost'; +import type { LuckyPostData } from '../../graphql/luckyPost'; +import { useLogContext } from '../../contexts/LogContext'; +import { LogEvent } from '../../lib/log'; +import { useViewSize, ViewSize } from '../../hooks'; +import { Tooltip } from '../tooltip/Tooltip'; + +export const LuckyButton = (): ReactElement => { + const router = useRouter(); + const { logEvent } = useLogContext(); + const isLaptop = useViewSize(ViewSize.Laptop); + const [isLoading, setIsLoading] = useState(false); + + const onClick = async () => { + if (isLoading) return; + setIsLoading(true); + try { + const data = await gqlClient.request(LUCKY_POST_QUERY); + const post = data.randomTrendingPosts?.[0]; + if (!post?.commentsPermalink) { + return; + } + logEvent({ + event_name: LogEvent.Click, + target_id: post.id, + extra: JSON.stringify({ origin: 'lucky button' }), + }); + router.push(post.commentsPermalink); + } finally { + setIsLoading(false); + } + }; + + return ( + +