Skip to content

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

refactor: move status handling to its own hook

thecodrr5973b799222d
diff --git a/apps/web/src/app-effects.js b/apps/web/src/app-effects.js
index 26080d942..5bf7db712 100644
--- a/apps/web/src/app-effects.js
+++ b/apps/web/src/app-effects.js
@@ -1,5 +1,5 @@
import React, { useEffect } from "react";
-import { useStore, store } from "./stores/app-store";
+import { useStore } from "./stores/app-store";
import { useStore as useUserStore } from "./stores/user-store";
import { useStore as useNotesStore } from "./stores/note-store";
import { useStore as useThemeStore } from "./stores/theme-store";
@@ -18,6 +18,7 @@ import {
} from "./common/dialog-controller";
import useSystemTheme from "./utils/use-system-theme";
import { isTesting } from "./utils/platform";
+import { updateStatus, removeStatus } from "./hooks/use-status";
if (process.env.NODE_ENV === "production") {
loadTrackerScript();
@@ -28,7 +29,6 @@ export default function AppEffects({ setShow }) {
const refreshNavItems = useStore((store) => store.refreshNavItems);
const sync = useStore((store) => store.sync);
const updateLastSynced = useStore((store) => store.updateLastSynced);
- const setProcessingStatus = useStore((store) => store.setProcessingStatus);
const isFocusMode = useStore((store) => store.isFocusMode);
const addReminder = useStore((store) => store.addReminder);
const initUser = useUserStore((store) => store.init);
@@ -95,13 +95,15 @@ export default function AppEffects({ setShow }) {
({ type, total, current }) => {
const [key, status] = getProcessingStatusFromType(type);
- if (current === total) setProcessingStatus(key);
- else
- setProcessingStatus(
+ if (current === total) {
+ removeStatus(key);
+ } else {
+ updateStatus({
key,
- `${status} attachments (${current}/${total})`,
- 0
- );
+ status: `${status} attachments (${current}/${total})`,
+ progress: 0,
+ });
+ }
}
);
@@ -111,13 +113,12 @@ export default function AppEffects({ setShow }) {
const [key] = getProcessingStatusFromType(type);
if (!key) return;
- const processingStatus = store.get().processingStatuses[key];
- if (!processingStatus) return;
- const { status } = processingStatus;
const percent = Math.round((loaded / total) * 100);
- if (loaded === total) setProcessingStatus(key, status, 100);
- else setProcessingStatus(key, status, percent);
+ updateStatus({
+ key,
+ progress: loaded === total ? 100 : percent,
+ });
}
);
@@ -127,7 +128,7 @@ export default function AppEffects({ setShow }) {
progressEvent.unsubscribe();
systemTimeInvalidEvent.unsubscribe();
};
- }, [setProcessingStatus]);
+ }, []);
useEffect(() => {
setShow(!isFocusMode);
diff --git a/apps/web/src/common/selectionoptions.js b/apps/web/src/common/selectionoptions.js
index e494321a9..1e3ce039b 100644
--- a/apps/web/src/common/selectionoptions.js
+++ b/apps/web/src/common/selectionoptions.js
@@ -14,6 +14,7 @@ import {
import { showExportDialog } from "../common/dialog-controller";
import { showToast } from "../utils/toast";
import { hashNavigate } from "../navigation";
+import { removeStatus, updateStatus } from "../hooks/use-status";
function createOption(key, title, icon, onClick) {
return {
@@ -23,7 +24,7 @@ function createOption(key, title, icon, onClick) {
onClick: async () => {
await onClick.call(this, selectionStore.get());
selectionStore.toggleSelectionMode(false);
- appStore.setProcessingStatus(key);
+ removeStatus(key);
},
};
}
@@ -47,7 +48,7 @@ const DeleteOption = createOption(
const statusText = `${
item.dateDeleted ? `Permanently deleting` : `Deleting`
} ${state.selectedItems.length} items...`;
- appStore.setProcessingStatus("deleteOption", statusText);
+ updateStatus({ key: "deleteOption", status: statusText });
var isAnyNoteOpened = false;
const items = state.selectedItems.map((item) => {
@@ -86,10 +87,10 @@ const UnfavoriteOption = createOption(
"Unfavorite",
Icon.Star,
function (state) {
- appStore.setProcessingStatus(
- "unfavoriteOption",
- `Unfavoriting ${state.selectedItems.length} notes...`
- );
+ updateStatus({
+ key: "unfavoriteOption",
+ status: `Unfavoriting ${state.selectedItems.length} notes...`,
+ });
// we know only notes can be favorited
state.selectedItems.forEach(async (item) => {
@@ -105,10 +106,10 @@ const AddToNotebookOption = createOption(
"Add to notebook(s)",
Icon.AddToNotebook,
async function (state) {
- appStore.setProcessingStatus(
- "atnOption",
- `Adding ${state.selectedItems.length} notes to notebooks...`
- );
+ updateStatus({
+ key: "atnOption",
+ status: `Adding ${state.selectedItems.length} notes to notebooks...`,
+ });
const items = state.selectedItems.map((item) => item.id);
await showMoveNoteDialog(items);
@@ -121,10 +122,10 @@ const ExportOption = createOption(
"Export",
Icon.Export,
async function (state) {
- appStore.setProcessingStatus(
- "exportOption",
- `Exporting ${state.selectedItems.length} notes...`
- );
+ updateStatus({
+ key: "exportOption",
+ status: `Exporting ${state.selectedItems.length} notes...`,
+ });
const items = state.selectedItems.map((item) => item.id);
if (await showExportDialog(items)) {
@@ -138,10 +139,10 @@ const RestoreOption = createOption(
"Restore",
Icon.Restore,
async function (state) {
- appStore.setProcessingStatus(
- "restoreOption",
- `Restoring ${state.selectedItems.length} items...`
- );
+ updateStatus({
+ key: "restoreOption",
+ status: `Restoring ${state.selectedItems.length} items...`,
+ });
const items = state.selectedItems.map((item) => item.id);
await db.trash.restore(...items);
diff --git a/apps/web/src/components/status-bar/index.js b/apps/web/src/components/status-bar/index.js
index 871cf9f0e..6128a6a79 100644
--- a/apps/web/src/components/status-bar/index.js
+++ b/apps/web/src/components/status-bar/index.js
@@ -18,15 +18,14 @@ import useAutoUpdater from "../../hooks/use-auto-updater";
import downloadUpdate from "../../commands/download-update";
import installUpdate from "../../commands/install-update";
import checkForUpdate from "../../commands/check-for-update";
-import { showIssueDialog } from "../../common/dialog-controller";
+import useStatus from "../../hooks/use-status";
+import { getIconFromAlias } from "../icons/resolver";
function StatusBar() {
const user = useUserStore((state) => state.user);
const sync = useAppStore((state) => state.sync);
const isLoggedIn = useUserStore((state) => state.isLoggedIn);
- const processingStatuses = useAppStore((state) =>
- Object.values(state.processingStatuses)
- );
+ const statuses = useStatus();
const updateStatus = useAutoUpdater();
return (
@@ -94,14 +93,17 @@ function StatusBar() {
Report an issue
</Text>
</Button>
- {processingStatuses?.map(({ key, status, progress }) => (
- <Flex key={key} ml={1} alignItems="center" justifyContent="center">
- <Loading size={12} />
- <Text variant="subBody" color="bgSecondaryText" ml={1}>
- {progress ? `${progress}% ${status}` : status}
- </Text>
- </Flex>
- ))}
+ {statuses?.map(({ key, status, progress, icon }) => {
+ const Icon = getIconFromAlias(icon);
+ return (
+ <Flex key={key} ml={1} alignItems="center" justifyContent="center">
+ {Icon ? <Icon size={12} /> : <Loading size={12} />}
+ <Text variant="subBody" color="bgSecondaryText" ml={1}>
+ {progress ? `${progress}% ${status}` : status}
+ </Text>
+ </Flex>
+ );
+ })}
{updateStatus && (
<Button
diff --git a/apps/web/src/hooks/useStatus.js b/apps/web/src/hooks/useStatus.js
new file mode 100644
index 000000000..ef0214014
--- /dev/null
+++ b/apps/web/src/hooks/useStatus.js
@@ -0,0 +1,53 @@
+import { useEffect } from "react";
+import { AppEventManager, AppEvents } from "../common/app-events";
+import { useState } from "react";
+
+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]);
+
+ 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);
+}
diff --git a/apps/web/src/stores/app-store.js b/apps/web/src/stores/app-store.js
index 78eb754a7..910c3c260 100644
--- a/apps/web/src/stores/app-store.js
+++ b/apps/web/src/stores/app-store.js
@@ -16,7 +16,6 @@ class AppStore extends BaseStore {
isFocusMode = false;
isEditorOpen = false;
isVaultCreated = false;
- processingStatuses = {};
syncStatus = "synced";
colors = [];
globalMenu = { items: [], data: {} };
@@ -65,13 +64,6 @@ class AppStore extends BaseStore {
this.set((state) => (state.isVaultCreated = toggleState));
};
- setProcessingStatus = (key, status, progress) => {
- this.set((state) => {
- if (!status) delete state.processingStatuses[key];
- else state.processingStatuses[key] = { key, status, progress };
- });
- };
-
/**
*
* @param {"backup"|"signup"|"email"|"recoverykey"} type