import { createContext, useContext, useMemo, useLayoutEffect, useRef, type ReactNode } from "react"; import { useQueryClient } from "@tanstack/react-query"; import { createLinkPreviewPageLoader } from "@/lib/link-preview-page-loader"; import type { LinkPreviewBatchResult, LinkPreviewResult, LinkPreviewTarget } from "@paperclipai/shared"; import { api } from "@/api/client"; export interface LinkPreviewContextValue { companyId: string; userId: string | null; register?: (target: LinkPreviewTarget) => () => void; load: (target: LinkPreviewTarget) => Promise; } const LinkPreviewContext = createContext(null); export function LinkPreviewProvider({ companyId, userId, children, autoLoad = true }: { companyId: string | null; userId: string | null; children: ReactNode; autoLoad?: boolean; }) { const client = useQueryClient(); const pageLoader = useRef | null>(null); const value = useMemo(() => companyId ? ({ companyId, userId, register: target => pageLoader.current?.register(target) ?? (() => {}), load: target => api.get( `/companies/${companyId}/link-preview?${new URLSearchParams({ url: target.canonicalUrl })}`, { timeoutMs: 6_000, cache: "no-store" }, ), }) : null, [companyId, userId, autoLoad]); // Link registration uses passive effects, after this layout effect initializes the registry. useLayoutEffect(() => { if (!value || !autoLoad) return; const loader = createLinkPreviewPageLoader(client, { ...value, readCached: targets => api.get(`/companies/${companyId}/link-previews/cached?${ new URLSearchParams(targets.map(target => ["url", target.canonicalUrl])) }`, { timeoutMs: 6_000, cache: "no-store" }), }); pageLoader.current = loader; return () => { loader.dispose(); if (pageLoader.current === loader) pageLoader.current = null; }; }, [value, autoLoad, client, companyId]); return {children}; } export function useLinkPreviewContext() { return useContext(LinkPreviewContext); }