mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-02 09:59:55 +08:00
refactor: workspace manager (#5060)
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import type { Workspace, WorkspaceStatus } from '@affine/workspace';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
export function useWorkspaceStatus<
|
||||
Selector extends ((status: WorkspaceStatus) => any) | undefined | null,
|
||||
Status = Selector extends (status: WorkspaceStatus) => any
|
||||
? ReturnType<Selector>
|
||||
: WorkspaceStatus,
|
||||
>(workspace?: Workspace | null, selector?: Selector): Status | null {
|
||||
// avoid re-render when selector is changed
|
||||
const [cachedSelector] = useState(() => selector);
|
||||
|
||||
const [status, setStatus] = useState<Status | null>(() => {
|
||||
if (!workspace) {
|
||||
return null;
|
||||
}
|
||||
return cachedSelector ? cachedSelector(workspace.status) : workspace.status;
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (!workspace) {
|
||||
setStatus(null);
|
||||
return;
|
||||
}
|
||||
setStatus(
|
||||
cachedSelector ? cachedSelector(workspace.status) : workspace.status
|
||||
);
|
||||
return workspace.onStatusChange.on(status =>
|
||||
setStatus(cachedSelector ? cachedSelector(status) : status)
|
||||
).dispose;
|
||||
}, [cachedSelector, workspace]);
|
||||
|
||||
return status;
|
||||
}
|
||||
Reference in New Issue
Block a user