mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-24 13:17:10 +08:00
refactor: update progress
This commit is contained in:
@@ -40,6 +40,8 @@ export const KanbanParentBlockRender = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const useBlockProgress = (block?: AsyncBlock) => {
|
const useBlockProgress = (block?: AsyncBlock) => {
|
||||||
|
// Progress of the progress bar. The range is between 0 and 1.
|
||||||
|
// Default progress is 1, that is 100%.
|
||||||
const [progress, setProgress] = useState(1);
|
const [progress, setProgress] = useState(1);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -57,36 +59,13 @@ const useBlockProgress = (block?: AsyncBlock) => {
|
|||||||
setProgress(checkedTodoChildren.length / todoChildren.length);
|
setProgress(checkedTodoChildren.length / todoChildren.length);
|
||||||
};
|
};
|
||||||
|
|
||||||
let childrenQueue: (() => void)[] = [];
|
|
||||||
const childrenUnobserve = () => {
|
|
||||||
childrenQueue.forEach(fn => fn());
|
|
||||||
childrenQueue = [];
|
|
||||||
};
|
|
||||||
|
|
||||||
const observeChildren = async () => {
|
|
||||||
const children = await block.children();
|
|
||||||
|
|
||||||
childrenUnobserve();
|
|
||||||
children.forEach(child => {
|
|
||||||
const unobserve = child.onUpdate(() => {
|
|
||||||
updateProgress();
|
|
||||||
});
|
|
||||||
childrenQueue.push(unobserve);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
observeChildren();
|
|
||||||
updateProgress();
|
updateProgress();
|
||||||
|
|
||||||
const unobserve = block.onUpdate(() => {
|
const unobserve = block.onUpdate(() => {
|
||||||
observeChildren();
|
|
||||||
updateProgress();
|
updateProgress();
|
||||||
});
|
});
|
||||||
|
|
||||||
return () => {
|
return unobserve;
|
||||||
unobserve();
|
|
||||||
childrenUnobserve();
|
|
||||||
};
|
|
||||||
}, [block]);
|
}, [block]);
|
||||||
|
|
||||||
return progress;
|
return progress;
|
||||||
|
|||||||
Reference in New Issue
Block a user