forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

note-studio-panel.tsx138 lines, 6.1 KiBHistoryRawDownload
1import { Show, Suspense, createMemo, createSignal } from "solid-js";
2import { createResource } from "@/lib/create-resource";
3import { getCourseNotes } from "@/api/course-notes";
4import { getCourses } from "@/api/courses";
5import { getMyCourses } from "@/api/reports";
6import { formatApiError, type CourseNote } from "@/api/client";
7import { PodcastPanel } from "@/components/notes/podcast-panel";
8import { RagOutputsPanel } from "@/components/notes/rag-outputs-panel";
9import { StudioOutputLibrary } from "@/components/ai/studio-output-library";
10import { EmptyState } from "@/components/ui/empty-state";
11import { ErrorAlert } from "@/components/ui/error-alert";
12import { Label } from "@/components/ui/label";
13import { PageSpinner } from "@/components/ui/page-spinner";
14import { SearchableSelect } from "@/components/ui/searchable-select";
15import { useAuth } from "@/stores/auth-context";
16import { useT } from "@/stores/preferences-context";
17import { courseNoteFiles } from "@/lib/note-source";
18import { hasMinRole } from "@/lib/roles";
19
20type StudioNote = CourseNote & { courseTitle: string; courseCreatorId: string };
21
22/** Pick a lesson note, then read its AI outputs and produce its podcast —
23 * the same two panels the note reader shows, gathered on the AI hub. */
24export function NoteStudioPanel() {
25 const t = useT();
26 const auth = useAuth();
27 const [selectedId, setSelectedId] = createSignal("");
28 let outputsSection: HTMLElement | undefined;
29 const [notes, { refetch }] = createResource(
30 () => auth.user()?.role ?? null,
31 async (role) => {
32 const courses = role === "student" ? await getMyCourses({ limit: 100 }) : await getCourses({ limit: 100 });
33 const rows = await Promise.all(
34 courses.items.map(async (course) => {
35 try {
36 const page = await getCourseNotes(course.id, { limit: 100 });
37 return page.items.map<StudioNote>((note) => ({
38 ...note,
39 courseTitle: course.title,
40 courseCreatorId: course.creator.id,
41 }));
42 } catch {
43 return [];
44 }
45 }),
46 );
47 return rows.flat();
48 },
49 );
50 const options = createMemo(() => (notes() ?? []).map((note) => ({ value: note.id, label: `${note.courseTitle} · ${note.title}` })));
51 const selected = createMemo(() => (notes() ?? []).find((note) => note.id === selectedId()) ?? null);
52 const canManageSelected = () => {
53 const note = selected();
54 const user = auth.user();
55 return !!note && !!user && (hasMinRole(user.role, "manager") || note.courseCreatorId === user.id);
56 };
57 const selectLibraryNote = (noteId: string) => {
58 setSelectedId(noteId);
59 requestAnimationFrame(() => outputsSection?.scrollIntoView({ behavior: "smooth", block: "start" }));
60 };
61
62 return (
63 <Suspense fallback={<PageSpinner />}>
64 <Show when={notes.error}>
65 <ErrorAlert message={formatApiError(notes.error)} onRetry={() => void refetch()} />
66 </Show>
67 <Show when={!notes.error && notes()}>
68 <Show
69 when={(notes()?.length ?? 0) > 0}
70 fallback={<EmptyState kind="notes" title={t("podcast.noNotes")} description={t("podcast.noNotesHint")} />}
71 >
72 {/* Laid out the way a studio home reads: a plain title and one line
73 saying what it makes, the source picker, the producers, then
74 everything produced so far. Flat hairline cards, no banner and no
75 shadow stack — the work is the content, not the chrome. */}
76 <div class="w-full space-y-8">
77 <header class="space-y-1.5">
78 <h1 class="text-2xl font-semibold tracking-tight text-text-strong">{t("aiHub.tab.studio")}</h1>
79 <p class="text-sm text-muted-foreground">{t("aiStudio.heading")}</p>
80 </header>
81
82 <div class="max-w-xl space-y-2">
83 <Label for="sound-studio-note">{t("podcast.source")}</Label>
84 <SearchableSelect
85 id="sound-studio-note"
86 class="w-full"
87 value={selectedId()}
88 onChange={setSelectedId}
89 options={options()}
90 placeholder={t("podcast.selectNote")}
91 />
92 </div>
93
94 <Show
95 when={selected()}
96 fallback={
97 <div class="rounded-xl border border-dashed border-border-line px-6 py-12 text-center">
98 <p class="text-sm font-medium text-text-strong">{t("aiStudio.pickPrompt")}</p>
99 <p class="mx-auto mt-1 max-w-md text-sm text-muted-foreground">{t("aiStudio.pickPromptHint")}</p>
100 </div>
101 }
102 >
103 {(note) => (
104 <section ref={outputsSection} class="scroll-mt-20 space-y-4">
105 <div class="flex flex-wrap items-baseline justify-between gap-2">
106 <h2 class="min-w-0 truncate text-base font-semibold text-text-strong">{note().title}</h2>
107 <span class="shrink-0 text-xs text-muted-foreground">{note().courseTitle}</span>
108 </div>
109 <div class="grid gap-4 xl:grid-cols-2">
110 <RagOutputsPanel
111 noteId={note().id}
112 source={courseNoteFiles}
113 canManage={canManageSelected()}
114 active
115 noteTitle={note().title}
116 />
117 <PodcastPanel noteId={note().id} noteTitle={note().title} active />
118 </div>
119 </section>
120 )}
121 </Show>
122
123 {/* The library is the landing view. Once a note is open the page is
124 about producing for that note, so the list of everything else
125 steps out of the way. */}
126 <Show when={!selected()}>
127 <StudioOutputLibrary
128 notes={(notes() ?? []).map((note) => ({ id: note.id, title: note.title, courseTitle: note.courseTitle }))}
129 selectedId={selectedId()}
130 onSelect={selectLibraryNote}
131 />
132 </Show>
133 </div>
134 </Show>
135 </Show>
136 </Suspense>
137 );
138}