Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the site switcher work again #1698

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ export default function PlaygroundConfigurationGroup({
type: 'local-fs',
handle: dirHandle,
};
dispatch(setOpfsMountDescriptor({ device, mountpoint }));
if (idb) {
await saveDirectoryHandle(idb, dirHandle);
}
Expand Down Expand Up @@ -200,15 +199,15 @@ export default function PlaygroundConfigurationGroup({
...currentConfiguration,
storage: 'device',
});
await playground.goTo('/');

// Read current querystring and replace storage=browser with
// storage=device.
const url = new URL(window.location.href);
url.searchParams.set('storage', 'device');
window.history.pushState({}, '', url.toString());

alert('You are now using WordPress from your local directory.');
dispatch(setOpfsMountDescriptor({ device, mountpoint }));
alert('You are now loading WordPress from your local directory.');
} finally {
setMounting(false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SiteManagerSidebar } from './site-manager-sidebar';
import { __experimentalUseNavigator as useNavigator } from '@wordpress/components';
import store, { selectSite } from '../../lib/redux-store';

import css from './style.module.css';

Expand All @@ -26,7 +27,7 @@ export function SiteManager({
);
};

const onSiteClick = (siteSlug?: string) => {
const onSiteClick = async (siteSlug: string) => {
onSiteChange(siteSlug);
const url = new URL(window.location.href);
if (siteSlug) {
Expand All @@ -36,6 +37,8 @@ export function SiteManager({
}
window.history.pushState({}, '', url.toString());

await store.dispatch(selectSite(siteSlug));

/**
* On mobile, the site editor and site preview are hidden.
* This doesn't give users any way to go back to the site view.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function SiteManagerSidebar({
}: {
className?: string;
siteSlug?: string;
onSiteClick: (siteSlug?: string) => void;
onSiteClick: (siteSlug: string) => void;
}) {
const [sites, setSites] = useState<Site[]>([]);

Expand Down
21 changes: 21 additions & 0 deletions packages/playground/website/src/lib/redux-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ const slice = createSlice({
// Export actions
export const { setActiveModal, setOpfsMountDescriptor } = slice.actions;

export function selectSite(siteSlug: string) {
return async (dispatch: typeof store.dispatch) => {
const opfsRoot = await navigator.storage.getDirectory();
const opfsDir = await opfsRoot.getDirectoryHandle(
siteSlug === 'wordpress' ? siteSlug : 'site-' + siteSlug,
{
create: true,
}
);
dispatch(
setOpfsMountDescriptor({
device: {
type: 'opfs',
path: await directoryHandleToOpfsPath(opfsDir),
},
mountpoint: '/wordpress',
})
);
};
}

// Configure store
const store = configureStore({
reducer: slice.reducer,
Expand Down
3 changes: 0 additions & 3 deletions packages/playground/website/src/lib/use-boot-playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ export function useBootPlayground({ blueprint }: UsePlaygroundOptions) {

useEffect(() => {
const remoteUrl = getRemoteUrl();
if (started.current === remoteUrl.toString()) {
return;
}
if (!iframe) {
// Iframe ref is likely not set on the initial render.
// Re-render the current component to start the playground.
Expand Down
Loading