forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

studio-output-library.tsx145 lines, 6.3 KiBHistoryRawDownload
1import { For, Show, Suspense, createMemo } from "solid-js";
2import { createResource } from "@/lib/create-resource";
3import { getCourseNoteRag } from "@/api/course-notes";
4import { listPodcastJobs } from "@/api/podcast";
5import { Badge } from "@/components/ui/badge";
6import { EmptyInline } from "@/components/ui/empty-inline";
7import { IconNote, IconSparkles, IconWaveform } from "@/components/ui/icons";
8import { PageSpinner } from "@/components/ui/page-spinner";
9import { cn } from "@/lib/cn";
10import { formatDate } from "@/lib/format";
11import { usePreferences, useT } from "@/stores/preferences-context";
12
13export type StudioLibraryNote = {
14 id: string;
15 title: string;
16 courseTitle: string;
17};
18
19type StudioLibraryItem = StudioLibraryNote & {
20 podcastCount: number;
21 hasSummary: boolean;
22 latestAt: number;
23};
24
25const LIBRARY_LIMIT = 100;
26
27export function StudioOutputLibrary(props: {
28 notes: StudioLibraryNote[];
29 selectedId: string;
30 onSelect: (noteId: string) => void;
31}) {
32 const t = useT();
33 const { locale } = usePreferences();
34 const source = createMemo(() => props.notes.map((note) => note.id).join(","));
35 const [items] = createResource(source, async (): Promise<StudioLibraryItem[]> => {
36 const [podcastResult, ...summaryResults] = await Promise.allSettled([
37 listPodcastJobs({ limit: LIBRARY_LIMIT }),
38 ...props.notes.map((note) => getCourseNoteRag(note.id, { limit: 1 })),
39 ]);
40 const podcasts = podcastResult.status === "fulfilled" ? podcastResult.value.items : [];
41 const podcastsByNote = new Map<string, { count: number; latestAt: number }>();
42
43 for (const podcast of podcasts) {
44 if (podcast.state !== "done") continue;
45 const current = podcastsByNote.get(podcast.source_id) ?? { count: 0, latestAt: 0 };
46 podcastsByNote.set(podcast.source_id, {
47 count: current.count + 1,
48 latestAt: Math.max(current.latestAt, podcast.finished_at ?? podcast.created_at),
49 });
50 }
51
52 return props.notes
53 .map((note, index): StudioLibraryItem | null => {
54 const summaryResult = summaryResults[index];
55 const summary = summaryResult?.status === "fulfilled" ? summaryResult.value.items[0] : undefined;
56 const podcast = podcastsByNote.get(note.id);
57 if (!summary && !podcast) return null;
58 return {
59 ...note,
60 podcastCount: podcast?.count ?? 0,
61 hasSummary: !!summary,
62 latestAt: Math.max(summary?.generated_at ?? 0, podcast?.latestAt ?? 0),
63 };
64 })
65 .filter((item): item is StudioLibraryItem => item != null)
66 .sort((a, b) => b.latestAt - a.latestAt);
67 });
68
69 return (
70 <section class="overflow-hidden rounded-xl border border-border-line bg-surface-base">
71 <header class="flex flex-wrap items-baseline justify-between gap-3 border-b border-border-hairline px-4 py-3">
72 <div class="min-w-0">
73 <h2 class="text-sm font-semibold text-text-strong">{t("aiStudio.library.title")}</h2>
74 <p class="mt-0.5 text-xs text-muted-foreground">{t("aiStudio.library.description")}</p>
75 </div>
76 <Show when={(items()?.length ?? 0) > 0}>
77 <span class="shrink-0 text-xs tabular-nums text-muted-foreground">
78 {t("aiStudio.library.count", { count: items()?.length ?? 0 })}
79 </span>
80 </Show>
81 </header>
82
83 <Suspense fallback={<div class="py-8"><PageSpinner /></div>}>
84 <Show
85 when={(items()?.length ?? 0) > 0}
86 fallback={
87 <div class="px-4 py-5 sm:px-5">
88 <EmptyInline title={t("aiStudio.library.empty")} hint={t("aiStudio.library.emptyHint")} />
89 </div>
90 }
91 >
92 {/* Studio's own working screens are flat, dense lists rather than
93 card grids: hairline dividers, one row per thing, and the active
94 row marked by an accent edge instead of a box of its own. */}
95 <ul class="divide-y divide-border-hairline">
96 <For each={items()}>
97 {(item) => (
98 <li>
99 <button
100 type="button"
101 aria-current={props.selectedId === item.id ? "true" : undefined}
102 class={cn(
103 "flex w-full items-center gap-3 border-l-2 px-4 py-2.5 text-left outline-hidden transition-colors hover:bg-surface-tint focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset",
104 props.selectedId === item.id
105 ? "border-l-primary bg-primary/[0.04]"
106 : "border-l-transparent",
107 )}
108 onClick={() => props.onSelect(item.id)}
109 >
110 <span class="shrink-0 text-muted-foreground">
111 <Show when={item.podcastCount > 0} fallback={<IconNote class="h-4 w-4" />}>
112 <IconWaveform class="h-4 w-4" />
113 </Show>
114 </span>
115 <span class="min-w-0 flex-1">
116 <span class="block truncate text-sm font-medium text-text-strong">{item.title}</span>
117 <span class="mt-0.5 block truncate text-xs text-muted-foreground">{item.courseTitle}</span>
118 </span>
119 <span class="hidden shrink-0 items-center gap-1.5 sm:flex">
120 <Show when={item.hasSummary}>
121 <Badge variant="outline" class="gap-1 rounded-md font-normal">
122 <IconSparkles class="h-3 w-3" />
123 {t("aiStudio.library.summary")}
124 </Badge>
125 </Show>
126 <Show when={item.podcastCount > 0}>
127 <Badge variant="outline" class="gap-1 rounded-md font-normal">
128 <IconWaveform class="h-3 w-3" />
129 {t("aiStudio.library.podcastCount", { count: item.podcastCount })}
130 </Badge>
131 </Show>
132 </span>
133 <span class="shrink-0 text-xs tabular-nums text-muted-foreground">
134 {formatDate(item.latestAt, locale())}
135 </span>
136 </button>
137 </li>
138 )}
139 </For>
140 </ul>
141 </Show>
142 </Suspense>
143 </section>
144 );
145}