Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

Track changes to prevent useless requests

Isaac395dc5c0cbb6
diff --git a/hooks/useArchivalTags.ts b/hooks/useArchivalTags.ts
index 9d0e5060..21e3c936 100644
--- a/hooks/useArchivalTags.ts
+++ b/hooks/useArchivalTags.ts
@@ -65,7 +65,17 @@ const useArchivalTags = (initialTags: Tag[]) => {
const removeTag = (tagToDelete: ArchivalTagOption) => {
setArchivalTags(prev => prev.map(t =>
- t.label === tagToDelete.label ? { ...t, archiveAsScreenshot: null, archiveAsMonolith: null, archiveAsPDF: null, archiveAsReadable: null, archiveAsWaybackMachine: null, aiTag: null } : t
+ t.label === tagToDelete.label ?
+ {
+ ...t,
+ archiveAsScreenshot: null,
+ archiveAsMonolith: null,
+ archiveAsPDF: null,
+ archiveAsReadable: null,
+ archiveAsWaybackMachine: null,
+ aiTag: null
+ }
+ : t
));
if (!tagToDelete.__isNew__) {
diff --git a/pages/settings/preference.tsx b/pages/settings/preference.tsx
index 190c596c..f0658bc3 100644
--- a/pages/settings/preference.tsx
+++ b/pages/settings/preference.tsx
@@ -52,6 +52,8 @@ export default function Preference() {
account.aiTaggingMethod
);
const [aiPredefinedTags, setAiPredefinedTags] = useState<string[]>();
+ const [hasAccountChanges, setHasAccountChanges] = useState(false);
+ const [hasTagChanges, setHasTagChanges] = useState(false);
const { data: config } = useConfig();
useEffect(() => {
@@ -104,40 +106,70 @@ export default function Preference() {
}
}, [account]);
- // Need to track the changes so we don't make useless API calls
- // tags.length !== archivalTags.length
- // also need to track the user object as well
+ useEffect(() => {
+ const relevantKeys = [
+ 'archiveAsScreenshot',
+ 'archiveAsMonolith',
+ 'archiveAsPDF',
+ 'archiveAsReadable',
+ 'archiveAsWaybackMachine',
+ 'linksRouteTo',
+ 'preventDuplicateLinks',
+ 'aiTaggingMethod',
+ 'aiPredefinedTags',
+ 'dashboardRecentLinks',
+ 'dashboardPinnedLinks'
+ ];
+
+ const hasChanges = relevantKeys.some(key =>
+ account[key] !== user[key]
+ );
+
+ setHasAccountChanges(hasChanges);
+ }, [account, user]);
+
+ useEffect(() => {
+ if (!tags || !archivalTags) return;
+
+ const hasChanges = archivalTags.some(newTag => {
+ const originalTag = tags.find(t => t.name === newTag.label);
+
+ if (!originalTag) return true;
+
+ return (
+ newTag.archiveAsScreenshot !== originalTag.archiveAsScreenshot ||
+ newTag.archiveAsMonolith !== originalTag.archiveAsMonolith ||
+ newTag.archiveAsPDF !== originalTag.archiveAsPDF ||
+ newTag.archiveAsReadable !== originalTag.archiveAsReadable ||
+ newTag.archiveAsWaybackMachine !== originalTag.archiveAsWaybackMachine ||
+ newTag.aiTag !== originalTag.aiTag
+ );
+ });
+
+ setHasTagChanges(hasChanges);
+ }, [archivalTags, tags]);
+
const submit = async () => {
setSubmitLoader(true);
const load = toast.loading(t("applying_settings"));
try {
- await updateUser.mutateAsync({ ...user });
- await updateArchivalTags.mutateAsync(archivalTags);
- toast.success(t("settings_applied"));
+ const promises = [];
+
+ if (hasAccountChanges) promises.push(updateUser.mutateAsync({ ...user }));
+ if (hasTagChanges) promises.push(updateArchivalTags.mutateAsync(archivalTags));
+
+ if (promises.length > 0) {
+ await Promise.all(promises);
+ toast.success(t("settings_applied"));
+ }
} catch (error: any) {
toast.error(error.message);
} finally {
setSubmitLoader(false);
toast.dismiss(load);
}
-
- // await updateUser.mutateAsync(
- // { ...user },
- // {
- // onSettled: (_, error) => {
- // setSubmitLoader(false);
- // toast.dismiss(load);
-
- // if (error) {
- // toast.error(error.message);
- // } else {
- // toast.success(t("settings_applied"));
- // }
- // },
- // }
- // );
};
return (