43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { preload } from "swr";
|
|
import { fetcher } from "@/lib/utils";
|
|
|
|
const CANVAS_CHAT_DATA_KEYS = [
|
|
"/api/models?includeUnavailable=1",
|
|
"/api/site-settings?scope=canvas-chat",
|
|
"/api/canvas-skill-tags",
|
|
] as const;
|
|
|
|
async function nullableNoStoreFetcher(url: string) {
|
|
const response = await fetch(url, { cache: "no-store" });
|
|
if (!response.ok) return null;
|
|
return response.json();
|
|
}
|
|
|
|
function preloadWithoutBlocking(
|
|
key: string,
|
|
dataFetcher: (url: string) => Promise<unknown>
|
|
) {
|
|
void Promise.resolve(preload(key, dataFetcher)).catch(() => undefined);
|
|
}
|
|
|
|
export function preloadCanvasChatData(canvasId?: string | null) {
|
|
CANVAS_CHAT_DATA_KEYS.forEach((key) => {
|
|
preloadWithoutBlocking(key, fetcher);
|
|
});
|
|
|
|
preloadWithoutBlocking(
|
|
"/api/user/membership-summary",
|
|
nullableNoStoreFetcher
|
|
);
|
|
preloadWithoutBlocking("/api/user/image-stats", nullableNoStoreFetcher);
|
|
|
|
const normalizedCanvasId = String(canvasId || "").trim();
|
|
if (normalizedCanvasId) {
|
|
const params = new URLSearchParams({
|
|
chatId: normalizedCanvasId,
|
|
scope: "canvas",
|
|
});
|
|
preloadWithoutBlocking(`/api/messages?${params.toString()}`, fetcher);
|
|
}
|
|
}
|