mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-31 00:59:57 +08:00
fix: remove useEffect on router sync with atoms (#2241)
This commit is contained in:
@@ -5,7 +5,6 @@ import {
|
||||
} from '@affine/workspace/atom';
|
||||
import { useAtom, useAtomValue } from 'jotai';
|
||||
import type { NextRouter } from 'next/router';
|
||||
import { useEffect } from 'react';
|
||||
|
||||
const logger = new DebugLogger('useSyncRouterWithCurrentWorkspaceId');
|
||||
|
||||
@@ -14,34 +13,49 @@ export function useSyncRouterWithCurrentWorkspaceId(router: NextRouter) {
|
||||
rootCurrentWorkspaceIdAtom
|
||||
);
|
||||
const metadata = useAtomValue(rootWorkspacesMetadataAtom);
|
||||
useEffect(() => {
|
||||
if (!router.isReady) {
|
||||
return;
|
||||
}
|
||||
const workspaceId = router.query.workspaceId;
|
||||
if (typeof workspaceId !== 'string') {
|
||||
return;
|
||||
}
|
||||
if (currentWorkspaceId) {
|
||||
if (currentWorkspaceId !== workspaceId) {
|
||||
const target = metadata.find(workspace => workspace.id === workspaceId);
|
||||
if (!target) {
|
||||
logger.debug('workspace not exist, redirect to current one');
|
||||
// workspaceId is invalid, redirect to currentWorkspaceId
|
||||
void router.push({
|
||||
pathname: router.pathname,
|
||||
query: {
|
||||
...router.query,
|
||||
workspaceId: currentWorkspaceId,
|
||||
},
|
||||
});
|
||||
}
|
||||
if (!router.isReady) {
|
||||
return;
|
||||
}
|
||||
const workspaceId = router.query.workspaceId;
|
||||
if (typeof workspaceId !== 'string') {
|
||||
return;
|
||||
}
|
||||
if (currentWorkspaceId === workspaceId) {
|
||||
return;
|
||||
}
|
||||
if (currentWorkspaceId) {
|
||||
if (currentWorkspaceId !== workspaceId) {
|
||||
const target = metadata.find(workspace => workspace.id === workspaceId);
|
||||
logger.debug('workspace not exist, redirect to current one');
|
||||
if (!target) {
|
||||
// workspaceId is invalid, redirect to currentWorkspaceId
|
||||
void router.push({
|
||||
pathname: router.pathname,
|
||||
query: {
|
||||
...router.query,
|
||||
workspaceId: currentWorkspaceId,
|
||||
},
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
const targetWorkspace = metadata.find(
|
||||
workspace => workspace.id === workspaceId
|
||||
);
|
||||
return;
|
||||
}
|
||||
const targetWorkspace = metadata.find(
|
||||
workspace => workspace.id === workspaceId
|
||||
);
|
||||
if (targetWorkspace) {
|
||||
console.log('set workspace id', workspaceId);
|
||||
setCurrentWorkspaceId(targetWorkspace.id);
|
||||
logger.debug('redirect to', targetWorkspace.id);
|
||||
void router.push({
|
||||
pathname: router.pathname,
|
||||
query: {
|
||||
...router.query,
|
||||
workspaceId: targetWorkspace.id,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
const targetWorkspace = metadata.at(0);
|
||||
if (targetWorkspace) {
|
||||
console.log('set workspace id', workspaceId);
|
||||
setCurrentWorkspaceId(targetWorkspace.id);
|
||||
@@ -53,20 +67,6 @@ export function useSyncRouterWithCurrentWorkspaceId(router: NextRouter) {
|
||||
workspaceId: targetWorkspace.id,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
const targetWorkspace = metadata.at(0);
|
||||
if (targetWorkspace) {
|
||||
console.log('set workspace id', workspaceId);
|
||||
setCurrentWorkspaceId(targetWorkspace.id);
|
||||
logger.debug('redirect to', targetWorkspace.id);
|
||||
void router.push({
|
||||
pathname: router.pathname,
|
||||
query: {
|
||||
...router.query,
|
||||
workspaceId: targetWorkspace.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}, [currentWorkspaceId, metadata, router, setCurrentWorkspaceId]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user