// TEMPORARY: remove after the brett migration (SDK-1909). // // Loads the legacy `/user-applications` endpoint alongside brett's // `/applications` and adapts the records into brett classes, so demo orgs stay // populated during the transition. Any failure degrades to empty, since this // data only ever adds entries to the list. import type { SanityClient } from "@sanity/client"; import { defineFetcher } from "@sanity/sdk/_internal"; import { catchError, forkJoin, map, of, switchMap } from "rxjs"; import { type CoreApp, type OrganizationId, type Studio } from "../../core"; import { adaptUserCoreApp, adaptUserStudio, } from "../../core/user-applications/adapter"; import { parseUserCoreApp, parseUserStudio, } from "../../core/user-applications/schema"; import { getClientState } from "./client"; export interface UserApplications { studios: Studio[]; coreApps: CoreApp[]; } const EMPTY: UserApplications = { studios: [], coreApps: [] }; export const userApplications = defineFetcher< [options: { organizationId: OrganizationId }], UserApplications >({ name: "workbench.userApplications", getKey: (_instance, { organizationId }) => organizationId, fetch: (instance) => ({ organizationId }) => getClientState(instance, { apiVersion: "vX", scope: "global", }).observable.pipe( switchMap((client: SanityClient) => { // Studios and core apps are separate `appType` requests on this endpoint. const request = (appType: "studio" | "coreApp") => client.withConfig({ maxRetries: 0 }).observable.request({ uri: "/user-applications", query: { appType, organizationId, includeManifest: "true" }, }); return forkJoin([request("studio"), request("coreApp")]).pipe( map(([studios, coreApps]) => ({ studios: studios.map((studio) => adaptUserStudio(parseUserStudio(studio), organizationId), ), coreApps: coreApps.map((coreApp) => adaptUserCoreApp(parseUserCoreApp(coreApp)), ), })), catchError(() => of(EMPTY)), ); }), ), });