import type { IconDescriptor } from "../applications/applications"; import type { ApplicationId } from "../applications/brett-applications"; import { getApplicationDomain } from "../env"; import type { OrganizationId } from "../organizations"; import type { RemoteModule } from "../remote-module"; import { APP_TYPE_NAME, getDeployedConfigModuleRef } from "./config"; import { type Installation, InstalledApplication } from "./installations"; /** * The deployed media-library config, as a module ref. A singleton application * has at most one active config, so the first match wins. * @public */ export function findDeployedConfigRef( installations: Installation[], ): RemoteModule | null { for (const installation of installations) { if ( installation.application.name === APP_TYPE_NAME["media-library"] && // The config host is still keyed on the slug — that's the address, not // identity — so a slug-less install has no deployed config to serve. installation.application.slug !== null && installation.access.some( (row) => row.resourceType === "media-libraries", ) && installation.activeConfig ) { return getDeployedConfigModuleRef( installation.application.slug, installation.activeConfig, installation.organizationId, ); } } return null; } /** * @public */ export class MediaLibraryApplication extends InstalledApplication<"media-library"> { constructor(id: ApplicationId, organizationId: OrganizationId) { // The media library is the Sanity-owned `media-library` singleton, so its // identity is fixed — like the slug above. super({ type: "media-library", slug: "media-library", name: "media-library", reference: "sanity/media-library", id, organizationId, }); } get href(): string { return "media"; } get title(): string { return "Media Library"; } get url(): URL { return new URL( `https://${this.slug}-apps-${this.organizationId}.${getApplicationDomain()}`, ); } get icon(): IconDescriptor { return { variant: "avatar", initials: this.initials, color: this.avatarColor, }; } }