feat(insights): make spotlight post/story cards clickable
Top-post and top-story cards in the pet insights dashboard now open the real post (PostDetailDialog, same fetch-by-id pattern as the notifications page) or story (StoryViewer, new initialStoryId prop to jump straight to the right story instead of always starting at index 0).
This commit is contained in:
1 parent
45448573ea
commit
6e9e08be70
3 files changed
+90
-7
No files matched your search
@@ -27,5 +27,5 @@ export default async function InsightsPage({ params }: PageProps) {
|
|||||||
redirect(`/pets/${petId}`);
|
redirect(`/pets/${petId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return <InsightsDashboard petId={petId} petName={pet.name} />;
|
return <InsightsDashboard petId={petId} petName={pet.name} petAvatarKey={pet.avatarKey} />;
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,20 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
import { PawPrint, MessageCircle, ImageOff, Loader2 } from "lucide-react";
|
import { PawPrint, MessageCircle, ImageOff, Loader2 } from "lucide-react";
|
||||||
import { useQuery } from "@tanstack/react-query";
|
import { useQuery } from "@tanstack/react-query";
|
||||||
import { useTranslations } from "next-intl";
|
import { useTranslations } from "next-intl";
|
||||||
import { useTRPC } from "@/trpc/client";
|
import { useTRPC } from "@/trpc/client";
|
||||||
import { getMediaUrl } from "@/lib/media-url";
|
import { getMediaUrl } from "@/lib/media-url";
|
||||||
import { BlurImage } from "@/components/ui/blur-image";
|
import { BlurImage } from "@/components/ui/blur-image";
|
||||||
|
import { PostDetailDialog } from "@/components/feed/PostDetailDialog";
|
||||||
|
import type { PostCardPost } from "@/components/feed/PostCard";
|
||||||
|
import { StoryViewer } from "@/components/stories/StoryViewer";
|
||||||
|
|
||||||
interface InsightsDashboardProps {
|
interface InsightsDashboardProps {
|
||||||
petId: string;
|
petId: string;
|
||||||
petName: string;
|
petName: string;
|
||||||
|
petAvatarKey: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function StatTile({ label, value }: { label: string; value: number }) {
|
function StatTile({ label, value }: { label: string; value: number }) {
|
||||||
@@ -27,11 +32,22 @@ interface SpotlightCardProps {
|
|||||||
thumbnailBlurhash: string | null;
|
thumbnailBlurhash: string | null;
|
||||||
subtitle: string | null;
|
subtitle: string | null;
|
||||||
emptyLabel: string;
|
emptyLabel: string;
|
||||||
|
onClick?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
function SpotlightCard({ title, thumbnailUrl, thumbnailBlurhash, subtitle, emptyLabel }: SpotlightCardProps) {
|
/**
|
||||||
|
* Gitea #30 follow-up — spotlight items are clickable when they point at a
|
||||||
|
* real post/story (onClick set); the empty state (no item yet) renders the
|
||||||
|
* same card as a disabled, non-interactive button.
|
||||||
|
*/
|
||||||
|
function SpotlightCard({ title, thumbnailUrl, thumbnailBlurhash, subtitle, emptyLabel, onClick }: SpotlightCardProps) {
|
||||||
return (
|
return (
|
||||||
<div className="flex items-center gap-3 rounded-lg border bg-card p-3">
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onClick}
|
||||||
|
disabled={!onClick}
|
||||||
|
className="flex w-full items-center gap-3 rounded-lg border bg-card p-3 text-left transition-colors enabled:hover:bg-muted/60 enabled:cursor-pointer disabled:cursor-default"
|
||||||
|
>
|
||||||
<div className="relative h-14 w-14 shrink-0 overflow-hidden rounded-md bg-muted">
|
<div className="relative h-14 w-14 shrink-0 overflow-hidden rounded-md bg-muted">
|
||||||
{thumbnailUrl ? (
|
{thumbnailUrl ? (
|
||||||
<BlurImage
|
<BlurImage
|
||||||
@@ -54,7 +70,7 @@ function SpotlightCard({ title, thumbnailUrl, thumbnailBlurhash, subtitle, empty
|
|||||||
<p className="text-sm text-muted-foreground">{emptyLabel}</p>
|
<p className="text-sm text-muted-foreground">{emptyLabel}</p>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</button>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,12 +79,33 @@ function SpotlightCard({ title, thumbnailUrl, thumbnailBlurhash, subtitle, empty
|
|||||||
* and comments across a pet's posts + active stories, plus which post/story
|
* and comments across a pet's posts + active stories, plus which post/story
|
||||||
* is resonating most. Reads insights.getPetInsights (owner-only).
|
* is resonating most. Reads insights.getPetInsights (owner-only).
|
||||||
*/
|
*/
|
||||||
export function InsightsDashboard({ petId, petName }: InsightsDashboardProps) {
|
export function InsightsDashboard({ petId, petName, petAvatarKey }: InsightsDashboardProps) {
|
||||||
const t = useTranslations("Insights");
|
const t = useTranslations("Insights");
|
||||||
const trpc = useTRPC();
|
const trpc = useTRPC();
|
||||||
|
|
||||||
const { data, isLoading } = useQuery(trpc.insights.getPetInsights.queryOptions({ petId }));
|
const { data, isLoading } = useQuery(trpc.insights.getPetInsights.queryOptions({ petId }));
|
||||||
|
|
||||||
|
// Post spotlight click — same "fetch by id, open in dialog" pattern as the
|
||||||
|
// notifications page (PostDetailDialog needs the full post shape, not the
|
||||||
|
// trimmed insights payload).
|
||||||
|
const [openPostId, setOpenPostId] = useState<string | null>(null);
|
||||||
|
const [selectedPost, setSelectedPost] = useState<PostCardPost | null>(null);
|
||||||
|
const { data: fetchedPost } = useQuery({
|
||||||
|
...trpc.posts.getById.queryOptions({ postId: openPostId ?? "" }),
|
||||||
|
enabled: !!openPostId,
|
||||||
|
});
|
||||||
|
useEffect(() => {
|
||||||
|
if (fetchedPost) setSelectedPost(fetchedPost as PostCardPost);
|
||||||
|
}, [fetchedPost]);
|
||||||
|
function closePostDialog() {
|
||||||
|
setOpenPostId(null);
|
||||||
|
setSelectedPost(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Story spotlight click — StoryViewer takes an initialStoryId to jump
|
||||||
|
// straight to the right story instead of always starting at index 0.
|
||||||
|
const [openStoryId, setOpenStoryId] = useState<string | null>(null);
|
||||||
|
|
||||||
if (isLoading || !data) {
|
if (isLoading || !data) {
|
||||||
return (
|
return (
|
||||||
<div className="flex justify-center py-24">
|
<div className="flex justify-center py-24">
|
||||||
@@ -109,6 +146,11 @@ export function InsightsDashboard({ petId, petName }: InsightsDashboardProps) {
|
|||||||
: null
|
: null
|
||||||
}
|
}
|
||||||
emptyLabel={t("noPostsYet")}
|
emptyLabel={t("noPostsYet")}
|
||||||
|
onClick={
|
||||||
|
data.topPostByReactions
|
||||||
|
? () => setOpenPostId(data.topPostByReactions!.id)
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
<SpotlightCard
|
<SpotlightCard
|
||||||
title={t("mostCommentedPost")}
|
title={t("mostCommentedPost")}
|
||||||
@@ -120,6 +162,11 @@ export function InsightsDashboard({ petId, petName }: InsightsDashboardProps) {
|
|||||||
: null
|
: null
|
||||||
}
|
}
|
||||||
emptyLabel={t("noPostsYet")}
|
emptyLabel={t("noPostsYet")}
|
||||||
|
onClick={
|
||||||
|
data.topPostByComments
|
||||||
|
? () => setOpenPostId(data.topPostByComments!.id)
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
@@ -146,6 +193,11 @@ export function InsightsDashboard({ petId, petName }: InsightsDashboardProps) {
|
|||||||
: null
|
: null
|
||||||
}
|
}
|
||||||
emptyLabel={t("noActiveStoriesYet")}
|
emptyLabel={t("noActiveStoriesYet")}
|
||||||
|
onClick={
|
||||||
|
data.topStoryByReactions
|
||||||
|
? () => setOpenStoryId(data.topStoryByReactions!.id)
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
<SpotlightCard
|
<SpotlightCard
|
||||||
title={t("mostCommentedStory")}
|
title={t("mostCommentedStory")}
|
||||||
@@ -159,10 +211,33 @@ export function InsightsDashboard({ petId, petName }: InsightsDashboardProps) {
|
|||||||
: null
|
: null
|
||||||
}
|
}
|
||||||
emptyLabel={t("noActiveStoriesYet")}
|
emptyLabel={t("noActiveStoriesYet")}
|
||||||
|
onClick={
|
||||||
|
data.topStoryByComments
|
||||||
|
? () => setOpenStoryId(data.topStoryByComments!.id)
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<p className="mt-3 text-xs text-muted-foreground">{t("storiesExpireHint")}</p>
|
<p className="mt-3 text-xs text-muted-foreground">{t("storiesExpireHint")}</p>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
<PostDetailDialog
|
||||||
|
post={selectedPost}
|
||||||
|
open={!!selectedPost}
|
||||||
|
onClose={closePostDialog}
|
||||||
|
onDeleted={closePostDialog}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{openStoryId && (
|
||||||
|
<StoryViewer
|
||||||
|
petId={petId}
|
||||||
|
petName={petName}
|
||||||
|
petAvatarKey={petAvatarKey}
|
||||||
|
open={!!openStoryId}
|
||||||
|
onOpenChange={(o) => !o && setOpenStoryId(null)}
|
||||||
|
initialStoryId={openStoryId}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -35,6 +35,8 @@ interface StoryViewerProps {
|
|||||||
petAvatarKey: string | null;
|
petAvatarKey: string | null;
|
||||||
open: boolean;
|
open: boolean;
|
||||||
onOpenChange: (open: boolean) => void;
|
onOpenChange: (open: boolean) => void;
|
||||||
|
/** Gitea #30 — jump straight to this story (e.g. from the Insights spotlight) instead of always starting at index 0. Falls back to 0 if not found among the active stories. */
|
||||||
|
initialStoryId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const IMAGE_STORY_DURATION_MS = 5000;
|
const IMAGE_STORY_DURATION_MS = 5000;
|
||||||
@@ -56,6 +58,7 @@ export function StoryViewer({
|
|||||||
petAvatarKey,
|
petAvatarKey,
|
||||||
open,
|
open,
|
||||||
onOpenChange,
|
onOpenChange,
|
||||||
|
initialStoryId,
|
||||||
}: StoryViewerProps) {
|
}: StoryViewerProps) {
|
||||||
const t = useTranslations("StoryViewer");
|
const t = useTranslations("StoryViewer");
|
||||||
const trpc = useTRPC();
|
const trpc = useTRPC();
|
||||||
@@ -174,14 +177,19 @@ export function StoryViewer({
|
|||||||
advance();
|
advance();
|
||||||
}, [advance]);
|
}, [advance]);
|
||||||
|
|
||||||
// Reset index when viewer opens
|
// Reset index when viewer opens — jump straight to initialStoryId (if given
|
||||||
|
// and already among the loaded stories) instead of always starting at 0.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (open) {
|
if (open) {
|
||||||
setCurrentIndex(0);
|
const initialIndex = initialStoryId
|
||||||
|
? Math.max(0, stories.findIndex((s) => s.id === initialStoryId))
|
||||||
|
: 0;
|
||||||
|
setCurrentIndex(initialIndex);
|
||||||
setProgress(0);
|
setProgress(0);
|
||||||
setIsMuted(true);
|
setIsMuted(true);
|
||||||
setCommentsOpen(false);
|
setCommentsOpen(false);
|
||||||
}
|
}
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [open]);
|
}, [open]);
|
||||||
|
|
||||||
const handleClose = useCallback(() => {
|
const handleClose = useCallback(() => {
|
||||||
|
|||||||
Reference in new issue
Block a user