SurfSense/surfsense_web/features/artifacts-library/hooks/use-library-artifacts.ts
Anish Sarkar a8c1fb660d feat(rename): complete searchSpace to workspace transition across frontend and backend
- Introduced a comprehensive specification for renaming `searchSpace` to `workspace` in `surfsense_web` and `surfsense_desktop`, ensuring all TypeScript identifiers, React props, and local data structures are updated.
- Implemented migration shims for persisted local state to prevent data loss during the transition.
- Updated observability metrics and IPC channels to reflect the new naming convention.
- Removed legacy `active-search-space` module and replaced it with `active-workspace` to maintain consistency.
- Ensured no behavioral changes or data loss for users during the renaming process.
2026-07-06 15:12:40 +05:30

98 lines
3.1 KiB
TypeScript

import { useQuery } from "@tanstack/react-query";
import { imageGenerationsApiService } from "@/lib/apis/image-generations-api.service";
import { podcastsApiService } from "@/lib/apis/podcasts-api.service";
import { reportsApiService } from "@/lib/apis/reports-api.service";
import { videoPresentationsApiService } from "@/lib/apis/video-presentations-api.service";
import type { LibraryArtifact, LibraryArtifactStatus } from "../model/artifact";
function podcastStatus(status: string): LibraryArtifactStatus {
if (status === "ready") return "ready";
if (status === "failed" || status === "cancelled") return "error";
return "running";
}
function videoStatus(status: string): LibraryArtifactStatus {
if (status === "ready") return "ready";
if (status === "failed") return "error";
return "running";
}
// Each list is fetched independently; one failing source shouldn't blank the
// whole library, so failures degrade to an empty slice.
async function fetchLibraryArtifacts(workspaceId: number): Promise<LibraryArtifact[]> {
const [reports, podcasts, videos, images] = await Promise.all([
reportsApiService.list(workspaceId).catch(() => []),
podcastsApiService.list(workspaceId).catch(() => []),
videoPresentationsApiService.list(workspaceId).catch(() => []),
imageGenerationsApiService.list(workspaceId).catch(() => []),
]);
const artifacts: LibraryArtifact[] = [];
for (const report of reports) {
const isResume = report.content_type === "typst";
artifacts.push({
key: `report-${report.id}`,
kind: isResume ? "resume" : "report",
entityId: report.id,
title: report.title,
status: report.report_metadata?.status === "failed" ? "error" : "ready",
createdAt: report.created_at,
contentType: isResume ? "typst" : "markdown",
sourceThreadId: report.thread_id,
});
}
for (const podcast of podcasts) {
artifacts.push({
key: `podcast-${podcast.id}`,
kind: "podcast",
entityId: podcast.id,
title: podcast.title,
status: podcastStatus(podcast.status),
createdAt: podcast.created_at,
contentType: "markdown",
sourceThreadId: podcast.thread_id,
});
}
for (const video of videos) {
artifacts.push({
key: `video-${video.id}`,
kind: "video",
entityId: video.id,
title: video.title,
status: videoStatus(video.status),
createdAt: video.created_at,
contentType: "markdown",
sourceThreadId: video.thread_id,
});
}
for (const image of images) {
artifacts.push({
key: `image-${image.id}`,
kind: "image",
entityId: image.id,
title: image.prompt,
status: image.is_success ? "ready" : "error",
createdAt: image.created_at,
contentType: "markdown",
});
}
return artifacts.sort(
(a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
);
}
export function useLibraryArtifacts(workspaceId: number) {
const { data, isLoading, error, refetch } = useQuery({
queryKey: ["artifacts-library", workspaceId],
queryFn: () => fetchLibraryArtifacts(workspaceId),
enabled: Number.isFinite(workspaceId) && workspaceId > 0,
staleTime: 60 * 1000,
});
return { artifacts: data ?? [], loading: isLoading, error, refresh: refetch };
}