{"version":3,"file":"use-optional-store-CIM4spDu.cjs","names":[],"sources":["../../core/src/store-api-context.ts","../../react/src/hooks/use-optional-store.ts"],"sourcesContent":["import { createContext, type Provider, use } from \"react\";\nimport type { StoreApi } from \"./store-api\";\n\nconst StoreApiContext = createContext<StoreApi | null>(null);\n\nexport const StoreApiProvider: Provider<StoreApi | null> =\n  StoreApiContext.Provider;\n\nexport function useStoreApi(): StoreApi {\n  const api = use(StoreApiContext);\n  if (!api) {\n    throw new Error(\"useStoreApi must be used within a StoreApiProvider\");\n  }\n  return api;\n}\n\n/**\n * Non-throwing variant for contexts where a StoreApiProvider may be absent\n * (e.g. the screen-builder widget palette/drag-preview, which mounts only the\n * widget preview providers). Returns null instead of throwing so callers can\n * fall back to default copy.\n */\nexport function useOptionalStoreApi(): StoreApi | null {\n  return use(StoreApiContext);\n}\n","import { useQuery, type UseQueryResult } from \"@tanstack/react-query\";\nimport { useOptionalStoreApi } from \"@fluid-app/portal-core/store-api-context\";\nimport type { StoreApi } from \"@fluid-app/portal-core/store-api\";\nimport type { Store } from \"@fluid-app/portal-core/store-types\";\n\n/** Shared cache key + staleTime for the tenant store query. */\nexport const STORE_QUERY_KEY = [\"portalTenant\", \"store\"] as const;\nexport const STORE_STALE_TIME = 5 * 60 * 1000;\n\n/**\n * Builds the tenant-store query fn. Shared by `useStore()` (sdk) and\n * `useOptionalStore()` so the fetch + response mapping lives in one place.\n */\nexport function fetchStoreQuery(api: StoreApi): () => Promise<Store> {\n  return async () => {\n    const response = await api.fetchStore();\n    return response.store;\n  };\n}\n\n/**\n * Tenant-store query that tolerates a missing `StoreApiProvider` (e.g. the\n * widget palette / drag-preview, which mounts widgets without one). Returns a\n * disabled query instead of throwing, and shares the cache key + staleTime with\n * `useStore()` so both resolve from the same cache entry.\n */\nexport function useOptionalStore(): UseQueryResult<Store> {\n  const api = useOptionalStoreApi();\n  return useQuery({\n    queryKey: STORE_QUERY_KEY,\n    // Only runs when enabled (api non-null), so the assertion is safe.\n    queryFn: fetchStoreQuery(api as StoreApi),\n    enabled: api !== null,\n    staleTime: STORE_STALE_TIME,\n  });\n}\n"],"mappings":";;;;AAGA,MAAM,mBAAA,GAAA,MAAA,eAAiD,KAAK;AAE5D,MAAa,mBACX,gBAAgB;AAElB,SAAgB,cAAwB;CACtC,MAAM,OAAA,GAAA,MAAA,KAAU,gBAAgB;AAChC,KAAI,CAAC,IACH,OAAM,IAAI,MAAM,qDAAqD;AAEvE,QAAO;;;;;;;;AAST,SAAgB,sBAAuC;AACrD,SAAA,GAAA,MAAA,KAAW,gBAAgB;;;;;ACjB7B,MAAa,kBAAkB,CAAC,gBAAgB,QAAQ;AACxD,MAAa,mBAAmB,MAAS;;;;;AAMzC,SAAgB,gBAAgB,KAAqC;AACnE,QAAO,YAAY;AAEjB,UADiB,MAAM,IAAI,YAAY,EACvB;;;;;;;;;AAUpB,SAAgB,mBAA0C;CACxD,MAAM,MAAM,qBAAqB;AACjC,SAAA,GAAA,sBAAA,UAAgB;EACd,UAAU;EAEV,SAAS,gBAAgB,IAAgB;EACzC,SAAS,QAAQ;EACjB,WAAW;EACZ,CAAC"}