@sanity/workbench 0.1.0-alpha.1 → 0.1.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/core.js ADDED
@@ -0,0 +1,779 @@
1
+ import { z } from "zod";
2
+ import { createLogger, logger } from "./_chunks-es/index.js";
3
+ import { valid, coerce, gt, lt, gte, rsort } from "semver";
4
+ class AbstractApplication {
5
+ type;
6
+ constructor(type) {
7
+ this.type = type;
8
+ }
9
+ /**
10
+ * Whether the application is served by a local CLI dev server rather than a
11
+ * deployed remote. Defaults to `false`; user applications flip this when
12
+ * constructed from a `LocalUserApplication` payload.
13
+ */
14
+ get isLocal() {
15
+ return !1;
16
+ }
17
+ get initials() {
18
+ const SYMBOLS = /[^\p{Alpha}\p{N}\p{White_Space}]/gu, WHITESPACE = new RegExp("\\p{White_Space}+", "u"), ALPHANUMERIC_SEGMENTS = new RegExp("(\\p{N}+|\\p{Alpha}+)", "gu"), IS_NUMERIC = new RegExp("^\\p{N}+$", "u");
19
+ if (!this.title) return "";
20
+ const namesArray = this.title.replace(SYMBOLS, "").split(WHITESPACE).filter(Boolean);
21
+ if (namesArray.length === 0) return "";
22
+ if (namesArray.length === 1) {
23
+ const word = namesArray[0], segments = word.match(ALPHANUMERIC_SEGMENTS) || [];
24
+ return segments.length === 0 ? "" : segments.length === 1 ? word.length === 1 ? word.toUpperCase() : IS_NUMERIC.test(word) ? `${word.charAt(0)}${word.charAt(1)}`.toUpperCase() : word.charAt(0).toUpperCase() : `${segments[0].charAt(0)}${segments[1].charAt(0)}`.toUpperCase();
25
+ }
26
+ return `${namesArray[0].charAt(0)}${namesArray[namesArray.length - 1].charAt(0)}`.toUpperCase();
27
+ }
28
+ }
29
+ class ApplicationList {
30
+ applications;
31
+ /**
32
+ * @param applications - The applications to initialize the list with.
33
+ */
34
+ constructor(applications) {
35
+ this.applications = [...applications];
36
+ }
37
+ findApplicationsByType(_type) {
38
+ const types = Array.isArray(_type) ? _type : [_type];
39
+ return this.applications.filter((r) => types.includes(r.type));
40
+ }
41
+ findApplication(type, id) {
42
+ switch (type) {
43
+ case "media-library":
44
+ case "canvas":
45
+ return this.applications.find((r) => r.type === type);
46
+ case "coreApp":
47
+ case "studio":
48
+ case "workspace":
49
+ return this.applications.find((r) => r.type === type && r.id === id);
50
+ }
51
+ }
52
+ toProtocolResources() {
53
+ return this.applications.reduce((acc, r) => r.type === "studio" || r.isLocal ? acc : [...acc, r.toProtocolResource()], []);
54
+ }
55
+ static areApplicationsEqual(application1, application2) {
56
+ return !application1 || !application2 || application1.type !== application2.type ? !1 : application1.id === application2.id;
57
+ }
58
+ }
59
+ const OrganizationId = z.string().nonempty().brand("OrganizationId");
60
+ function brandOrganizationId(id) {
61
+ return OrganizationId.parse(id);
62
+ }
63
+ const OrganizationMember = z.object({
64
+ sanityUserId: z.string(),
65
+ isCurrentUser: z.boolean(),
66
+ user: z.object({
67
+ id: z.string(),
68
+ displayName: z.string(),
69
+ familyName: z.string(),
70
+ givenName: z.string(),
71
+ middleName: z.string().nullable(),
72
+ imageUrl: z.string().nullable(),
73
+ email: z.string(),
74
+ loginProvider: z.string()
75
+ }),
76
+ roles: z.array(
77
+ z.object({
78
+ name: z.string(),
79
+ title: z.string(),
80
+ description: z.string().optional()
81
+ })
82
+ )
83
+ }), Organization = z.object({
84
+ id: OrganizationId,
85
+ name: z.string(),
86
+ slug: z.string().nullable(),
87
+ createdAt: z.string(),
88
+ updatedAt: z.string(),
89
+ dashboardStatus: z.enum(["enabled", "disabled"]),
90
+ aiFeaturesStatus: z.enum(["enabled", "disabled"]),
91
+ defaultRoleName: z.string()
92
+ });
93
+ function parseOrganization(data, options) {
94
+ const includeMembers = options?.includeMembers ?? !0, includeFeatures = options?.includeFeatures ?? !0, extensions = {
95
+ ...includeMembers && {
96
+ members: z.array(OrganizationMember)
97
+ },
98
+ ...includeFeatures && {
99
+ features: z.array(z.string())
100
+ }
101
+ };
102
+ return (Object.keys(extensions).length > 0 ? Organization.extend(extensions) : Organization).parse(data);
103
+ }
104
+ const CanvasId = z.string().nonempty().brand("CanvasId");
105
+ function brandCanvasId(id) {
106
+ return CanvasId.parse(id);
107
+ }
108
+ const Canvas = z.object({
109
+ id: CanvasId,
110
+ organizationId: OrganizationId,
111
+ status: z.enum(["active", "provisioning"])
112
+ });
113
+ function parseCanvas(data) {
114
+ return Canvas.parse(data);
115
+ }
116
+ class CanvasApplication extends AbstractApplication {
117
+ canvas;
118
+ constructor(canvas) {
119
+ super("canvas"), this.canvas = canvas;
120
+ }
121
+ get id() {
122
+ return this.canvas.id;
123
+ }
124
+ get href() {
125
+ return "canvas";
126
+ }
127
+ get title() {
128
+ return "Canvas";
129
+ }
130
+ get url() {
131
+ return new URL(
132
+ `https://canvas.${import.meta.env.VITE_SANITY_DOMAIN}/o/${this.canvas.organizationId}`
133
+ );
134
+ }
135
+ get isFederated() {
136
+ return !1;
137
+ }
138
+ toProtocolResource() {
139
+ return {
140
+ type: "canvas",
141
+ id: this.id
142
+ };
143
+ }
144
+ }
145
+ const MediaLibraryId = z.string().nonempty().brand("MediaLibraryId");
146
+ function brandMediaLibraryId(id) {
147
+ return MediaLibraryId.parse(id);
148
+ }
149
+ const MediaLibrary = z.object({
150
+ id: MediaLibraryId,
151
+ organizationId: OrganizationId,
152
+ status: z.enum(["active", "provisioning"]),
153
+ aclMode: z.enum(["private", "public"])
154
+ });
155
+ function parseMediaLibrary(data) {
156
+ return MediaLibrary.parse(data);
157
+ }
158
+ class MediaLibraryApplication extends AbstractApplication {
159
+ library;
160
+ constructor(library) {
161
+ super("media-library"), this.library = library;
162
+ }
163
+ get id() {
164
+ return this.library.id;
165
+ }
166
+ get href() {
167
+ return "media";
168
+ }
169
+ get title() {
170
+ return "Media Library";
171
+ }
172
+ get isFederated() {
173
+ return !1;
174
+ }
175
+ get url() {
176
+ return new URL(
177
+ `https://media.${import.meta.env.VITE_SANITY_DOMAIN}/${this.library.organizationId}`
178
+ );
179
+ }
180
+ toProtocolResource() {
181
+ return {
182
+ type: "media-library",
183
+ id: this.id
184
+ };
185
+ }
186
+ }
187
+ const ProjectId = z.string().nonempty().brand("ProjectId");
188
+ function brandProjectId(id) {
189
+ return ProjectId.parse(id);
190
+ }
191
+ const ProjectMember = z.object({
192
+ id: z.string(),
193
+ createdAt: z.string(),
194
+ updatedAt: z.string(),
195
+ isCurrentUser: z.boolean(),
196
+ isRobot: z.boolean(),
197
+ roles: z.array(
198
+ z.object({
199
+ name: z.string(),
200
+ title: z.string(),
201
+ description: z.string()
202
+ })
203
+ )
204
+ }), Project = z.object({
205
+ id: ProjectId,
206
+ displayName: z.string(),
207
+ studioHost: z.string().nullable(),
208
+ organizationId: OrganizationId,
209
+ metadata: z.object({
210
+ color: z.string().optional(),
211
+ externalStudioHost: z.string().optional(),
212
+ initialTemplate: z.string().optional(),
213
+ cliInitializedAt: z.string().optional(),
214
+ integration: z.literal(["manage", "cli"])
215
+ }),
216
+ isBlocked: z.boolean(),
217
+ isDisabled: z.boolean(),
218
+ isDisabledByUser: z.boolean(),
219
+ activityFeedEnabled: z.boolean(),
220
+ createdAt: z.string(),
221
+ updatedAt: z.string()
222
+ });
223
+ function parseProject(data, options) {
224
+ const includeMembers = options?.includeMembers ?? !0, includeFeatures = options?.includeFeatures ?? !0, extensions = {
225
+ ...includeMembers && { members: z.array(ProjectMember) },
226
+ ...includeFeatures && { features: z.array(z.string()) }
227
+ };
228
+ return (Object.keys(extensions).length > 0 ? Project.extend(extensions) : Project).parse(data);
229
+ }
230
+ const joinUrlPaths = (...paths) => {
231
+ let nextPath = null;
232
+ const safeJoinSegments = (segment1, segment2) => segment1 ? segment2 ? segment1.endsWith("/") && segment2.startsWith("/") ? segment1 + segment2.slice(1) : segment1.endsWith("/") || segment2.startsWith("/") ? segment1 + segment2 : `${segment1}/${segment2}` : segment1 : segment2 || "", validPaths = paths.filter(
233
+ (path) => path != null
234
+ );
235
+ for (const path of validPaths)
236
+ nextPath = safeJoinSegments(
237
+ nextPath,
238
+ path instanceof URL ? path.pathname : path
239
+ );
240
+ return nextPath ?? "";
241
+ };
242
+ function normalizePath(pathname) {
243
+ return pathname.startsWith("/") || (pathname = `/${pathname}`), pathname !== "/" && pathname.endsWith("/") && (pathname = pathname.slice(0, -1)), pathname;
244
+ }
245
+ const UserApplicationId = z.string().nonempty().brand("UserApplicationId");
246
+ function brandUserApplicationId(id) {
247
+ return UserApplicationId.parse(id);
248
+ }
249
+ const LocalUserApplicationBase = z.object({
250
+ host: z.string(),
251
+ port: z.number(),
252
+ /** The `deployment.appId` from the application's `sanity.cli.ts`, when set. */
253
+ id: z.string().optional()
254
+ }), LocalUserApplication = z.discriminatedUnion("type", [
255
+ LocalUserApplicationBase.extend({
256
+ type: z.literal("studio"),
257
+ manifest: z.custom().optional()
258
+ }),
259
+ LocalUserApplicationBase.extend({
260
+ type: z.literal("coreApp"),
261
+ manifest: z.custom().optional()
262
+ })
263
+ ]), UserApplicationBase = z.object({
264
+ id: UserApplicationId,
265
+ appHost: z.string(),
266
+ urlType: z.enum(["internal", "external"]),
267
+ createdAt: z.string(),
268
+ updatedAt: z.string(),
269
+ dashboardStatus: z.enum(["default", "disabled"])
270
+ }), ActiveDeployment = z.object({
271
+ id: z.string(),
272
+ version: z.string(),
273
+ isActiveDeployment: z.boolean(),
274
+ userApplicationId: z.string(),
275
+ isAutoUpdating: z.boolean(),
276
+ size: z.number(),
277
+ deployedAt: z.string(),
278
+ deployedBy: z.string(),
279
+ createdAt: z.string(),
280
+ updatedAt: z.string()
281
+ });
282
+ class UserApplication extends AbstractApplication {
283
+ application;
284
+ id;
285
+ /**
286
+ * For local applications (`isLocal === true`), the deployed application
287
+ * that shares the same `id` — if one was passed at construction. `null`
288
+ * for deployed applications or when no remote twin was provided.
289
+ */
290
+ remoteApplication;
291
+ #isLocal;
292
+ constructor(application, type, options = {}) {
293
+ super(type), this.application = application, this.id = brandUserApplicationId(application.id), this.#isLocal = options.isLocal ?? !1, this.remoteApplication = options.remoteApplication ?? null;
294
+ }
295
+ /**
296
+ * Local dev servers are rendered as federated remotes; deployed applications
297
+ * are rendered in an iframe.
298
+ */
299
+ get isFederated() {
300
+ return this.isLocal;
301
+ }
302
+ get isLocal() {
303
+ return this.#isLocal;
304
+ }
305
+ /**
306
+ * @returns A fully resolved URL instance for the application.
307
+ * For internal applications, constructs a URL using the Sanity studio domain
308
+ * pattern resolved from the environment at the consuming app's build time.
309
+ * For external applications (including local dev servers), returns the
310
+ * provided app host as a URL.
311
+ */
312
+ get url() {
313
+ return this.application.urlType === "internal" ? import.meta.env.VITE_SANITY_ENV === "production" ? new URL(`https://${this.application.appHost}.sanity.studio`) : new URL(
314
+ `https://${this.application.appHost}.studio.${import.meta.env.VITE_SANITY_DOMAIN}`
315
+ ) : new URL(this.application.appHost);
316
+ }
317
+ }
318
+ const CoreAppUserApplicationManifest = z.object({
319
+ version: z.string(),
320
+ icon: z.string().optional(),
321
+ title: z.string().optional()
322
+ }), CoreAppUserApplicationBase = UserApplicationBase.extend({
323
+ title: z.string(),
324
+ organizationId: OrganizationId,
325
+ type: z.literal("coreApp"),
326
+ manifest: CoreAppUserApplicationManifest.nullable().optional()
327
+ }), InternalCoreAppUserApplication = CoreAppUserApplicationBase.extend({
328
+ urlType: z.literal("internal"),
329
+ activeDeployment: ActiveDeployment.extend({
330
+ manifest: CoreAppUserApplicationManifest.nullable().optional()
331
+ })
332
+ }), ExternalCoreAppUserApplication = CoreAppUserApplicationBase.extend({
333
+ urlType: z.literal("external"),
334
+ activeDeployment: z.null()
335
+ }), CoreAppUserApplication = z.discriminatedUnion("urlType", [
336
+ InternalCoreAppUserApplication,
337
+ ExternalCoreAppUserApplication
338
+ ]);
339
+ function parseCoreApplication(data) {
340
+ return CoreAppUserApplication.parse(data);
341
+ }
342
+ class CoreAppApplication extends UserApplication {
343
+ activeDeployment;
344
+ constructor(application, options = {}) {
345
+ super(application, "coreApp", options), this.activeDeployment = application.activeDeployment;
346
+ }
347
+ get href() {
348
+ return this.isLocal ? `/local/${this.id}` : `/application/${this.application.id}`;
349
+ }
350
+ get title() {
351
+ return this.activeDeployment?.manifest?.title ?? this.application.title;
352
+ }
353
+ get subtitle() {
354
+ }
355
+ get updatedAt() {
356
+ return this.application.updatedAt;
357
+ }
358
+ get(attr) {
359
+ if (!(attr in this.application))
360
+ throw new Error(
361
+ `Attribute ${attr.toString()} does not exist on application ${this.application.id}`
362
+ );
363
+ return this.application[attr];
364
+ }
365
+ toProtocolResource() {
366
+ return {
367
+ ...this.application,
368
+ type: "application",
369
+ url: this.url.toString()
370
+ };
371
+ }
372
+ }
373
+ const Workspace = z.object({
374
+ name: z.string(),
375
+ title: z.string(),
376
+ subtitle: z.string().optional(),
377
+ basePath: z.string(),
378
+ projectId: ProjectId,
379
+ dataset: z.string().optional(),
380
+ icon: z.string().nullable().optional()
381
+ }), ServerManifest = z.object({
382
+ buildId: z.string().optional(),
383
+ bundleVersion: z.string().optional(),
384
+ version: z.string().optional(),
385
+ workspaces: z.array(
386
+ Workspace.extend({
387
+ dataset: z.string(),
388
+ schemaDescriptorId: z.string()
389
+ })
390
+ ).optional()
391
+ }), ClientManifest$1 = z.object({
392
+ version: z.number(),
393
+ createdAt: z.string(),
394
+ studioVersion: z.string().optional(),
395
+ workspaces: z.array(
396
+ Workspace.extend({
397
+ schema: z.string(),
398
+ tools: z.string().optional()
399
+ })
400
+ )
401
+ }), StudioUserApplicationBase = UserApplicationBase.extend({
402
+ title: z.string().nullable(),
403
+ projectId: ProjectId,
404
+ type: z.literal("studio"),
405
+ manifest: ClientManifest$1.nullable(),
406
+ manifestData: z.object({ value: ClientManifest$1 }).nullable(),
407
+ autoUpdatingVersion: z.string().nullable(),
408
+ config: z.object({
409
+ "live-manifest": z.object({
410
+ createdAt: z.string(),
411
+ updatedAt: z.string(),
412
+ updatedBy: z.string(),
413
+ value: ServerManifest
414
+ }).optional()
415
+ })
416
+ }), InternalStudioUserApplication = StudioUserApplicationBase.extend({
417
+ urlType: z.literal("internal"),
418
+ activeDeployment: ActiveDeployment.extend({
419
+ manifest: ServerManifest.nullable()
420
+ })
421
+ }), ExternalStudioUserApplication = StudioUserApplicationBase.extend({
422
+ urlType: z.literal("external"),
423
+ activeDeployment: z.null()
424
+ }), StudioUserApplication = z.discriminatedUnion("urlType", [
425
+ InternalStudioUserApplication,
426
+ ExternalStudioUserApplication
427
+ ]);
428
+ function parseStudioUserApplication(data) {
429
+ return StudioUserApplication.parse(data);
430
+ }
431
+ class StudioWorkspace extends AbstractApplication {
432
+ /**
433
+ * Workspaces always belong to a studio application.
434
+ * They do not exist on their own & therefore can access
435
+ * information about the studio they're in via this property.
436
+ */
437
+ studioApplication;
438
+ workspace;
439
+ project;
440
+ isDefaultWorkspace;
441
+ constructor(studioApplication, workspace, project, isDefaultWorkspace) {
442
+ super("workspace"), this.studioApplication = studioApplication, this.workspace = workspace, this.project = project, this.isDefaultWorkspace = isDefaultWorkspace;
443
+ }
444
+ /**
445
+ * The studio application that this workspace belongs to.
446
+ */
447
+ get studio() {
448
+ return this.studioApplication;
449
+ }
450
+ get id() {
451
+ return StudioWorkspace.makeId(this.studio.id, this.workspace.name);
452
+ }
453
+ get href() {
454
+ return joinUrlPaths(this.studio.href, this.workspace.name);
455
+ }
456
+ get title() {
457
+ return this.isDefaultWorkspace ? this.project.displayName : this.workspace.title;
458
+ }
459
+ get subtitle() {
460
+ return this.isDefaultWorkspace ? (URL.canParse(this.studio.get("appHost")) ? new URL(this.studio.get("appHost")) : this.studio.url).hostname : this.get("subtitle");
461
+ }
462
+ get(attr) {
463
+ return this.workspace[attr];
464
+ }
465
+ get isFederated() {
466
+ return this.studio.isFederated;
467
+ }
468
+ /**
469
+ * With comlink, studio-applications were not considered applications at all, only workspaces. This is partially why
470
+ * we create default workspaces when the application has no manifest or the manifest has no workspaces.
471
+ *
472
+ * Thereby, to create it we depend on a lot of information from the parent application even if it's duplicated across
473
+ * different workspaces.
474
+ */
475
+ toProtocolResource() {
476
+ return {
477
+ ...this.workspace,
478
+ type: "studio",
479
+ userApplicationId: this.studio.id,
480
+ activeDeployment: this.studio.get("activeDeployment"),
481
+ autoUpdatingVersion: this.studio.get("autoUpdatingVersion"),
482
+ dashboardStatus: this.studio.get("dashboardStatus"),
483
+ url: this.studio.url.toString(),
484
+ href: this.href,
485
+ id: this.id,
486
+ hasManifest: !0,
487
+ hasSchema: !!("schema" in this.workspace && this.workspace.schema),
488
+ manifest: this.studio.get("manifestData")?.value ?? null,
489
+ updatedAt: this.studio.get("updatedAt"),
490
+ version: this.studio.get("activeDeployment")?.version,
491
+ urlType: this.studio.get("urlType"),
492
+ config: this.studio.get("config")
493
+ };
494
+ }
495
+ /**
496
+ * @returns the URL to the workspace of a studio application.
497
+ */
498
+ get url() {
499
+ const studioUrl = new URL(this.studio.url), normalizedUrlPath = normalizePath(studioUrl.pathname);
500
+ let finalBasePath = normalizePath(this.get("basePath"));
501
+ return finalBasePath.startsWith(normalizedUrlPath) && (finalBasePath = finalBasePath.slice(normalizedUrlPath.length)), studioUrl.pathname = joinUrlPaths(normalizedUrlPath, finalBasePath), studioUrl;
502
+ }
503
+ static makeId(applicationId, workspaceName) {
504
+ return `${applicationId}-${workspaceName}`;
505
+ }
506
+ static splitId(id) {
507
+ return id.split(new RegExp(/-(.*)/)).slice(0, 2);
508
+ }
509
+ }
510
+ const ClientManifest = ClientManifest$1.superRefine((data, ctx) => {
511
+ data.version || ctx.addIssue({
512
+ code: "invalid_type",
513
+ message: "Manifest version is too old",
514
+ expected: "number"
515
+ }), data.version < 2 && ctx.addIssue({
516
+ code: "invalid_value",
517
+ message: "Manifest version is too old",
518
+ values: [2, 3]
519
+ }), data.version >= 3 && !data.studioVersion && ctx.addIssue({
520
+ code: "invalid_type",
521
+ message: "Manifest version 3 or higher requires a `studioVersion`",
522
+ expected: "string"
523
+ });
524
+ }), DEFAULT_WORKSPACE_DATA = {
525
+ name: "default",
526
+ title: "Default",
527
+ basePath: "/"
528
+ };
529
+ class StudioApplication extends UserApplication {
530
+ /**
531
+ * Returns a list of studio workspaces based on the application manifest.
532
+ * If there is no manifest, or alternatively it is not valid, then we create a default workspace.
533
+ */
534
+ workspaces = [];
535
+ project;
536
+ /**
537
+ * The projects actually referenced by this studio — the application's own
538
+ * project plus any project used by a workspace. Preserved so the instance
539
+ * can be re-constructed (e.g. by dock wrappers) without having to thread
540
+ * the full organization project list through again.
541
+ */
542
+ projects;
543
+ /**
544
+ * @param application - The studio application to create a list of workspaces for
545
+ * @param projects - The projects available in the organization. It's not enough to just pass
546
+ * the project that associates with the application because that is the project the app is deployed in relation to.
547
+ * The workspaces may have different projects completely.
548
+ */
549
+ constructor(application, projects, options = {}) {
550
+ super(application, "studio", options);
551
+ let workspaces = [];
552
+ if (this.hasManifest)
553
+ try {
554
+ workspaces = ClientManifest.parse(
555
+ application.manifestData?.value
556
+ ).workspaces;
557
+ } catch (error) {
558
+ console.warn(
559
+ `Failed to parse manifest for application ${application.id}`,
560
+ error
561
+ );
562
+ }
563
+ if (workspaces.length === 0) {
564
+ const serverManifestWorkspaces = application.activeDeployment?.manifest?.workspaces;
565
+ Array.isArray(serverManifestWorkspaces) && serverManifestWorkspaces.length && (workspaces = serverManifestWorkspaces);
566
+ }
567
+ const workspacesWithProjectsMap = workspaces.reduce((acc, workspace) => {
568
+ const project2 = projects.find((p) => p.id === workspace.projectId);
569
+ return project2 ? acc.set(workspace, project2) : console.warn(
570
+ `Project not found for application ${application.id} and workspace ${workspace.name}. This workspace has been omitted.`
571
+ ), acc;
572
+ }, /* @__PURE__ */ new Map()), project = projects.find((p) => p.id === application.projectId);
573
+ if (!project)
574
+ throw new Error(`Project not found for application ${application.id}`);
575
+ workspacesWithProjectsMap.size === 0 && workspacesWithProjectsMap.set(
576
+ {
577
+ ...DEFAULT_WORKSPACE_DATA,
578
+ projectId: application.projectId
579
+ },
580
+ project
581
+ ), this.workspaces = Object.freeze(
582
+ Array.from(workspacesWithProjectsMap.entries()).map(([workspace, p]) => {
583
+ const isDefaultWorkspace = workspace.name === DEFAULT_WORKSPACE_DATA.name && workspace.basePath === DEFAULT_WORKSPACE_DATA.basePath && workspace.title === DEFAULT_WORKSPACE_DATA.title;
584
+ return new StudioWorkspace(this, workspace, p, isDefaultWorkspace);
585
+ })
586
+ ), this.project = project;
587
+ const usedProjects = /* @__PURE__ */ new Set([project]);
588
+ for (const workspace of this.workspaces)
589
+ usedProjects.add(workspace.project);
590
+ this.projects = Array.from(usedProjects);
591
+ }
592
+ get href() {
593
+ return this.isLocal ? `/local/${this.id}` : `/studio/${this.application.id}`;
594
+ }
595
+ get title() {
596
+ return this.get("title") || (this.workspaces.length > 1 && this.get("urlType") === "internal" ? this.project.displayName : this.workspaces[0].title);
597
+ }
598
+ get subtitle() {
599
+ return new URL(this.url).hostname;
600
+ }
601
+ get(attr) {
602
+ if (!(attr in this.application))
603
+ throw new Error(
604
+ `Attribute ${attr.toString()} does not exist on studio ${this.application.id}`
605
+ );
606
+ return this.application[attr];
607
+ }
608
+ get hasManifest() {
609
+ return typeof this.application.manifestData?.value?.version == "number" && this.application.manifestData?.value?.version >= 2;
610
+ }
611
+ get hasSchema() {
612
+ const workspaces = this.get("manifestData")?.value?.workspaces ?? [];
613
+ return workspaces.length === 0 ? !1 : workspaces.every((w) => !!w.schema);
614
+ }
615
+ resolveVersion() {
616
+ let version;
617
+ if (this.get("urlType") === "internal")
618
+ version = this.get("activeDeployment")?.version;
619
+ else {
620
+ const manifest = this.get("manifestData")?.value;
621
+ version = manifest && "studioVersion" in manifest ? manifest.studioVersion : void 0;
622
+ }
623
+ const bundleVersion = this.get("activeDeployment")?.manifest?.bundleVersion;
624
+ return bundleVersion && valid(coerce(bundleVersion)) && (!version || version && gt(bundleVersion, version)) && (version = bundleVersion), !version || !valid(coerce(version)) ? null : coerce(version);
625
+ }
626
+ get version() {
627
+ const version = this.resolveVersion();
628
+ return version ? version.toString() : null;
629
+ }
630
+ get compatibilityStatus() {
631
+ return StudioApplication.resolveCompatibilityStatus(this);
632
+ }
633
+ /**
634
+ * Used to calculate the compatibility status of a given studio application.
635
+ * Optionally if you've resolved the version elsewhere provide that value to
636
+ * get the new compatibility status without mutating the application.
637
+ */
638
+ static resolveCompatibilityStatus(application, version = application.version) {
639
+ return version === null || lt(version, StudioApplication.MinimumStudioVersion) ? StudioApplication.CompatibilityStatuses.UNKNOWN : !application.hasSchema || !application.hasManifest || StudioApplication.resolveIssues(application, version).length > 0 ? StudioApplication.CompatibilityStatuses.PARTIALLY_COMPATIBLE : StudioApplication.CompatibilityStatuses.FULLY_COMPATIBLE;
640
+ }
641
+ get isAutoRedirecting() {
642
+ return StudioApplication.resolveIsAutoRedirecting(this);
643
+ }
644
+ /**
645
+ * Mirrors the `isRedirectable` function from Saison defined in
646
+ * https://github.com/sanity-io/saison/blob/83556405d23e07f6d3a71c76249c67e33fe1101f/src/utils/applications.ts
647
+ *
648
+ * Returns whether a studio application is auto-redirecting, meaning it can only be accessed in the context of
649
+ * Dashboard.
650
+ */
651
+ static resolveIsAutoRedirecting(application, versionArg = application.version) {
652
+ let version = versionArg;
653
+ if (application.get("urlType") === "external" || !version)
654
+ return !1;
655
+ if (!application.get("activeDeployment")?.isAutoUpdating)
656
+ return gt(version, "3.92.0");
657
+ const autoUpdatingVersion = application.get("autoUpdatingVersion");
658
+ if (autoUpdatingVersion) {
659
+ if (["next", "stable", "latest"].includes(autoUpdatingVersion))
660
+ return !0;
661
+ const autoUpdatingVersionPinnedVersion = coerce(autoUpdatingVersion);
662
+ autoUpdatingVersionPinnedVersion && (version = autoUpdatingVersionPinnedVersion);
663
+ }
664
+ return gt(version, StudioApplication.MinimumStudioVersion);
665
+ }
666
+ /**
667
+ * Returns a list of issues that prevent the studio from functioning properly in the Dashboard.
668
+ * This static value depends on the version of the studio that comes from the `activeDeployment` property.
669
+ * As such, if the studio is auto-updating, this list will be incorrect & instead you should use
670
+ * the static method `resolveIssues` to get the correct issues by passing the resolved version.
671
+ */
672
+ get issues() {
673
+ return StudioApplication.resolveIssues(this);
674
+ }
675
+ static resolveIssues(application, version = application.version) {
676
+ const issues = StudioApplication.Features.filter(
677
+ (feature) => !application.isFeatureSupported(feature.id, version)
678
+ );
679
+ return application.hasManifest || issues.push({
680
+ id: StudioApplication.StudioIssues.ISSUE_MANIFEST
681
+ }), issues;
682
+ }
683
+ isFeatureSupported(feature, version = this.version) {
684
+ const featureVersion = StudioApplication.Features.find(
685
+ (_) => _.id === feature
686
+ )?.version;
687
+ return !featureVersion || !version ? !1 : gte(version, featureVersion);
688
+ }
689
+ toProtocolResource() {
690
+ throw new Error(
691
+ "Studio application resources cannot be converted to protocol resources"
692
+ );
693
+ }
694
+ static CompatibilityStatuses = {
695
+ UNKNOWN: "unknown",
696
+ PARTIALLY_COMPATIBLE: "partially-compatible",
697
+ FULLY_COMPATIBLE: "fully-compatible"
698
+ };
699
+ static StudioIssues = {
700
+ ISSUE_ACTIVITY: "ACTIVITY",
701
+ ISSUE_AGENT: "AGENT",
702
+ ISSUE_FAVORITES: "FAVORITES",
703
+ ISSUE_URL_SYNCING: "URL_SYNCING",
704
+ ISSUE_UI_ADJUSTMENT: "UI_ADJUSTMENT",
705
+ ISSUE_CONTENT_MAPPING: "CONTENT_MAPPING",
706
+ ISSUE_LOGIN: "LOGIN",
707
+ ISSUE_MANIFEST: "MANIFEST"
708
+ };
709
+ static Features = [
710
+ {
711
+ id: StudioApplication.StudioIssues.ISSUE_AGENT,
712
+ version: "5.1.0"
713
+ },
714
+ {
715
+ id: StudioApplication.StudioIssues.ISSUE_FAVORITES,
716
+ version: "3.88.1"
717
+ },
718
+ {
719
+ id: StudioApplication.StudioIssues.ISSUE_ACTIVITY,
720
+ version: "3.88.1"
721
+ },
722
+ {
723
+ id: StudioApplication.StudioIssues.ISSUE_URL_SYNCING,
724
+ version: "3.75.0"
725
+ },
726
+ {
727
+ id: StudioApplication.StudioIssues.ISSUE_UI_ADJUSTMENT,
728
+ version: "3.78.1"
729
+ },
730
+ {
731
+ id: StudioApplication.StudioIssues.ISSUE_CONTENT_MAPPING,
732
+ version: "3.68.0"
733
+ },
734
+ {
735
+ id: StudioApplication.StudioIssues.ISSUE_LOGIN,
736
+ version: "2.28.0"
737
+ }
738
+ ];
739
+ static MinimumStudioVersion = "2.28.0";
740
+ static MinimumStudioVersionWithNoIssues = rsort(
741
+ StudioApplication.Features.map((feature) => feature.version)
742
+ ).at(0);
743
+ }
744
+ export {
745
+ AbstractApplication,
746
+ ActiveDeployment,
747
+ ApplicationList,
748
+ CanvasApplication,
749
+ ClientManifest$1 as ClientManifest,
750
+ CoreAppApplication,
751
+ CoreAppUserApplicationManifest,
752
+ LocalUserApplication,
753
+ MediaLibraryApplication,
754
+ Organization,
755
+ OrganizationId,
756
+ Project,
757
+ ProjectId,
758
+ StudioApplication,
759
+ StudioUserApplication,
760
+ StudioWorkspace,
761
+ UserApplication,
762
+ UserApplicationBase,
763
+ UserApplicationId,
764
+ brandCanvasId,
765
+ brandMediaLibraryId,
766
+ brandOrganizationId,
767
+ brandProjectId,
768
+ brandUserApplicationId,
769
+ createLogger,
770
+ joinUrlPaths,
771
+ logger,
772
+ parseCanvas,
773
+ parseCoreApplication,
774
+ parseMediaLibrary,
775
+ parseOrganization,
776
+ parseProject,
777
+ parseStudioUserApplication
778
+ };
779
+ //# sourceMappingURL=core.js.map