Skip to content

Commit 56c7b47

Browse files
authored
[Website] Isolate resolveBlueprintFromURL() calls (#2654)
## Motivation for the change, related issues Moves all the `resolveBlueprintFromURL()` into a single state function to set the stage for Blueprints v2. Version 2 will handle merging Query API overrides with the Blueprint using PHP logic, not TypeScript logic. This PR is the first step there – it moves all the TypeScript parsing and merging logic into a single place to make the upcoming refactoring easier. A part of #2586. ## Testing Instructions (or ideally a Blueprint) CI
1 parent ed61fe4 commit 56c7b47

File tree

9 files changed

+221
-240
lines changed

9 files changed

+221
-240
lines changed

packages/playground/website/src/components/ensure-playground-site/ensure-playground-site-is-selected.tsx

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { useEffect, useState } from 'react';
2-
import type { ResolvedBlueprint } from '../../lib/state/url/resolve-blueprint-from-url';
3-
import { resolveBlueprintFromURL } from '../../lib/state/url/resolve-blueprint-from-url';
42
import { useCurrentUrl } from '../../lib/state/url/router-hooks';
53
import { opfsSiteStorage } from '../../lib/state/opfs/opfs-site-storage';
64
import {
@@ -21,6 +19,7 @@ import { usePrevious } from '../../lib/hooks/use-previous';
2119
import { modalSlugs } from '../layout';
2220
import { setActiveModal } from '../../lib/state/redux/slice-ui';
2321
import { selectClientBySiteSlug } from '../../lib/state/redux/slice-clients';
22+
import { randomSiteName } from '../../lib/state/redux/random-site-name';
2423

2524
/**
2625
* Ensures the redux store always has an activeSite value.
@@ -151,50 +150,16 @@ export function EnsurePlaygroundSiteIsSelected({
151150
return children;
152151
}
153152

154-
function parseSearchParams(searchParams: URLSearchParams) {
155-
const params: Record<string, any> = {};
156-
for (const key of searchParams.keys()) {
157-
const value = searchParams.getAll(key);
158-
params[key] = value.length > 1 ? value : value[0];
159-
}
160-
return params;
161-
}
162153
async function createNewTemporarySite(
163154
dispatch: ReturnType<typeof useAppDispatch>,
164155
requestedSiteSlug?: string
165156
) {
166157
// If the site slug is missing, create a new temporary site.
167-
// Lean on the Query API parameters and the Blueprint API to
168-
// create the new site.
169-
const newUrl = new URL(window.location.href);
170-
const defaultBlueprint =
171-
'https://raw.githubusercontent.com/WordPress/blueprints/refs/heads/trunk/blueprints/welcome/blueprint.json';
172-
let resolvedBlueprint: ResolvedBlueprint | undefined = undefined;
173-
174-
try {
175-
resolvedBlueprint = await resolveBlueprintFromURL(
176-
newUrl,
177-
defaultBlueprint
178-
);
179-
} catch (e) {
180-
logger.error('Error resolving blueprint:', e);
181-
}
182-
183-
// Create a new site otherwise
158+
const siteName = requestedSiteSlug
159+
? deriveSiteNameFromSlug(requestedSiteSlug)
160+
: randomSiteName();
184161
const newSiteInfo = await dispatch(
185-
setTemporarySiteSpec({
186-
metadata: {
187-
originalBlueprint: resolvedBlueprint?.blueprint,
188-
originalBlueprintSource: resolvedBlueprint?.source,
189-
name: requestedSiteSlug
190-
? deriveSiteNameFromSlug(requestedSiteSlug)
191-
: undefined,
192-
},
193-
originalUrlParams: {
194-
searchParams: parseSearchParams(newUrl.searchParams),
195-
hash: newUrl.hash,
196-
},
197-
})
162+
setTemporarySiteSpec(siteName, new URL(window.location.href))
198163
);
199164
await dispatch(setActiveSite(newSiteInfo.slug));
200165
}

packages/playground/website/src/components/site-manager/sidebar/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
useAppDispatch,
2323
useAppSelector,
2424
} from '../../../lib/state/redux/store';
25-
import type { SiteLogo } from '../../../lib/site-metadata';
25+
import type { SiteLogo } from '../../../lib/state/redux/slice-sites';
2626
import {
2727
selectSortedSites,
2828
selectTemporarySite,

packages/playground/website/src/components/site-manager/site-persist-button/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { persistTemporarySite } from '../../../lib/state/redux/persist-temporary
1111
import { selectClientInfoBySiteSlug } from '../../../lib/state/redux/slice-clients';
1212
import { useLocalFsAvailability } from '../../../lib/hooks/use-local-fs-availability';
1313
import { isOpfsAvailable } from '../../../lib/state/opfs/opfs-site-storage';
14-
import type { SiteStorageType } from '../../../lib/site-metadata';
14+
import type { SiteStorageType } from '../../../lib/state/redux/slice-sites';
1515

1616
export function SitePersistButton({
1717
siteSlug,

packages/playground/website/src/components/site-manager/storage-type/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Icon } from '@wordpress/components';
22
import { ClockIcon, folder, layout } from '@wp-playground/components';
33
import css from './style.module.css';
4-
import type { SiteStorageType } from '../../../lib/site-metadata';
4+
import type { SiteStorageType } from '../../../lib/state/redux/slice-sites';
55

66
export function StorageType({ type }: { type: SiteStorageType }) {
77
switch (type) {

packages/playground/website/src/lib/site-metadata.ts

Lines changed: 0 additions & 124 deletions
This file was deleted.

packages/playground/website/src/lib/state/opfs/opfs-site-storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import metadataWorkerUrl from './opfs-site-storage-worker-for-safari?worker&url';
9-
import type { SiteMetadata } from '../../site-metadata';
9+
import type { SiteMetadata } from '../redux/slice-sites';
1010
import type { SiteInfo } from '../redux/slice-sites';
1111
import { joinPaths } from '@php-wasm/util';
1212
import { logger } from '@php-wasm/logger';

packages/playground/website/src/lib/state/redux/persist-temporary-site.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
updateSiteMetadata,
1616
} from './slice-sites';
1717
import { PlaygroundRoute, redirectTo } from '../url/router';
18-
import type { SiteStorageType } from '../../site-metadata';
18+
import type { SiteStorageType } from './slice-sites';
1919

2020
export function persistTemporarySite(
2121
siteSlug: string,

0 commit comments

Comments
 (0)