import { Show, Suspense, createEffect, createMemo, createSignal } from "solid-js"; import { Link, useNavigate } from "@tanstack/solid-router"; import { createResource } from "@/lib/create-resource"; import { getCourseNotes } from "@/api/course-notes"; import { getCourses } from "@/api/courses"; import { formatApiError, type CourseNote } from "@/api/client"; import { PodcastPanel } from "@/components/notes/podcast-panel"; import { RagOutputsPanel } from "@/components/notes/rag-outputs-panel"; import { StudioOutputLibrary } from "@/components/ai/studio-output-library"; import { RecordNotFound } from "@/components/layout/record-not-found"; import { buttonVariants } from "@/components/ui/button"; import { EmptyState } from "@/components/ui/empty-state"; import { ErrorAlert } from "@/components/ui/error-alert"; import { IconChevronLeft } from "@/components/ui/icons"; import { Label } from "@/components/ui/label"; import { PageSpinner } from "@/components/ui/page-spinner"; import { SearchableSelect } from "@/components/ui/searchable-select"; import { useAuth } from "@/stores/auth-context"; import { useT } from "@/stores/preferences-context"; import { cn } from "@/lib/cn"; import { courseNoteFiles } from "@/lib/note-source"; import { hasMinRole } from "@/lib/roles"; const RAIL_KEY = "hezarfen.studio.railWidth"; const RAIL_MIN = 280; const RAIL_MAX = 720; const RAIL_DEFAULT = 352; function readRailWidth(): number { if (typeof localStorage === "undefined") return RAIL_DEFAULT; const value = Number(localStorage.getItem(RAIL_KEY)); return Number.isFinite(value) && value >= RAIL_MIN && value <= RAIL_MAX ? value : RAIL_DEFAULT; } type StudioNote = CourseNote & { courseTitle: string; courseCreatorId: string }; const studioNotesCache = new Map>(); /** Every lesson note the account can produce from, with its course. */ export async function loadStudioNotes(): Promise { // GET /courses is already scoped to what the caller teaches or takes. const courses = await getCourses({ limit: 100 }); const rows: StudioNote[][] = Array.from({ length: courses.items.length }, () => []); let next = 0; await Promise.all(Array.from({ length: Math.min(4, courses.items.length) }, async () => { while (next < courses.items.length) { const index = next++; const course = courses.items[index]!; try { const page = await getCourseNotes(course.id, { limit: 100 }); rows[index] = page.items.map((note) => ({ ...note, courseTitle: course.title, courseCreatorId: course.creator.id, })); } catch { rows[index] = []; } } })); return rows.flat(); } export function getCachedStudioNotes(userId: string): Promise { let pending = studioNotesCache.get(userId); if (!pending) { pending = loadStudioNotes().catch((error) => { studioNotesCache.delete(userId); throw error; }); studioNotesCache.set(userId, pending); } return pending; } function useStudioNotes() { const auth = useAuth(); return createResource(() => auth.user()?.id ?? null, (userId) => userId ? getCachedStudioNotes(userId) : []); } /** * The studio's landing page: what it makes, a picker to start on any note, * and the library of everything produced so far. Picking a note or a row * opens that note's own page (`/ai/studio/$noteId`); nothing opens in place. */ export function NoteStudioPanel() { const t = useT(); const navigate = useNavigate(); const [notes, { refetch }] = useStudioNotes(); const options = createMemo(() => (notes() ?? []).map((note) => ({ value: note.id, label: `${note.courseTitle} · ${note.title}` }))); const open = (noteId: string, episode?: string) => { if (!noteId) return; void navigate({ to: "/ai/studio/$noteId", params: { noteId }, search: { episode } }); }; return (

{t("aiHub.tab.studio")}

{t("aiStudio.heading")}

}> void refetch()} /> 0} fallback={} > {/* A studio home: a plain title and one line saying what it makes, the way to start on a note, then everything produced so far. Flat hairline cards, no banner and no shadow stack. */}
open(noteId)} options={options()} placeholder={t("podcast.selectNote")} />
({ id: note.id, title: note.title, courseTitle: note.courseTitle }))} selectedId="" onSelect={open} />
); } /** * One note's studio page: its summary and its episodes, nothing else — no * picker, no library. "Kitaplığa dön" is the way back. `episode` preselects * an episode opened from the library. */ export function NoteStudioDetail(props: { noteId: string; episode?: string }) { const t = useT(); const auth = useAuth(); const [notes, { refetch }] = useStudioNotes(); const note = createMemo(() => (notes() ?? []).find((row) => row.id === props.noteId) ?? null); // The audio rail is resizable from its left edge, remembered on this device. const [railWidth, setRailWidth] = createSignal(readRailWidth()); const clampRail = (value: number) => Math.round(Math.min(RAIL_MAX, Math.max(RAIL_MIN, value))); createEffect(() => { if (typeof localStorage !== "undefined") localStorage.setItem(RAIL_KEY, String(railWidth())); }); const startResize = (event: PointerEvent) => { event.preventDefault(); const handle = event.currentTarget as HTMLElement; handle.setPointerCapture(event.pointerId); const startX = event.clientX; const startWidth = railWidth(); const move = (next: PointerEvent) => setRailWidth(clampRail(startWidth + (startX - next.clientX))); const stop = () => { handle.removeEventListener("pointermove", move); handle.removeEventListener("pointerup", stop); handle.removeEventListener("pointercancel", stop); document.body.style.removeProperty("cursor"); document.body.style.removeProperty("user-select"); }; document.body.style.cursor = "col-resize"; document.body.style.userSelect = "none"; handle.addEventListener("pointermove", move); handle.addEventListener("pointerup", stop); handle.addEventListener("pointercancel", stop); }; const canManage = () => { const current = note(); const user = auth.user(); return !!current && !!user && (hasMinRole(user.role, "manager") || current.courseCreatorId === user.id); }; return ( }> void refetch()} /> }> {(current) => ( // Laid out like a Mistral Studio tool page (Text to Speech): a thin // top bar with the way back and the note's name, the work on a // wide canvas, and the audio controls on a rail at the right.
{t("aiStudio.backToLibrary")}
)}
); }