2026-07-26 16:55:20 +07:00
|
|
|
import { useEffect } from "react";
|
2026-08-01 09:19:39 +07:00
|
|
|
import useSWR, { useSWRConfig } from "swr";
|
|
|
|
|
import { useAction } from "@/hooks/use-action";
|
2026-07-26 16:14:32 +07:00
|
|
|
import { recordingsApi } from "@/lib/api";
|
2026-08-26 17:05:41 +07:00
|
|
|
import type { PaginatedRecordings, VoiceRecording } from "@/lib/types";
|
2026-07-27 21:54:31 +07:00
|
|
|
import type { WsHook } from "@/lib/ws-hook";
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-08-30 12:59:46 +07:00
|
|
|
const RECORDINGS_PREFIX = "recordings";
|
2026-08-01 09:19:39 +07:00
|
|
|
|
2026-08-30 12:59:46 +07:00
|
|
|
function recordingsKey(userId?: string): [string, string] {
|
|
|
|
|
return [RECORDINGS_PREFIX, userId ?? "all"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Revalidate/mutate every recording cache (all filters + global list). */
|
|
|
|
|
function isRecordingsKey(key: unknown): boolean {
|
|
|
|
|
return (
|
|
|
|
|
Array.isArray(key) &&
|
|
|
|
|
key.length === 2 &&
|
|
|
|
|
key[0] === RECORDINGS_PREFIX &&
|
|
|
|
|
typeof key[1] === "string"
|
2026-08-26 17:05:41 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-30 12:59:46 +07:00
|
|
|
export function useRecordingsPage(
|
|
|
|
|
initialPage?: PaginatedRecordings,
|
|
|
|
|
userId?: string,
|
|
|
|
|
) {
|
|
|
|
|
return useSWR<PaginatedRecordings>(
|
|
|
|
|
recordingsKey(userId),
|
|
|
|
|
() => recordingsApi.list(50, undefined, userId),
|
|
|
|
|
// fallbackData only applies to the unfiltered "all" view; a filtered cache
|
|
|
|
|
// must not be pre-seeded with unfiltered rows (would flash wrong data).
|
|
|
|
|
{ fallbackData: userId ? undefined : initialPage },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useRecordings(
|
|
|
|
|
initialPage?: PaginatedRecordings,
|
|
|
|
|
userId?: string,
|
|
|
|
|
) {
|
|
|
|
|
const page = useRecordingsPage(initialPage, userId);
|
2026-08-26 17:05:41 +07:00
|
|
|
return {
|
|
|
|
|
...page,
|
|
|
|
|
data: page.data?.items,
|
|
|
|
|
nextCursor: page.data?.nextCursor ?? null,
|
|
|
|
|
hasMore: page.data?.hasMore ?? false,
|
|
|
|
|
refetch: () => page.mutate(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-30 12:59:46 +07:00
|
|
|
export function useLoadMoreRecordings(userId?: string) {
|
2026-08-26 17:05:41 +07:00
|
|
|
const { mutate } = useSWRConfig();
|
|
|
|
|
return useAction(
|
2026-08-30 12:59:46 +07:00
|
|
|
async ({ channelId, cursor }: { channelId?: string; cursor: string }) => {
|
2026-08-26 17:05:41 +07:00
|
|
|
const result = await recordingsApi.list(50, channelId, userId, cursor);
|
|
|
|
|
await mutate(
|
2026-08-30 12:59:46 +07:00
|
|
|
recordingsKey(userId),
|
2026-08-26 17:05:41 +07:00
|
|
|
(old: PaginatedRecordings | undefined): PaginatedRecordings => {
|
|
|
|
|
if (!old) return result;
|
|
|
|
|
const existingIds = new Set(old.items.map((r) => r.id));
|
|
|
|
|
const newUnique = result.items.filter((r) => !existingIds.has(r.id));
|
|
|
|
|
return {
|
|
|
|
|
items: [...old.items, ...newUnique],
|
|
|
|
|
nextCursor: result.nextCursor,
|
|
|
|
|
hasMore: result.hasMore,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
{ revalidate: false },
|
|
|
|
|
);
|
|
|
|
|
return result;
|
2026-08-07 10:44:03 +07:00
|
|
|
},
|
|
|
|
|
);
|
2026-07-26 16:14:32 +07:00
|
|
|
}
|
|
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
export function useDeleteRecording() {
|
2026-08-01 09:19:39 +07:00
|
|
|
const { mutate } = useSWRConfig();
|
|
|
|
|
return useAction((id: string) => recordingsApi.delete(id), {
|
2026-08-26 17:05:41 +07:00
|
|
|
onSuccess: (_, id) => {
|
2026-08-30 12:59:46 +07:00
|
|
|
// A deleted recording disappears from every filter view.
|
2026-08-26 17:05:41 +07:00
|
|
|
void mutate(
|
2026-08-30 12:59:46 +07:00
|
|
|
(key: unknown) => isRecordingsKey(key),
|
2026-08-26 17:05:41 +07:00
|
|
|
(
|
|
|
|
|
old: PaginatedRecordings | undefined,
|
|
|
|
|
): PaginatedRecordings | undefined => {
|
|
|
|
|
if (!old) return old;
|
|
|
|
|
return {
|
|
|
|
|
...old,
|
|
|
|
|
items: old.items.filter((r) => r.id !== id),
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
{ revalidate: false },
|
|
|
|
|
);
|
2026-08-01 09:19:39 +07:00
|
|
|
},
|
2026-07-26 16:55:20 +07:00
|
|
|
});
|
|
|
|
|
}
|
2026-07-26 16:14:32 +07:00
|
|
|
|
2026-07-26 16:55:20 +07:00
|
|
|
export function useRecordingsWsSync(ws: WsHook) {
|
2026-08-01 09:19:39 +07:00
|
|
|
const { mutate } = useSWRConfig();
|
2026-07-26 16:46:53 +07:00
|
|
|
useEffect(() => {
|
2026-07-26 16:55:20 +07:00
|
|
|
const unsub = ws.on("voice_recording_uploaded", (data) => {
|
|
|
|
|
const rec = data as VoiceRecording;
|
2026-08-30 12:59:46 +07:00
|
|
|
// A fresh recording should appear in every filter view it belongs to.
|
2026-08-01 09:19:39 +07:00
|
|
|
void mutate(
|
2026-08-30 12:59:46 +07:00
|
|
|
(key: unknown) => isRecordingsKey(key),
|
2026-08-26 17:05:41 +07:00
|
|
|
(old: PaginatedRecordings | undefined): PaginatedRecordings => {
|
|
|
|
|
if (!old) return { items: [rec], nextCursor: null, hasMore: false };
|
|
|
|
|
if (old.items.some((r) => r.id === rec.id)) return old;
|
|
|
|
|
return {
|
|
|
|
|
...old,
|
|
|
|
|
items: [rec, ...old.items],
|
|
|
|
|
};
|
|
|
|
|
},
|
2026-08-01 09:19:39 +07:00
|
|
|
{ revalidate: false },
|
2026-07-26 16:55:20 +07:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
return unsub;
|
2026-08-01 09:19:39 +07:00
|
|
|
}, [ws, mutate]);
|
2026-07-26 16:14:32 +07:00
|
|
|
}
|