hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { Show, Suspense, createEffect, createMemo, createSignal } from "solid-js"; |
| 2 | import { Link, useNavigate } from "@tanstack/solid-router"; |
| 3 | import { createResource } from "@/lib/create-resource"; |
| 4 | import { getCourseNotes } from "@/api/course-notes"; |
| 5 | import { getCourses } from "@/api/courses"; |
| 6 | import { formatApiError, type CourseNote } from "@/api/client"; |
| 7 | import { PodcastPanel } from "@/components/notes/podcast-panel"; |
| 8 | import { RagOutputsPanel } from "@/components/notes/rag-outputs-panel"; |
| 9 | import { StudioOutputLibrary } from "@/components/ai/studio-output-library"; |
| 10 | import { RecordNotFound } from "@/components/layout/record-not-found"; |
| 11 | import { buttonVariants } from "@/components/ui/button"; |
| 12 | import { EmptyState } from "@/components/ui/empty-state"; |
| 13 | import { ErrorAlert } from "@/components/ui/error-alert"; |
| 14 | import { IconChevronLeft } from "@/components/ui/icons"; |
| 15 | import { Label } from "@/components/ui/label"; |
| 16 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 17 | import { SearchableSelect } from "@/components/ui/searchable-select"; |
| 18 | import { useAuth } from "@/stores/auth-context"; |
| 19 | import { useT } from "@/stores/preferences-context"; |
| 20 | import { cn } from "@/lib/cn"; |
| 21 | import { courseNoteFiles } from "@/lib/note-source"; |
| 22 | import { hasMinRole } from "@/lib/roles"; |
| 23 | |
| 24 | const RAIL_KEY = "hezarfen.studio.railWidth"; |
| 25 | const RAIL_MIN = 280; |
| 26 | const RAIL_MAX = 720; |
| 27 | const RAIL_DEFAULT = 352; |
| 28 | |
| 29 | function readRailWidth(): number { |
| 30 | if (typeof localStorage === "undefined") return RAIL_DEFAULT; |
| 31 | const value = Number(localStorage.getItem(RAIL_KEY)); |
| 32 | return Number.isFinite(value) && value >= RAIL_MIN && value <= RAIL_MAX ? value : RAIL_DEFAULT; |
| 33 | } |
| 34 | |
| 35 | type StudioNote = CourseNote & { courseTitle: string; courseCreatorId: string }; |
| 36 | const studioNotesCache = new Map<string, Promise<StudioNote[]>>(); |
| 37 | |
| 38 | /** Every lesson note the account can produce from, with its course. */ |
| 39 | export async function loadStudioNotes(): Promise<StudioNote[]> { |
| 40 | // GET /courses is already scoped to what the caller teaches or takes. |
| 41 | const courses = await getCourses({ limit: 100 }); |
| 42 | const rows: StudioNote[][] = Array.from({ length: courses.items.length }, () => []); |
| 43 | let next = 0; |
| 44 | await Promise.all(Array.from({ length: Math.min(4, courses.items.length) }, async () => { |
| 45 | while (next < courses.items.length) { |
| 46 | const index = next++; |
| 47 | const course = courses.items[index]!; |
| 48 | try { |
| 49 | const page = await getCourseNotes(course.id, { limit: 100 }); |
| 50 | rows[index] = page.items.map<StudioNote>((note) => ({ |
| 51 | ...note, |
| 52 | courseTitle: course.title, |
| 53 | courseCreatorId: course.creator.id, |
| 54 | })); |
| 55 | } catch { |
| 56 | rows[index] = []; |
| 57 | } |
| 58 | } |
| 59 | })); |
| 60 | return rows.flat(); |
| 61 | } |
| 62 | |
| 63 | export function getCachedStudioNotes(userId: string): Promise<StudioNote[]> { |
| 64 | let pending = studioNotesCache.get(userId); |
| 65 | if (!pending) { |
| 66 | pending = loadStudioNotes().catch((error) => { |
| 67 | studioNotesCache.delete(userId); |
| 68 | throw error; |
| 69 | }); |
| 70 | studioNotesCache.set(userId, pending); |
| 71 | } |
| 72 | return pending; |
| 73 | } |
| 74 | |
| 75 | function useStudioNotes() { |
| 76 | const auth = useAuth(); |
| 77 | return createResource(() => auth.user()?.id ?? null, (userId) => userId ? getCachedStudioNotes(userId) : []); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * The studio's landing page: what it makes, a picker to start on any note, |
| 82 | * and the library of everything produced so far. Picking a note or a row |
| 83 | * opens that note's own page (`/ai/studio/$noteId`); nothing opens in place. |
| 84 | */ |
| 85 | export function NoteStudioPanel() { |
| 86 | const t = useT(); |
| 87 | const navigate = useNavigate(); |
| 88 | const [notes, { refetch }] = useStudioNotes(); |
| 89 | const options = createMemo(() => (notes() ?? []).map((note) => ({ value: note.id, label: `${note.courseTitle} · ${note.title}` }))); |
| 90 | const open = (noteId: string, episode?: string) => { |
| 91 | if (!noteId) return; |
| 92 | void navigate({ to: "/ai/studio/$noteId", params: { noteId }, search: { episode } }); |
| 93 | }; |
| 94 | |
| 95 | return ( |
| 96 | <div class="w-full space-y-8"> |
| 97 | <header class="space-y-1.5"> |
| 98 | <h1 class="text-2xl font-semibold tracking-tight text-text-strong">{t("aiHub.tab.studio")}</h1> |
| 99 | <p class="text-sm text-muted-foreground">{t("aiStudio.heading")}</p> |
| 100 | </header> |
| 101 | <Suspense fallback={<PageSpinner />}> |
| 102 | <Show when={notes.error}> |
| 103 | <ErrorAlert message={formatApiError(notes.error)} onRetry={() => void refetch()} /> |
| 104 | </Show> |
| 105 | <Show when={!notes.error && notes()}> |
| 106 | <Show |
| 107 | when={(notes()?.length ?? 0) > 0} |
| 108 | fallback={<EmptyState kind="notes" title={t("podcast.noNotes")} description={t("podcast.noNotesHint")} />} |
| 109 | > |
| 110 | {/* A studio home: a plain title and one line saying what it makes, |
| 111 | the way to start on a note, then everything produced so far. |
| 112 | Flat hairline cards, no banner and no shadow stack. */} |
| 113 | <div class="space-y-8"> |
| 114 | <div class="max-w-xl space-y-2"> |
| 115 | <Label for="sound-studio-note">{t("podcast.source")}</Label> |
| 116 | <SearchableSelect |
| 117 | id="sound-studio-note" |
| 118 | class="w-full" |
| 119 | value="" |
| 120 | onChange={(noteId) => open(noteId)} |
| 121 | options={options()} |
| 122 | placeholder={t("podcast.selectNote")} |
| 123 | /> |
| 124 | </div> |
| 125 | |
| 126 | <StudioOutputLibrary |
| 127 | notes={(notes() ?? []).map((note) => ({ id: note.id, title: note.title, courseTitle: note.courseTitle }))} |
| 128 | selectedId="" |
| 129 | onSelect={open} |
| 130 | /> |
| 131 | </div> |
| 132 | </Show> |
| 133 | </Show> |
| 134 | </Suspense> |
| 135 | </div> |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * One note's studio page: its summary and its episodes, nothing else — no |
| 141 | * picker, no library. "Kitaplığa dön" is the way back. `episode` preselects |
| 142 | * an episode opened from the library. |
| 143 | */ |
| 144 | export function NoteStudioDetail(props: { noteId: string; episode?: string }) { |
| 145 | const t = useT(); |
| 146 | const auth = useAuth(); |
| 147 | const [notes, { refetch }] = useStudioNotes(); |
| 148 | const note = createMemo(() => (notes() ?? []).find((row) => row.id === props.noteId) ?? null); |
| 149 | // The audio rail is resizable from its left edge, remembered on this device. |
| 150 | const [railWidth, setRailWidth] = createSignal(readRailWidth()); |
| 151 | const clampRail = (value: number) => Math.round(Math.min(RAIL_MAX, Math.max(RAIL_MIN, value))); |
| 152 | createEffect(() => { |
| 153 | if (typeof localStorage !== "undefined") localStorage.setItem(RAIL_KEY, String(railWidth())); |
| 154 | }); |
| 155 | const startResize = (event: PointerEvent) => { |
| 156 | event.preventDefault(); |
| 157 | const handle = event.currentTarget as HTMLElement; |
| 158 | handle.setPointerCapture(event.pointerId); |
| 159 | const startX = event.clientX; |
| 160 | const startWidth = railWidth(); |
| 161 | const move = (next: PointerEvent) => setRailWidth(clampRail(startWidth + (startX - next.clientX))); |
| 162 | const stop = () => { |
| 163 | handle.removeEventListener("pointermove", move); |
| 164 | handle.removeEventListener("pointerup", stop); |
| 165 | handle.removeEventListener("pointercancel", stop); |
| 166 | document.body.style.removeProperty("cursor"); |
| 167 | document.body.style.removeProperty("user-select"); |
| 168 | }; |
| 169 | document.body.style.cursor = "col-resize"; |
| 170 | document.body.style.userSelect = "none"; |
| 171 | handle.addEventListener("pointermove", move); |
| 172 | handle.addEventListener("pointerup", stop); |
| 173 | handle.addEventListener("pointercancel", stop); |
| 174 | }; |
| 175 | const canManage = () => { |
| 176 | const current = note(); |
| 177 | const user = auth.user(); |
| 178 | return !!current && !!user && (hasMinRole(user.role, "manager") || current.courseCreatorId === user.id); |
| 179 | }; |
| 180 | |
| 181 | return ( |
| 182 | <Suspense fallback={<PageSpinner />}> |
| 183 | <Show when={notes.error}> |
| 184 | <ErrorAlert message={formatApiError(notes.error)} onRetry={() => void refetch()} /> |
| 185 | </Show> |
| 186 | <Show when={!notes.error && notes()}> |
| 187 | <Show when={note()} fallback={<RecordNotFound backTo="/ai/studio" />}> |
| 188 | {(current) => ( |
| 189 | // Laid out like a Mistral Studio tool page (Text to Speech): a thin |
| 190 | // top bar with the way back and the note's name, the work on a |
| 191 | // wide canvas, and the audio controls on a rail at the right. |
| 192 | <div class="flex w-full flex-col lg:-mx-10 lg:-my-6 lg:h-[calc(100dvh-49px-env(safe-area-inset-top))] lg:w-auto"> |
| 193 | <div class="flex min-h-12 shrink-0 flex-wrap items-center gap-x-3 gap-y-1 border-b border-border-hairline pb-3 lg:px-6 lg:pb-0"> |
| 194 | <Link |
| 195 | to="/ai/studio" |
| 196 | class={cn(buttonVariants({ variant: "ghost", size: "sm" }), "-ml-2 h-10 gap-1 rounded-lg text-muted-foreground sm:h-8 touch:h-10")} |
| 197 | > |
| 198 | <IconChevronLeft class="h-4 w-4" /> |
| 199 | {t("aiStudio.backToLibrary")} |
| 200 | </Link> |
| 201 | <span class="hidden h-4 w-px bg-border-line sm:block" aria-hidden="true" /> |
| 202 | <h1 class="min-w-0 truncate text-sm font-semibold text-text-strong">{current().title}</h1> |
| 203 | <span class="rounded-md bg-primary/10 px-1.5 py-0.5 text-xs font-medium text-primary-text">{current().courseTitle}</span> |
| 204 | </div> |
| 205 | <div |
| 206 | class="grid min-h-0 flex-1 grid-cols-1 lg:grid-cols-[minmax(0,1fr)_var(--studio-rail)]" |
| 207 | style={{ "--studio-rail": `${railWidth()}px` }} |
| 208 | > |
| 209 | <div class="min-w-0 py-6 lg:overflow-y-auto lg:px-8"> |
| 210 | <div class="mx-auto w-full max-w-3xl"> |
| 211 | <RagOutputsPanel |
| 212 | noteId={current().id} |
| 213 | source={courseNoteFiles} |
| 214 | canManage={canManage()} |
| 215 | active |
| 216 | flat |
| 217 | noteTitle={current().title} |
| 218 | /> |
| 219 | </div> |
| 220 | </div> |
| 221 | <aside class="relative min-w-0 border-t border-border-hairline py-6 lg:overflow-y-auto lg:border-l lg:border-t-0 lg:bg-surface-overlay/30 lg:px-5"> |
| 222 | {/* Drag (or arrow keys) to widen the rail; double-click resets it. */} |
| 223 | <div |
| 224 | role="separator" |
| 225 | aria-orientation="vertical" |
| 226 | aria-label={t("aiStudio.resizeRail")} |
| 227 | aria-valuemin={RAIL_MIN} |
| 228 | aria-valuemax={RAIL_MAX} |
| 229 | aria-valuenow={railWidth()} |
| 230 | tabIndex={0} |
| 231 | title={t("aiStudio.resizeHint")} |
| 232 | class="group absolute inset-y-0 left-0 z-10 hidden w-2 cursor-col-resize touch-none outline-hidden lg:block" |
| 233 | onPointerDown={startResize} |
| 234 | onDblClick={() => setRailWidth(RAIL_DEFAULT)} |
| 235 | onKeyDown={(event) => { |
| 236 | if (event.key === "ArrowLeft") setRailWidth((w) => clampRail(w + 24)); |
| 237 | else if (event.key === "ArrowRight") setRailWidth((w) => clampRail(w - 24)); |
| 238 | else return; |
| 239 | event.preventDefault(); |
| 240 | }} |
| 241 | > |
| 242 | <span class="block h-full w-px bg-transparent transition-colors group-hover:bg-primary/60 group-focus-visible:bg-primary group-active:bg-primary" /> |
| 243 | {/* A grip on the border, always visible, so the rail reads as |
| 244 | draggable before anyone hovers the hairline. */} |
| 245 | <span |
| 246 | aria-hidden="true" |
| 247 | class="absolute left-0 top-[45vh] flex h-9 w-3.5 -translate-x-1/2 items-center justify-center gap-[3px] rounded-full border border-border-line bg-surface-base text-text-subtle shadow-xs transition-colors group-hover:border-primary/60 group-hover:text-primary-text group-focus-visible:border-primary group-active:border-primary" |
| 248 | > |
| 249 | <span class="flex flex-col gap-[3px]"><span class="h-[3px] w-[3px] rounded-full bg-current" /><span class="h-[3px] w-[3px] rounded-full bg-current" /><span class="h-[3px] w-[3px] rounded-full bg-current" /></span> |
| 250 | </span> |
| 251 | </div> |
| 252 | <PodcastPanel noteId={current().id} noteTitle={current().title} episode={props.episode} active flat /> |
| 253 | </aside> |
| 254 | </div> |
| 255 | </div> |
| 256 | )} |
| 257 | </Show> |
| 258 | </Show> |
| 259 | </Suspense> |
| 260 | ); |
| 261 | } |