mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-29 00:20:04 +01:00
43 lines
995 B
TypeScript
43 lines
995 B
TypeScript
//@ts-ignore
|
|
import { EVENTS } from "@notesnook/core/common";
|
|
import { useCallback, useEffect, useState } from "react";
|
|
import { db } from "../common/database";
|
|
|
|
export type SyncProgressEventType = {
|
|
type: "upload" | "download";
|
|
total: number;
|
|
current: number;
|
|
};
|
|
|
|
const useSyncProgress = () => {
|
|
const [progress, setProgress] = useState<SyncProgressEventType>();
|
|
const EV = db.eventManager;
|
|
|
|
const onProgress = useCallback(
|
|
({ type, current, total }: SyncProgressEventType) => {
|
|
//@ts-ignore
|
|
|
|
setProgress({ type, current, total });
|
|
},
|
|
[]
|
|
);
|
|
|
|
const onSyncComplete = () => {
|
|
setProgress(undefined);
|
|
};
|
|
useEffect(() => {
|
|
EV?.subscribe(EVENTS.syncProgress, onProgress);
|
|
EV?.subscribe(EVENTS.syncCompleted, onSyncComplete);
|
|
return () => {
|
|
EV?.unsubscribe(EVENTS.syncProgress, onProgress);
|
|
EV?.unsubscribe(EVENTS.syncCompleted, onSyncComplete);
|
|
};
|
|
}, []);
|
|
|
|
return {
|
|
progress
|
|
};
|
|
};
|
|
|
|
export default useSyncProgress;
|