diff --git a/src/api/client/types.ts b/src/api/client/types.ts index 03a371a..46e8f18 100644 --- a/src/api/client/types.ts +++ b/src/api/client/types.ts @@ -1392,3 +1392,27 @@ export type PodcastCancelVerdict = { /** Whether *this call* cancelled something. */ cancelled: boolean; }; + +/** One row in the caller's own episode history — `GET /podcast/jobs`. The + * backend's own row plus the title of the course note it narrates; every field + * past `job_id`/`state`/`source_id`/`created_at` can be `null` until the job + * settles (or forever, if the note is gone). */ +export type PodcastJobSummary = { + job_id: string; + /** `queued` | `running` | `done` | `failed` | `cancelled`. */ + state: string; + /** The narration the service resolved; `null` until its first report. */ + format: PodcastFormat | null; + /** The course note this episode narrates. */ + source_id: string; + /** That note's title; `null` once the note is gone — fall back to `source_id`. */ + source_title: string | null; + /** UTC unix-milliseconds, server-stamped. */ + created_at: number; + /** When the job reached its terminal state; `null` while it is live. */ + finished_at: number | null; + /** The finished episode's running time in seconds; `null` until then. */ + duration_secs: number | null; + /** The failure's own code, once one is on the record. */ + error_code: string | null; +}; diff --git a/src/api/podcast/index.ts b/src/api/podcast/index.ts index 60a8069..f1571f4 100644 --- a/src/api/podcast/index.ts +++ b/src/api/podcast/index.ts @@ -1,4 +1,5 @@ export * from "./postPodcastJob"; +export * from "./listPodcastJobs"; export * from "./getPodcastJobById"; export * from "./getPodcastJobResultById"; export * from "./postPodcastJobCancel"; diff --git a/src/api/podcast/listPodcastJobs.ts b/src/api/podcast/listPodcastJobs.ts new file mode 100644 index 0000000..5ea30f9 --- /dev/null +++ b/src/api/podcast/listPodcastJobs.ts @@ -0,0 +1,10 @@ +import { client, pageQuery, type Page, type PageParams, type PodcastJobSummary } from "../client"; + +/** + * The caller's own episodes, newest first — every job the backend ever minted + * for this user, whatever state it stopped in. A reading door: it answers from + * the rows alone, so it still lists the history with the service down. + */ +export function listPodcastJobs(params?: PageParams, signal?: AbortSignal): Promise> { + return client>(`/podcast/jobs${pageQuery(params)}`, { signal }); +} diff --git a/src/components/notes/podcast-history.test.tsx b/src/components/notes/podcast-history.test.tsx new file mode 100644 index 0000000..062b145 --- /dev/null +++ b/src/components/notes/podcast-history.test.tsx @@ -0,0 +1,110 @@ +import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; +import { createSignal } from "solid-js"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { PodcastHistory } from "@/components/notes/podcast-history"; +import type { PodcastJobSummary } from "@/api/client"; +import { PreferencesProvider } from "@/stores/preferences-context"; + +const podcastApi = vi.hoisted(() => ({ + listPodcastJobs: vi.fn(), + podcastAudioUrl: vi.fn((jobId: string) => `/api/podcast/jobs/${encodeURIComponent(jobId)}/audio`), +})); + +vi.mock("@/api/podcast", () => podcastApi); + +function page(items: PodcastJobSummary[]) { + return { items, total: items.length, limit: 10, offset: 0 }; +} + +function job(over: Partial): PodcastJobSummary { + return { + job_id: "job-1", + state: "done", + format: "duz_okuma", + source_id: "note-1", + source_title: "Hücre", + created_at: Date.UTC(2026, 0, 2), + finished_at: Date.UTC(2026, 0, 2), + duration_secs: 754, + error_code: null, + ...over, + }; +} + +describe("PodcastHistory", () => { + beforeEach(() => { + // The panel's own test defaults to English; this one pins Turkish so the + // exact labels the spec names are the ones asserted. + localStorage.setItem("hezarfen.locale", "tr"); + }); + + afterEach(() => { + cleanup(); + localStorage.clear(); + vi.clearAllMocks(); + }); + + const renderHistory = () => + render(() => ( + + + + )); + + it("renders a finished row's title, state and duration, and plays it by job id", async () => { + podcastApi.listPodcastJobs.mockResolvedValue(page([job({})])); + + renderHistory(); + + await waitFor(() => expect(screen.getByText("Hücre")).toBeTruthy()); + expect(screen.getByText("Hazır")).toBeTruthy(); + expect(screen.getByText("12:34")).toBeTruthy(); + + fireEvent.click(screen.getByRole("button", { name: "Bölümü oynat" })); + expect(podcastApi.podcastAudioUrl).toHaveBeenCalledWith("job-1"); + expect(document.querySelector("audio")?.getAttribute("src")).toBe("/api/podcast/jobs/job-1/audio"); + }); + + it("gives a non-done row no play control", async () => { + podcastApi.listPodcastJobs.mockResolvedValue( + page([job({ state: "running", duration_secs: null, finished_at: null })]), + ); + + renderHistory(); + + await waitFor(() => expect(screen.getByText("Hazırlanıyor")).toBeTruthy()); + expect(screen.queryByRole("button", { name: "Bölümü oynat" })).toBeNull(); + expect(document.querySelector("audio")).toBeNull(); + }); + + it("shows the empty state when the caller has no episodes", async () => { + podcastApi.listPodcastJobs.mockResolvedValue(page([])); + + renderHistory(); + + await waitFor(() => expect(screen.getByText("Henüz bölüm oluşturulmadı")).toBeTruthy()); + }); + + it("falls back to the note id when the note is gone", async () => { + podcastApi.listPodcastJobs.mockResolvedValue(page([job({ source_id: "note-gone", source_title: null })])); + + renderHistory(); + + await waitFor(() => expect(screen.getByText("note-gone")).toBeTruthy()); + }); + + it("refetches when the panel signals a finished generation", async () => { + podcastApi.listPodcastJobs.mockResolvedValue(page([])); + const [key, setKey] = createSignal(""); + + render(() => ( + + + + )); + + await waitFor(() => expect(podcastApi.listPodcastJobs).toHaveBeenCalledTimes(1)); + setKey("job-2"); + await waitFor(() => expect(podcastApi.listPodcastJobs).toHaveBeenCalledTimes(2)); + }); +}); diff --git a/src/components/notes/podcast-history.tsx b/src/components/notes/podcast-history.tsx new file mode 100644 index 0000000..79f0b53 --- /dev/null +++ b/src/components/notes/podcast-history.tsx @@ -0,0 +1,117 @@ +import { For, Show, Suspense, createEffect, createSignal } from "solid-js"; +import { createResource } from "@/lib/create-resource"; +import { formatApiError } from "@/api/client"; +import { listPodcastJobs, podcastAudioUrl } from "@/api/podcast"; +import { Alert } from "@/components/ui/alert"; +import { Badge } from "@/components/ui/badge"; +import { Button } from "@/components/ui/button"; +import { EmptyInline } from "@/components/ui/empty-inline"; +import { PageSpinner } from "@/components/ui/page-spinner"; +import { IconWaveform } from "@/components/ui/icons"; +import { formatDate, formatDurationClock } from "@/lib/format"; +import { usePreferences, useT } from "@/stores/preferences-context"; + +const HISTORY_PAGE_SIZE = 10; + +/** + * The caller's own episodes, under the Ses Atölyesi panel. One-directional: + * the studio produces, this only reads. A finished episode plays through the + * same job-id audio door the panel's own player uses. + */ +export function PodcastHistory(props: { active?: boolean; refetchKey?: string | number }) { + const t = useT(); + const { locale } = usePreferences(); + const [playing, setPlaying] = createSignal(""); + const active = () => props.active !== false; + + const [list, { refetch }] = createResource( + () => (active() ? true : null), + () => listPodcastJobs({ limit: HISTORY_PAGE_SIZE }), + ); + + // The panel bumps the key ("", then the job id) when a generation finishes; + // pull a fresh page so the episode it just produced appears. The history + // never starts work itself. + createEffect(() => { + if (props.refetchKey) void refetch(); + }); + + const jobs = () => list()?.items ?? []; + + const stateVariant = (state: string) => { + if (state === "done") return "success" as const; + if (state === "failed") return "destructive" as const; + if (state === "cancelled") return "secondary" as const; + return "warning" as const; + }; + const stateLabel = (state: string) => { + if (state === "done") return t("podcast.history.state.done"); + if (state === "failed") return t("podcast.history.state.failed"); + if (state === "cancelled") return t("podcast.history.state.cancelled"); + return t("podcast.history.state.pending"); + }; + + return ( +
+

{t("podcast.history.title")}

+ + + {formatApiError(list.error)} + + + + }> + 0} + fallback={ + + } + > +
    + + {(row) => ( +
  • +
    +

    {row.source_title ?? row.source_id}

    +
    + {stateLabel(row.state)} + + {row.error_code} + + {formatDate(row.created_at, locale())} + + {formatDurationClock((row.duration_secs ?? 0) * 1000)} + +
    +
    + + + +
  • + )} +
    +
+
+
+
+ + + + +
+ ); +} diff --git a/src/components/notes/podcast-panel.test.tsx b/src/components/notes/podcast-panel.test.tsx index f191f2a..8e3af7d 100644 --- a/src/components/notes/podcast-panel.test.tsx +++ b/src/components/notes/podcast-panel.test.tsx @@ -8,6 +8,7 @@ const podcastApi = vi.hoisted(() => ({ getPodcastJobById: vi.fn(), getPodcastJobResultById: vi.fn(), postPodcastJobCancel: vi.fn(), + listPodcastJobs: vi.fn(async () => ({ items: [], total: 0, limit: 10, offset: 0 })), podcastAudioUrl: vi.fn((jobId: string) => `/api/podcast/jobs/${encodeURIComponent(jobId)}/audio`), })); diff --git a/src/components/notes/podcast-panel.tsx b/src/components/notes/podcast-panel.tsx index cca6b18..65afc83 100644 --- a/src/components/notes/podcast-panel.tsx +++ b/src/components/notes/podcast-panel.tsx @@ -14,6 +14,7 @@ import { Button } from "@/components/ui/button"; import { IconWaveform, IconX } from "@/components/ui/icons"; import { Label } from "@/components/ui/label"; import { Select } from "@/components/ui/select"; +import { PodcastHistory } from "@/components/notes/podcast-history"; import { useT } from "@/stores/preferences-context"; const POLL_MS = 2_000; @@ -212,6 +213,8 @@ export function PodcastPanel(props: { noteId: string; active?: boolean }) { )} + + ); } diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts index 21b3651..b150089 100644 --- a/src/i18n/messages.ts +++ b/src/i18n/messages.ts @@ -903,6 +903,14 @@ export type MessageKey = | "podcast.state.done" | "podcast.state.failed" | "podcast.state.cancelled" + | "podcast.history.title" + | "podcast.history.state.pending" + | "podcast.history.state.done" + | "podcast.history.state.failed" + | "podcast.history.state.cancelled" + | "podcast.history.empty" + | "podcast.history.emptyHint" + | "podcast.history.play" | "insights.title" | "insights.subtitle" | "insights.student" @@ -3013,6 +3021,14 @@ const en: Dict = { "podcast.state.done": "Ready", "podcast.state.failed": "Failed", "podcast.state.cancelled": "Cancelled", + "podcast.history.title": "Recent episodes", + "podcast.history.state.pending": "Preparing", + "podcast.history.state.done": "Ready", + "podcast.history.state.failed": "Failed", + "podcast.history.state.cancelled": "Cancelled", + "podcast.history.empty": "No episodes created yet", + "podcast.history.emptyHint": "Episodes appear here once the studio produces one.", + "podcast.history.play": "Play episode", "insights.title": "Hezarfen Intelligence", "insights.subtitle": "Review student signals, recommendations, and nightly analysis runs.", "insights.student": "Student", @@ -5125,6 +5141,14 @@ const tr: Dict = { "podcast.state.done": "Hazır", "podcast.state.failed": "Başarısız", "podcast.state.cancelled": "İptal edildi", + "podcast.history.title": "Son bölümler", + "podcast.history.state.pending": "Hazırlanıyor", + "podcast.history.state.done": "Hazır", + "podcast.history.state.failed": "Başarısız", + "podcast.history.state.cancelled": "İptal edildi", + "podcast.history.empty": "Henüz bölüm oluşturulmadı", + "podcast.history.emptyHint": "Stüdyo bir bölüm ürettiğinde burada görünür.", + "podcast.history.play": "Bölümü oynat", "insights.title": "Hezarfen Zeka", "insights.subtitle": "Öğrenci sinyallerini, önerileri ve gece analizlerini inceleyin.", "insights.student": "Öğrenci",