MicProtocol / apps /web /src /components /chat /MediaCommentList.tsx
Benaah's picture
Upload folder using huggingface_hub
fb12607 verified
Raw History Blame Contribute Delete
6.73 kB
"use client";
import { useState, useEffect, useRef } from "react";
import { trpc } from "@/lib/trpc";
import { formatTime, clsx } from "@/lib/utils";
export interface MediaCommentListProps {
episodeId: string;
assetId: string;
assetType?: "audio" | "video" | "transcript";
currentUserId: string;
/** Optional seek callback when a comment is clicked. */
onSeek?: (seconds: number) => void;
/** Click time of the underlying player. */
currentTime?: number;
}
/**
* A timestamped comment list pinned to an episode asset. Lets editors
* / producers drop comments at the exact second of an audio file.
* Backed by the tRPC `mediaComments` router (Postgres-backed) plus
* the chat panel for the inline message view.
*/
export function MediaCommentList(props: MediaCommentListProps) {
const utils = trpc.useUtils();
const [draft, setDraft] = useState("");
const [draftTime, setDraftTime] = useState<number | null>(null);
const list = trpc.mediaComments.list.useQuery({
episodeId: props.episodeId,
assetId: props.assetId,
} as any);
const create = trpc.mediaComments.create.useMutation({
onSuccess: () => {
utils.mediaComments.list.invalidate({
episodeId: props.episodeId,
assetId: props.assetId,
});
setDraft("");
setDraftTime(null);
},
});
const resolve = trpc.mediaComments.resolve.useMutation({
onSuccess: () => {
utils.mediaComments.list.invalidate({
episodeId: props.episodeId,
assetId: props.assetId,
});
},
});
const items = list.data ?? [];
return (
<div className="flex h-full flex-col rounded-lg border bg-white dark:border-zinc-800 dark:bg-zinc-950">
<header className="flex items-center justify-between border-b px-3 py-2 dark:border-zinc-800">
<h3 className="text-sm font-semibold">Timestamped notes</h3>
<span className="text-xs text-zinc-500">
{items.length} note{items.length === 1 ? "" : "s"}
</span>
</header>
<ul className="flex-1 divide-y overflow-y-auto dark:divide-zinc-800">
{items.length === 0 && (
<li className="px-3 py-6 text-center text-xs text-zinc-400">
No notes yet — pause the player and start typing.
</li>
)}
{items.map((c) => (
<li
key={c.id}
className={clsx(
"flex gap-3 px-3 py-2 text-sm",
c.status === "resolved" &&
"opacity-50 line-through",
)}
>
<button
onClick={() =>
props.onSeek?.(Number(c.mediaTimestampSeconds))
}
className="shrink-0 rounded bg-amber-100 px-2 py-0.5 text-xs font-mono text-amber-800 hover:bg-amber-200 dark:bg-amber-900/30 dark:text-amber-200"
>
▶ {formatTime(Number(c.mediaTimestampSeconds))}
</button>
<div className="flex-1">
<p className="whitespace-pre-wrap">{c.body}</p>
<p className="mt-1 text-[10px] uppercase tracking-wide text-zinc-400">
{(((c as any).user?.profile as any)?.displayName ??
(c as any).user?.email?.split("@")[0] ??
"Someone") as string}{" "}
· {new Date(c.createdAt).toLocaleString()}
</p>
</div>
{c.status === "open" && (
<button
onClick={() => resolve.mutate({ id: c.id })}
className="self-start text-[10px] text-emerald-600 hover:underline"
>
resolve
</button>
)}
</li>
))}
</ul>
<footer className="border-t p-2 dark:border-zinc-800">
<div className="mb-1 flex items-center gap-2 text-xs text-zinc-500">
<span>at</span>
<button
onClick={() =>
setDraftTime(props.currentTime ?? draftTime ?? 0)
}
className="rounded bg-zinc-100 px-2 py-0.5 font-mono text-xs hover:bg-zinc-200 dark:bg-zinc-800"
>
{formatTime(draftTime ?? props.currentTime ?? 0)}
</button>
</div>
<div className="flex items-end gap-2">
<textarea
value={draft}
onChange={(e) => {
setDraft(e.target.value);
if (draftTime == null && props.currentTime != null) {
setDraftTime(props.currentTime);
}
}}
placeholder="Drop a note at this timestamp…"
rows={2}
className="flex-1 resize-none rounded border bg-transparent p-2 text-sm outline-none focus:ring-2 focus:ring-blue-500 dark:border-zinc-800"
/>
<button
onClick={() => {
const t = draftTime ?? props.currentTime ?? 0;
if (!draft.trim()) return;
create.mutate({
episodeId: props.episodeId,
assetId: props.assetId,
assetType: props.assetType ?? "audio",
mediaTimestampSeconds: t,
body: draft.trim(),
});
}}
disabled={!draft.trim() || create.isPending}
className="rounded bg-blue-600 px-3 py-2 text-xs font-medium text-white disabled:cursor-not-allowed disabled:bg-zinc-300"
>
Add note
</button>
</div>
</footer>
</div>
);
}