fix: correct router logic (#2342)

This commit is contained in:
Himself65
2023-05-12 13:55:45 +08:00
committed by GitHub
parent 10b4558947
commit 21fdced2bd
24 changed files with 206 additions and 332 deletions

View File

@@ -37,6 +37,7 @@
},
"devDependencies": {
"@types/ws": "^8.5.4",
"next": "^13.4.1",
"ws": "^8.13.0"
},
"version": "0.6.0-canary.0"

View File

@@ -1,6 +1,7 @@
import type { EditorContainer } from '@blocksuite/editor';
import { atom, createStore } from 'jotai';
import { atomWithStorage, createJSONStorage } from 'jotai/utils';
import Router from 'next/router';
import type { WorkspaceFlavour } from './type';
@@ -32,12 +33,49 @@ export const rootCurrentWorkspaceIdAtom = atomWithStorage<string | null>(
null,
createJSONStorage(() => sessionStorage)
);
rootCurrentWorkspaceIdAtom.onMount = set => {
if (typeof window !== 'undefined') {
const callback = (url: string) => {
const value = url.split('/')[2];
if (value) {
set(value);
} else {
set(null);
}
};
callback(window.location.pathname);
Router.events.on('routeChangeStart', callback);
return () => {
Router.events.off('routeChangeStart', callback);
};
}
};
export const rootCurrentPageIdAtom = atomWithStorage<string | null>(
'root-current-page-id',
null,
createJSONStorage(() => sessionStorage)
);
rootCurrentPageIdAtom.onMount = set => {
if (typeof window !== 'undefined') {
const callback = (url: string) => {
const value = url.split('/')[3];
if (value) {
set(value);
} else {
set(null);
}
};
callback(window.location.pathname);
Router.events.on('routeChangeStart', callback);
return () => {
Router.events.off('routeChangeStart', callback);
};
}
};
// current editor atom, each app should have only one editor in the same time
export const rootCurrentEditorAtom = atom<Readonly<EditorContainer> | null>(
null