Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

fix: only delete last file when select multiple files #1576 (#1578)

CorrectRoadHf7a1680f72fd
diff --git a/web/src/hooks/useEvent.ts b/web/src/hooks/useEvent.ts
new file mode 100644
index 000000000..8b218d472
--- /dev/null
+++ b/web/src/hooks/useEvent.ts
@@ -0,0 +1,25 @@
+import React, { useEffect, useRef, EffectCallback, DependencyList } from "react";
+
+const useIsoMorphicEffect = (effect: EffectCallback, deps?: DependencyList | undefined) => {
+ useEffect(effect, deps);
+};
+
+function useLatestValue<T>(value: T) {
+ const cache = useRef(value);
+
+ useIsoMorphicEffect(() => {
+ cache.current = value;
+ }, [value]);
+
+ return cache;
+}
+
+// TODO: Add React.useEvent ?? once the useEvent hook is available
+function useEvent<F extends (...args: any[]) => any, P extends any[] = Parameters<F>, R = ReturnType<F>>(
+ cb: (...args: P) => R
+) {
+ const cache = useLatestValue(cb);
+ return React.useCallback((...args: P) => cache.current(...args), [cache]);
+};
+
+export default useEvent;
diff --git a/web/src/pages/ResourcesDashboard.tsx b/web/src/pages/ResourcesDashboard.tsx
index b230c58e5..dff5cf18f 100644
--- a/web/src/pages/ResourcesDashboard.tsx
+++ b/web/src/pages/ResourcesDashboard.tsx
@@ -13,6 +13,7 @@ import Dropdown from "@/components/kit/Dropdown";
import ResourceItem from "@/components/ResourceItem";
import { showCommonDialog } from "@/components/Dialog/CommonDialog";
import showCreateResourceDialog from "@/components/CreateResourceDialog";
+import useEvent from "@/hooks/useEvent";
const ResourcesDashboard = () => {
const { t } = useTranslation();
@@ -40,13 +41,13 @@ const ResourcesDashboard = () => {
});
}, []);
- const handleCheckBtnClick = (resourceId: ResourceId) => {
+ const handleCheckBtnClick = useEvent((resourceId: ResourceId) => {
setSelectedList([...selectedList, resourceId]);
- };
+ });
- const handleUncheckBtnClick = (resourceId: ResourceId) => {
+ const handleUncheckBtnClick = useEvent((resourceId: ResourceId) => {
setSelectedList(selectedList.filter((id) => id !== resourceId));
- };
+ });
const handleStyleChangeBtnClick = (listStyle: "GRID" | "TABLE") => {
setListStyle(listStyle);