Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

refactor: convert useStatus hook to use zustand

thecodrr2766f1dbc6ad
diff --git a/apps/web/src/hooks/useStatus.js b/apps/web/src/hooks/useStatus.js
index ef0214014..5f1edbf88 100644
--- a/apps/web/src/hooks/useStatus.js
+++ b/apps/web/src/hooks/useStatus.js
@@ -1,53 +1,26 @@
-import { useEffect } from "react";
-import { AppEventManager, AppEvents } from "../common/app-events";
-import { useState } from "react";
+import create from "zustand";
-export default function useStatus() {
- const [statuses, setStatuses] = useState({});
- useEffect(() => {
- const updateStatusEvent = AppEventManager.subscribe(
- AppEvents.UPDATE_STATUS,
- (param) => {
- const { key, status, progress, icon } = param;
- setStatuses((statuses) => {
- if (!key) return statuses;
- const statusText = status || statuses[key]?.status;
- return {
- ...statuses,
- [key]: { key, status: statusText, progress, icon },
- };
- });
- }
- );
- const removeStatusEvent = AppEventManager.subscribe(
- AppEvents.REMOVE_STATUS,
- (key) => {
- setStatuses((statuses) => {
- if (!key || !statuses[key]) return statuses;
- const clone = { ...statuses };
- delete clone[key];
- return clone;
- });
- }
- );
- return () => {
- updateStatusEvent.unsubscribe();
- removeStatusEvent.unsubscribe();
- };
- }, [setStatuses]);
+const useStatusStore = create((set) => ({
+ statuses: {},
+ updateStatus: ({ key, status, progress, icon }) =>
+ set((state) => {
+ if (!key) return;
+ const { statuses } = state;
+ const statusText = status || statuses[key]?.status;
+ statuses[key] = { key, status: statusText, progress, icon };
+ }),
+ removeStatus: (key) =>
+ set((state) => {
+ const { statuses } = state;
+ if (!key || !statuses[key]) return;
+ delete statuses[key];
+ }),
+}));
+export default function useStatus() {
+ const statuses = useStatusStore((store) => store.statuses);
return Object.values(statuses);
}
-export function updateStatus({ key, status, progress, icon }) {
- AppEventManager.publish(AppEvents.UPDATE_STATUS, {
- key,
- status,
- progress,
- icon,
- });
-}
-
-export function removeStatus(key) {
- AppEventManager.publish(AppEvents.REMOVE_STATUS, key);
-}
+export const updateStatus = useStatusStore.getState().updateStatus;
+export const removeStatus = useStatusStore.getState().removeStatus;