fix: workspace router

This commit is contained in:
alt0
2023-02-07 22:46:40 +08:00
parent 731deda1e1
commit ef4a25d82b
3 changed files with 9 additions and 7 deletions

View File

@@ -1,3 +1,4 @@
import { useRouter } from 'next/router';
import Modal from '@/ui/modal'; import Modal from '@/ui/modal';
import Input from '@/ui/input'; import Input from '@/ui/input';
import { import {
@@ -29,6 +30,7 @@ export const WorkspaceDelete = ({
}: WorkspaceDeleteProps) => { }: WorkspaceDeleteProps) => {
const [deleteStr, setDeleteStr] = useState<string>(''); const [deleteStr, setDeleteStr] = useState<string>('');
const { t } = useTranslation(); const { t } = useTranslation();
const router = useRouter();
const { deleteWorkSpace } = useWorkspaceHelper(); const { deleteWorkSpace } = useWorkspaceHelper();
const handlerInputChange = (workspaceName: string) => { const handlerInputChange = (workspaceName: string) => {
setDeleteStr(workspaceName); setDeleteStr(workspaceName);
@@ -37,6 +39,7 @@ export const WorkspaceDelete = ({
const handleDelete = async () => { const handleDelete = async () => {
await deleteWorkSpace(); await deleteWorkSpace();
onClose(); onClose();
router.push(`/workspace`);
}; };
return ( return (

View File

@@ -5,7 +5,7 @@ import { useRouter } from 'next/router';
// Cause it not just ensure workspace loaded, but also have router change. // Cause it not just ensure workspace loaded, but also have router change.
export const useEnsureWorkspace = () => { export const useEnsureWorkspace = () => {
const [workspaceLoaded, setWorkspaceLoaded] = useState(false); const [workspaceLoaded, setWorkspaceLoaded] = useState(false);
const { dataCenter, loadWorkspace } = useAppState(); const { workspaceList, loadWorkspace, user } = useAppState();
const router = useRouter(); const router = useRouter();
const [activeWorkspaceId, setActiveWorkspaceId] = useState( const [activeWorkspaceId, setActiveWorkspaceId] = useState(
router.query.workspaceId as string router.query.workspaceId as string
@@ -17,14 +17,14 @@ export const useEnsureWorkspace = () => {
// If router.query.workspaceId is not in workspace list, jump to 404 page // If router.query.workspaceId is not in workspace list, jump to 404 page
// If workspaceList is empty, we need to create a default workspace but not jump to 404 // If workspaceList is empty, we need to create a default workspace but not jump to 404
if ( if (
dataCenter.workspaces.length && workspaceList.length &&
// FIXME: router is not ready when this hook is called // FIXME: router is not ready when this hook is called
location.pathname.startsWith(`/workspace/${router.query.workspaceId}`) && location.pathname.startsWith(`/workspace/${router.query.workspaceId}`) &&
dataCenter.workspaces.findIndex( workspaceList.findIndex(
meta => meta.id.toString() === router.query.workspaceId meta => meta.id.toString() === router.query.workspaceId
) === -1 ) === -1
) { ) {
router.push(`/404`); router.push('/404');
return; return;
} }
// If user is not login and input a custom workspaceId, jump to 404 page // If user is not login and input a custom workspaceId, jump to 404 page
@@ -37,12 +37,12 @@ export const useEnsureWorkspace = () => {
// return; // return;
// } // }
const workspaceId = const workspaceId =
(router.query.workspaceId as string) || dataCenter.workspaces[0]?.id; (router.query.workspaceId as string) || workspaceList[0]?.id;
loadWorkspace.current(workspaceId).finally(() => { loadWorkspace.current(workspaceId).finally(() => {
setWorkspaceLoaded(true); setWorkspaceLoaded(true);
setActiveWorkspaceId(activeWorkspaceId); setActiveWorkspaceId(activeWorkspaceId);
}); });
}, [loadWorkspace, router, dataCenter.workspaces, activeWorkspaceId]); }, [loadWorkspace, router, user, workspaceList, activeWorkspaceId]);
return { return {
workspaceLoaded, workspaceLoaded,

View File

@@ -72,7 +72,6 @@ export const AppStateProvider = ({
// FIXME: onWorkspacesChange should have dispose function // FIXME: onWorkspacesChange should have dispose function
dataCenter?.onWorkspacesChange( dataCenter?.onWorkspacesChange(
() => { () => {
console.log(123);
setAppState({ setAppState({
...appState, ...appState,
workspaceList: dataCenter.workspaces, workspaceList: dataCenter.workspaces,