The studio lists the caller's own episodes
Browse files at this commit37d12e58d62a451f1123ce4a2050fe80f1218fb5Parent: 5352a4a- feat
- the studio lists the caller's own episodes
8 changed files +290 -0
- src/api/client/types.ts +24 -0
- src/api/podcast/index.ts +1 -0
- src/api/podcast/listPodcastJobs.ts +10 -0
- src/components/notes/podcast-history.test.tsx +110 -0
- src/components/notes/podcast-history.tsx +117 -0
- src/components/notes/podcast-panel.test.tsx +1 -0
- src/components/notes/podcast-panel.tsx +3 -0
- src/i18n/messages.ts +24 -0
src/api/client/types.tsmodified+24 -0
| @@ -1392,3 +1392,27 @@ export type PodcastCancelVerdict = { | |||
| 1392 | /** Whether *this call* cancelled something. */ | 1392 | /** Whether *this call* cancelled something. */ |
| 1393 | cancelled: boolean; | 1393 | cancelled: boolean; |
| 1394 | }; | 1394 | }; |
| 1395 | + | ||
| 1396 | +/** One row in the caller's own episode history — `GET /podcast/jobs`. The | ||
| 1397 | + * backend's own row plus the title of the course note it narrates; every field | ||
| 1398 | + * past `job_id`/`state`/`source_id`/`created_at` can be `null` until the job | ||
| 1399 | + * settles (or forever, if the note is gone). */ | ||
| 1400 | +export type PodcastJobSummary = { | ||
| 1401 | + job_id: string; | ||
| 1402 | + /** `queued` | `running` | `done` | `failed` | `cancelled`. */ | ||
| 1403 | + state: string; | ||
| 1404 | + /** The narration the service resolved; `null` until its first report. */ | ||
| 1405 | + format: PodcastFormat | null; | ||
| 1406 | + /** The course note this episode narrates. */ | ||
| 1407 | + source_id: string; | ||
| 1408 | + /** That note's title; `null` once the note is gone — fall back to `source_id`. */ | ||
| 1409 | + source_title: string | null; | ||
| 1410 | + /** UTC unix-milliseconds, server-stamped. */ | ||
| 1411 | + created_at: number; | ||
| 1412 | + /** When the job reached its terminal state; `null` while it is live. */ | ||
| 1413 | + finished_at: number | null; | ||
| 1414 | + /** The finished episode's running time in seconds; `null` until then. */ | ||
| 1415 | + duration_secs: number | null; | ||
| 1416 | + /** The failure's own code, once one is on the record. */ | ||
| 1417 | + error_code: string | null; | ||
| 1418 | +}; | ||
src/api/podcast/index.tsmodified+1 -0
| @@ -1,4 +1,5 @@ | |||
| 1 | export * from "./postPodcastJob"; | 1 | export * from "./postPodcastJob"; |
| 2 | +export * from "./listPodcastJobs"; | ||
| 2 | export * from "./getPodcastJobById"; | 3 | export * from "./getPodcastJobById"; |
| 3 | export * from "./getPodcastJobResultById"; | 4 | export * from "./getPodcastJobResultById"; |
| 4 | export * from "./postPodcastJobCancel"; | 5 | export * from "./postPodcastJobCancel"; |
src/api/podcast/listPodcastJobs.tsadded+10 -0
| @@ -0,0 +1,10 @@ | |||
| 1 | +import { client, pageQuery, type Page, type PageParams, type PodcastJobSummary } from "../client"; | ||
| 2 | + | ||
| 3 | +/** | ||
| 4 | + * The caller's own episodes, newest first — every job the backend ever minted | ||
| 5 | + * for this user, whatever state it stopped in. A reading door: it answers from | ||
| 6 | + * the rows alone, so it still lists the history with the service down. | ||
| 7 | + */ | ||
| 8 | +export function listPodcastJobs(params?: PageParams, signal?: AbortSignal): Promise<Page<PodcastJobSummary>> { | ||
| 9 | + return client<Page<PodcastJobSummary>>(`/podcast/jobs${pageQuery(params)}`, { signal }); | ||
| 10 | +} | ||
src/components/notes/podcast-history.test.tsxadded+110 -0
| @@ -0,0 +1,110 @@ | |||
| 1 | +import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; | ||
| 2 | +import { createSignal } from "solid-js"; | ||
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| 4 | +import { PodcastHistory } from "@/components/notes/podcast-history"; | ||
| 5 | +import type { PodcastJobSummary } from "@/api/client"; | ||
| 6 | +import { PreferencesProvider } from "@/stores/preferences-context"; | ||
| 7 | + | ||
| 8 | +const podcastApi = vi.hoisted(() => ({ | ||
| 9 | + listPodcastJobs: vi.fn(), | ||
| 10 | + podcastAudioUrl: vi.fn((jobId: string) => `/api/podcast/jobs/${encodeURIComponent(jobId)}/audio`), | ||
| 11 | +})); | ||
| 12 | + | ||
| 13 | +vi.mock("@/api/podcast", () => podcastApi); | ||
| 14 | + | ||
| 15 | +function page(items: PodcastJobSummary[]) { | ||
| 16 | + return { items, total: items.length, limit: 10, offset: 0 }; | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +function job(over: Partial<PodcastJobSummary>): PodcastJobSummary { | ||
| 20 | + return { | ||
| 21 | + job_id: "job-1", | ||
| 22 | + state: "done", | ||
| 23 | + format: "duz_okuma", | ||
| 24 | + source_id: "note-1", | ||
| 25 | + source_title: "Hücre", | ||
| 26 | + created_at: Date.UTC(2026, 0, 2), | ||
| 27 | + finished_at: Date.UTC(2026, 0, 2), | ||
| 28 | + duration_secs: 754, | ||
| 29 | + error_code: null, | ||
| 30 | + ...over, | ||
| 31 | + }; | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +describe("PodcastHistory", () => { | ||
| 35 | + beforeEach(() => { | ||
| 36 | + // The panel's own test defaults to English; this one pins Turkish so the | ||
| 37 | + // exact labels the spec names are the ones asserted. | ||
| 38 | + localStorage.setItem("hezarfen.locale", "tr"); | ||
| 39 | + }); | ||
| 40 | + | ||
| 41 | + afterEach(() => { | ||
| 42 | + cleanup(); | ||
| 43 | + localStorage.clear(); | ||
| 44 | + vi.clearAllMocks(); | ||
| 45 | + }); | ||
| 46 | + | ||
| 47 | + const renderHistory = () => | ||
| 48 | + render(() => ( | ||
| 49 | + <PreferencesProvider> | ||
| 50 | + <PodcastHistory active /> | ||
| 51 | + </PreferencesProvider> | ||
| 52 | + )); | ||
| 53 | + | ||
| 54 | + it("renders a finished row's title, state and duration, and plays it by job id", async () => { | ||
| 55 | + podcastApi.listPodcastJobs.mockResolvedValue(page([job({})])); | ||
| 56 | + | ||
| 57 | + renderHistory(); | ||
| 58 | + | ||
| 59 | + await waitFor(() => expect(screen.getByText("Hücre")).toBeTruthy()); | ||
| 60 | + expect(screen.getByText("Hazır")).toBeTruthy(); | ||
| 61 | + expect(screen.getByText("12:34")).toBeTruthy(); | ||
| 62 | + | ||
| 63 | + fireEvent.click(screen.getByRole("button", { name: "Bölümü oynat" })); | ||
| 64 | + expect(podcastApi.podcastAudioUrl).toHaveBeenCalledWith("job-1"); | ||
| 65 | + expect(document.querySelector("audio")?.getAttribute("src")).toBe("/api/podcast/jobs/job-1/audio"); | ||
| 66 | + }); | ||
| 67 | + | ||
| 68 | + it("gives a non-done row no play control", async () => { | ||
| 69 | + podcastApi.listPodcastJobs.mockResolvedValue( | ||
| 70 | + page([job({ state: "running", duration_secs: null, finished_at: null })]), | ||
| 71 | + ); | ||
| 72 | + | ||
| 73 | + renderHistory(); | ||
| 74 | + | ||
| 75 | + await waitFor(() => expect(screen.getByText("Hazırlanıyor")).toBeTruthy()); | ||
| 76 | + expect(screen.queryByRole("button", { name: "Bölümü oynat" })).toBeNull(); | ||
| 77 | + expect(document.querySelector("audio")).toBeNull(); | ||
| 78 | + }); | ||
| 79 | + | ||
| 80 | + it("shows the empty state when the caller has no episodes", async () => { | ||
| 81 | + podcastApi.listPodcastJobs.mockResolvedValue(page([])); | ||
| 82 | + | ||
| 83 | + renderHistory(); | ||
| 84 | + | ||
| 85 | + await waitFor(() => expect(screen.getByText("Henüz bölüm oluşturulmadı")).toBeTruthy()); | ||
| 86 | + }); | ||
| 87 | + | ||
| 88 | + it("falls back to the note id when the note is gone", async () => { | ||
| 89 | + podcastApi.listPodcastJobs.mockResolvedValue(page([job({ source_id: "note-gone", source_title: null })])); | ||
| 90 | + | ||
| 91 | + renderHistory(); | ||
| 92 | + | ||
| 93 | + await waitFor(() => expect(screen.getByText("note-gone")).toBeTruthy()); | ||
| 94 | + }); | ||
| 95 | + | ||
| 96 | + it("refetches when the panel signals a finished generation", async () => { | ||
| 97 | + podcastApi.listPodcastJobs.mockResolvedValue(page([])); | ||
| 98 | + const [key, setKey] = createSignal<string>(""); | ||
| 99 | + | ||
| 100 | + render(() => ( | ||
| 101 | + <PreferencesProvider> | ||
| 102 | + <PodcastHistory active refetchKey={key()} /> | ||
| 103 | + </PreferencesProvider> | ||
| 104 | + )); | ||
| 105 | + | ||
| 106 | + await waitFor(() => expect(podcastApi.listPodcastJobs).toHaveBeenCalledTimes(1)); | ||
| 107 | + setKey("job-2"); | ||
| 108 | + await waitFor(() => expect(podcastApi.listPodcastJobs).toHaveBeenCalledTimes(2)); | ||
| 109 | + }); | ||
| 110 | +}); | ||
src/components/notes/podcast-history.tsxadded+117 -0
| @@ -0,0 +1,117 @@ | |||
| 1 | +import { For, Show, Suspense, createEffect, createSignal } from "solid-js"; | ||
| 2 | +import { createResource } from "@/lib/create-resource"; | ||
| 3 | +import { formatApiError } from "@/api/client"; | ||
| 4 | +import { listPodcastJobs, podcastAudioUrl } from "@/api/podcast"; | ||
| 5 | +import { Alert } from "@/components/ui/alert"; | ||
| 6 | +import { Badge } from "@/components/ui/badge"; | ||
| 7 | +import { Button } from "@/components/ui/button"; | ||
| 8 | +import { EmptyInline } from "@/components/ui/empty-inline"; | ||
| 9 | +import { PageSpinner } from "@/components/ui/page-spinner"; | ||
| 10 | +import { IconWaveform } from "@/components/ui/icons"; | ||
| 11 | +import { formatDate, formatDurationClock } from "@/lib/format"; | ||
| 12 | +import { usePreferences, useT } from "@/stores/preferences-context"; | ||
| 13 | + | ||
| 14 | +const HISTORY_PAGE_SIZE = 10; | ||
| 15 | + | ||
| 16 | +/** | ||
| 17 | + * The caller's own episodes, under the Ses Atölyesi panel. One-directional: | ||
| 18 | + * the studio produces, this only reads. A finished episode plays through the | ||
| 19 | + * same job-id audio door the panel's own player uses. | ||
| 20 | + */ | ||
| 21 | +export function PodcastHistory(props: { active?: boolean; refetchKey?: string | number }) { | ||
| 22 | + const t = useT(); | ||
| 23 | + const { locale } = usePreferences(); | ||
| 24 | + const [playing, setPlaying] = createSignal(""); | ||
| 25 | + const active = () => props.active !== false; | ||
| 26 | + | ||
| 27 | + const [list, { refetch }] = createResource( | ||
| 28 | + () => (active() ? true : null), | ||
| 29 | + () => listPodcastJobs({ limit: HISTORY_PAGE_SIZE }), | ||
| 30 | + ); | ||
| 31 | + | ||
| 32 | + // The panel bumps the key ("", then the job id) when a generation finishes; | ||
| 33 | + // pull a fresh page so the episode it just produced appears. The history | ||
| 34 | + // never starts work itself. | ||
| 35 | + createEffect(() => { | ||
| 36 | + if (props.refetchKey) void refetch(); | ||
| 37 | + }); | ||
| 38 | + | ||
| 39 | + const jobs = () => list()?.items ?? []; | ||
| 40 | + | ||
| 41 | + const stateVariant = (state: string) => { | ||
| 42 | + if (state === "done") return "success" as const; | ||
| 43 | + if (state === "failed") return "destructive" as const; | ||
| 44 | + if (state === "cancelled") return "secondary" as const; | ||
| 45 | + return "warning" as const; | ||
| 46 | + }; | ||
| 47 | + const stateLabel = (state: string) => { | ||
| 48 | + if (state === "done") return t("podcast.history.state.done"); | ||
| 49 | + if (state === "failed") return t("podcast.history.state.failed"); | ||
| 50 | + if (state === "cancelled") return t("podcast.history.state.cancelled"); | ||
| 51 | + return t("podcast.history.state.pending"); | ||
| 52 | + }; | ||
| 53 | + | ||
| 54 | + return ( | ||
| 55 | + <div class="space-y-3 border-t border-border-line pt-4"> | ||
| 56 | + <h3 class="text-sm font-semibold">{t("podcast.history.title")}</h3> | ||
| 57 | + | ||
| 58 | + <Show when={list.error}> | ||
| 59 | + <Alert variant="destructive">{formatApiError(list.error)}</Alert> | ||
| 60 | + </Show> | ||
| 61 | + | ||
| 62 | + <Show when={!list.error}> | ||
| 63 | + <Suspense fallback={<PageSpinner />}> | ||
| 64 | + <Show | ||
| 65 | + when={jobs().length > 0} | ||
| 66 | + fallback={ | ||
| 67 | + <EmptyInline | ||
| 68 | + title={t("podcast.history.empty")} | ||
| 69 | + hint={t("podcast.history.emptyHint")} | ||
| 70 | + /> | ||
| 71 | + } | ||
| 72 | + > | ||
| 73 | + <ul class="space-y-2"> | ||
| 74 | + <For each={jobs()}> | ||
| 75 | + {(row) => ( | ||
| 76 | + <li class="flex items-center gap-3 rounded-lg border border-border/60 p-3"> | ||
| 77 | + <div class="min-w-0 flex-1"> | ||
| 78 | + <p class="truncate text-sm font-medium">{row.source_title ?? row.source_id}</p> | ||
| 79 | + <div class="mt-1 flex flex-wrap items-center gap-2 text-xs text-muted-foreground"> | ||
| 80 | + <Badge variant={stateVariant(row.state)}>{stateLabel(row.state)}</Badge> | ||
| 81 | + <Show when={row.state === "failed" && row.error_code}> | ||
| 82 | + <span class="mono">{row.error_code}</span> | ||
| 83 | + </Show> | ||
| 84 | + <span>{formatDate(row.created_at, locale())}</span> | ||
| 85 | + <Show when={row.duration_secs != null}> | ||
| 86 | + <span class="tabular-nums">{formatDurationClock((row.duration_secs ?? 0) * 1000)}</span> | ||
| 87 | + </Show> | ||
| 88 | + </div> | ||
| 89 | + </div> | ||
| 90 | + <Show when={row.state === "done"}> | ||
| 91 | + <Button | ||
| 92 | + type="button" | ||
| 93 | + size="sm" | ||
| 94 | + variant="outline" | ||
| 95 | + class="shrink-0 rounded-lg" | ||
| 96 | + aria-label={t("podcast.history.play")} | ||
| 97 | + onClick={() => setPlaying(row.job_id)} | ||
| 98 | + > | ||
| 99 | + <IconWaveform class="h-4 w-4" /> | ||
| 100 | + </Button> | ||
| 101 | + </Show> | ||
| 102 | + </li> | ||
| 103 | + )} | ||
| 104 | + </For> | ||
| 105 | + </ul> | ||
| 106 | + </Show> | ||
| 107 | + </Suspense> | ||
| 108 | + </Show> | ||
| 109 | + | ||
| 110 | + <Show when={playing()}> | ||
| 111 | + <audio class="w-full" controls autoplay preload="metadata" src={podcastAudioUrl(playing())}> | ||
| 112 | + {t("podcast.audioUnsupported")} | ||
| 113 | + </audio> | ||
| 114 | + </Show> | ||
| 115 | + </div> | ||
| 116 | + ); | ||
| 117 | +} | ||
src/components/notes/podcast-panel.test.tsxmodified+1 -0
| @@ -8,6 +8,7 @@ const podcastApi = vi.hoisted(() => ({ | |||
| 8 | getPodcastJobById: vi.fn(), | 8 | getPodcastJobById: vi.fn(), |
| 9 | getPodcastJobResultById: vi.fn(), | 9 | getPodcastJobResultById: vi.fn(), |
| 10 | postPodcastJobCancel: vi.fn(), | 10 | postPodcastJobCancel: vi.fn(), |
| 11 | + listPodcastJobs: vi.fn(async () => ({ items: [], total: 0, limit: 10, offset: 0 })), | ||
| 11 | podcastAudioUrl: vi.fn((jobId: string) => `/api/podcast/jobs/${encodeURIComponent(jobId)}/audio`), | 12 | podcastAudioUrl: vi.fn((jobId: string) => `/api/podcast/jobs/${encodeURIComponent(jobId)}/audio`), |
| 12 | })); | 13 | })); |
| 13 | 14 | ||
src/components/notes/podcast-panel.tsxmodified+3 -0
| @@ -14,6 +14,7 @@ import { Button } from "@/components/ui/button"; | |||
| 14 | import { IconWaveform, IconX } from "@/components/ui/icons"; | 14 | import { IconWaveform, IconX } from "@/components/ui/icons"; |
| 15 | import { Label } from "@/components/ui/label"; | 15 | import { Label } from "@/components/ui/label"; |
| 16 | import { Select } from "@/components/ui/select"; | 16 | import { Select } from "@/components/ui/select"; |
| 17 | +import { PodcastHistory } from "@/components/notes/podcast-history"; | ||
| 17 | import { useT } from "@/stores/preferences-context"; | 18 | import { useT } from "@/stores/preferences-context"; |
| 18 | 19 | ||
| 19 | const POLL_MS = 2_000; | 20 | const POLL_MS = 2_000; |
| @@ -212,6 +213,8 @@ export function PodcastPanel(props: { noteId: string; active?: boolean }) { | |||
| 212 | </div> | 213 | </div> |
| 213 | )} | 214 | )} |
| 214 | </Show> | 215 | </Show> |
| 216 | + | ||
| 217 | + <PodcastHistory active={active()} refetchKey={status()?.state === "done" ? jobId() : ""} /> | ||
| 215 | </section> | 218 | </section> |
| 216 | ); | 219 | ); |
| 217 | } | 220 | } |
src/i18n/messages.tsmodified+24 -0
| @@ -903,6 +903,14 @@ export type MessageKey = | |||
| 903 | | "podcast.state.done" | 903 | | "podcast.state.done" |
| 904 | | "podcast.state.failed" | 904 | | "podcast.state.failed" |
| 905 | | "podcast.state.cancelled" | 905 | | "podcast.state.cancelled" |
| 906 | + | "podcast.history.title" | ||
| 907 | + | "podcast.history.state.pending" | ||
| 908 | + | "podcast.history.state.done" | ||
| 909 | + | "podcast.history.state.failed" | ||
| 910 | + | "podcast.history.state.cancelled" | ||
| 911 | + | "podcast.history.empty" | ||
| 912 | + | "podcast.history.emptyHint" | ||
| 913 | + | "podcast.history.play" | ||
| 906 | | "insights.title" | 914 | | "insights.title" |
| 907 | | "insights.subtitle" | 915 | | "insights.subtitle" |
| 908 | | "insights.student" | 916 | | "insights.student" |
| @@ -3013,6 +3021,14 @@ const en: Dict = { | |||
| 3013 | "podcast.state.done": "Ready", | 3021 | "podcast.state.done": "Ready", |
| 3014 | "podcast.state.failed": "Failed", | 3022 | "podcast.state.failed": "Failed", |
| 3015 | "podcast.state.cancelled": "Cancelled", | 3023 | "podcast.state.cancelled": "Cancelled", |
| 3024 | + "podcast.history.title": "Recent episodes", | ||
| 3025 | + "podcast.history.state.pending": "Preparing", | ||
| 3026 | + "podcast.history.state.done": "Ready", | ||
| 3027 | + "podcast.history.state.failed": "Failed", | ||
| 3028 | + "podcast.history.state.cancelled": "Cancelled", | ||
| 3029 | + "podcast.history.empty": "No episodes created yet", | ||
| 3030 | + "podcast.history.emptyHint": "Episodes appear here once the studio produces one.", | ||
| 3031 | + "podcast.history.play": "Play episode", | ||
| 3016 | "insights.title": "Hezarfen Intelligence", | 3032 | "insights.title": "Hezarfen Intelligence", |
| 3017 | "insights.subtitle": "Review student signals, recommendations, and nightly analysis runs.", | 3033 | "insights.subtitle": "Review student signals, recommendations, and nightly analysis runs.", |
| 3018 | "insights.student": "Student", | 3034 | "insights.student": "Student", |
| @@ -5125,6 +5141,14 @@ const tr: Dict = { | |||
| 5125 | "podcast.state.done": "Hazır", | 5141 | "podcast.state.done": "Hazır", |
| 5126 | "podcast.state.failed": "Başarısız", | 5142 | "podcast.state.failed": "Başarısız", |
| 5127 | "podcast.state.cancelled": "İptal edildi", | 5143 | "podcast.state.cancelled": "İptal edildi", |
| 5144 | + "podcast.history.title": "Son bölümler", | ||
| 5145 | + "podcast.history.state.pending": "Hazırlanıyor", | ||
| 5146 | + "podcast.history.state.done": "Hazır", | ||
| 5147 | + "podcast.history.state.failed": "Başarısız", | ||
| 5148 | + "podcast.history.state.cancelled": "İptal edildi", | ||
| 5149 | + "podcast.history.empty": "Henüz bölüm oluşturulmadı", | ||
| 5150 | + "podcast.history.emptyHint": "Stüdyo bir bölüm ürettiğinde burada görünür.", | ||
| 5151 | + "podcast.history.play": "Bölümü oynat", | ||
| 5128 | "insights.title": "Hezarfen Zeka", | 5152 | "insights.title": "Hezarfen Zeka", |
| 5129 | "insights.subtitle": "Öğrenci sinyallerini, önerileri ve gece analizlerini inceleyin.", | 5153 | "insights.subtitle": "Öğrenci sinyallerini, önerileri ve gece analizlerini inceleyin.", |
| 5130 | "insights.student": "Öğrenci", | 5154 | "insights.student": "Öğrenci", |