/** * Data Application resolution for the Table grid: the addressed Application read, its template * surface and the paged read a Catalog or Routing App supplies. CustomerGrid reads one value. */ import { useCallback, useEffect, useRef, useState } from "react"; import { API_V1, CREDENTIALS } from "../apiContract"; import { readDataPath } from "./dataPath"; import type { DataPath } from "./dataPath"; import type { GridHostCapabilities } from "./seam"; import type { ApplicationType, Field, Row } from "./types"; import { applicationTemplateSurface } from "./viewModes"; export type SavedApplication = { id: string; type: ApplicationType; name: string; revision?: number; config?: { schemaVersion?: number; display?: Record }; starred?: boolean; semanticRoleBindings?: Record; capabilities?: Record; compatibleDatabases?: string[]; openTarget?: { database?: string }; templateKind?: unknown; fieldMap?: unknown; }; export interface ApplicationHostInput { /** A foreground grid: neither embedded nor a Query preview. */ foreground: boolean; dataPath: DataPath | undefined; /** The database the registry is asked about. */ applicationDatabase: string; /** The View's referenced Application, and whether it matches the View's reference. */ resolvedApplication: SavedApplication | null; resolvedApplicationMatches: boolean; /** The View's Application reference key, "" without one. */ applicationRefKey: string; } export interface ApplicationHost { /** `::` on an addressed surface, "" otherwise. */ key: string; /** The key whose registry read settled. */ settledKey: string; /** The addressed Application. Null before the read settles, or when missing or incompatible. */ definition: SavedApplication | null; catalog: boolean; routing: boolean; /** A Catalog or Routing App: its rows come from its own paged read. */ paged: boolean; /** An App with its own screen. It reads its own routes, never the source rows. */ templateView: boolean; /** The ordinary row reader may run. */ rowsEnabled: boolean; /** The paged App's current read. Null until it lands for this App. */ pagedRead: { key: string; fields: Field[]; rows: Row[] } | null; acceptPagedRead: (fields: Field[], rows: Row[]) => void; /** Replace the addressed definition with a newer copy of the same Application. */ adopt: (application: SavedApplication) => void; /** What the grid gives up to this host. An addressed Application takes all three. */ capabilities: GridHostCapabilities; } export function useApplicationHost({ foreground, dataPath, applicationDatabase, resolvedApplication, resolvedApplicationMatches, applicationRefKey, }: ApplicationHostInput): ApplicationHost { // An addressed Application surface: `dataPath.application` on a foreground grid. const isApplicationSurface = foreground && !!dataPath?.application; const addressedApplicationKey = isApplicationSurface && dataPath?.database ? `${dataPath.database}::${dataPath.application}` : ""; const [addressedApplication, setAddressedApplication] = useState<{ key: string; application: SavedApplication | null; }>({ key: "", application: null }); const applicationOpenSeq = useRef(0); useEffect(() => { if (!addressedApplicationKey || !dataPath?.database) return; const mine = ++applicationOpenSeq.current; const controller = new AbortController(); const requestedDatabase = dataPath.database; const requestedId = dataPath.application; void fetch(`${API_V1}/applications?database=${encodeURIComponent(applicationDatabase)}`, { credentials: CREDENTIALS, signal: controller.signal, }).then(async (response) => response.ok ? response.json() : null).then((body) => { if (controller.signal.aborted || mine !== applicationOpenSeq.current) return; const livePath = readDataPath(); if (livePath?.database !== requestedDatabase || livePath.application !== requestedId) return; const listed: SavedApplication[] = Array.isArray(body?.applications) ? body.applications : []; const application = listed.find((candidate) => candidate.id === requestedId) ?? null; const compatible = application?.openTarget?.database === requestedDatabase || application?.compatibleDatabases?.includes(requestedDatabase) === true; setAddressedApplication({ key: addressedApplicationKey, application: compatible ? application : null }); }).catch(() => { if (controller.signal.aborted || mine !== applicationOpenSeq.current) return; const livePath = readDataPath(); if (livePath?.database !== requestedDatabase || livePath.application !== requestedId) return; setAddressedApplication({ key: addressedApplicationKey, application: null }); }); return () => controller.abort(); }, [addressedApplicationKey, applicationDatabase, dataPath?.database, dataPath?.application]); const addressedDefinition = addressedApplication.key === addressedApplicationKey ? addressedApplication.application : null; const isCatalogApplication = applicationTemplateSurface(addressedDefinition) === "catalog" || (resolvedApplicationMatches && applicationTemplateSurface(resolvedApplication) === "catalog"); const isRoutingApplication = applicationTemplateSurface(addressedDefinition) === "route" || (resolvedApplicationMatches && applicationTemplateSurface(resolvedApplication) === "route"); const isPagedOdooApplication = isCatalogApplication || isRoutingApplication; // C2: an App with its own screen (Competitor watch and every C1 view) reads its own routes, // never the source database rows. const isTemplateViewApplication = applicationTemplateSurface(addressedDefinition) === "app"; // Resolve the registry before allowing an App to use an ordinary row reader. const legacyRowsEnabled = !isPagedOdooApplication && (!isApplicationSurface || !!addressedDefinition) && !isTemplateViewApplication; const catalogReadKey = addressedApplicationKey || applicationRefKey; const [catalogRead, setCatalogRead] = useState<{ key: string; fields: Field[]; rows: Row[] }>( { key: "", fields: [], rows: [] }); const catalogData = isPagedOdooApplication && catalogRead.key === catalogReadKey ? catalogRead : null; const acceptPagedRead = useCallback((fields: Field[], rows: Row[]) => { setCatalogRead({ key: catalogReadKey, fields, rows }); }, [catalogReadKey]); const adopt = useCallback((application: SavedApplication) => { setAddressedApplication((prev) => prev.key && prev.application?.id === application.id ? { key: prev.key, application } : prev); }, []); return { key: addressedApplicationKey, settledKey: addressedApplication.key, definition: addressedDefinition, catalog: isCatalogApplication, routing: isRoutingApplication, paged: isPagedOdooApplication, templateView: isTemplateViewApplication, rowsEnabled: legacyRowsEnabled, pagedRead: catalogData, acceptPagedRead, adopt, capabilities: { ownsLayout: isApplicationSurface, hidesViews: isApplicationSurface, ownsDisplay: isApplicationSurface }, }; }