mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-17 01:56:27 +08:00
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import { useRouter } from 'next/router';
|
|
import { useAppState } from '@/providers/app-state-provider/context';
|
|
import { useEffect, useRef, useState } from 'react';
|
|
|
|
export const useInitWorkspace = (disabled?: boolean) => {
|
|
const [loading, setLoading] = useState(true);
|
|
// Do not set as a constant, or it will trigger a hell of re-rendering
|
|
const defaultOutLineWorkspaceId = useRef(new Date().getTime().toString());
|
|
|
|
const router = useRouter();
|
|
const {
|
|
workspacesMeta,
|
|
loadWorkspace,
|
|
currentWorkspace,
|
|
currentWorkspaceId,
|
|
} = useAppState();
|
|
const workspaceId =
|
|
(router.query.workspaceId as string) ||
|
|
workspacesMeta?.[0]?.id ||
|
|
defaultOutLineWorkspaceId.current;
|
|
|
|
useEffect(() => {
|
|
if (disabled) {
|
|
setLoading(false);
|
|
return;
|
|
}
|
|
setLoading(true);
|
|
loadWorkspace(workspaceId).finally(() => {
|
|
setLoading(false);
|
|
});
|
|
}, [workspaceId, disabled, loadWorkspace]);
|
|
|
|
return {
|
|
workspaceId,
|
|
workspace: workspaceId === currentWorkspaceId ? currentWorkspace : null,
|
|
loading,
|
|
};
|
|
};
|