Skip to content

Commit 2c26d92

Browse files
authored
Fix tweak favoriting behavior (#757)
<!-- Please read https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before submitting your pull request --> ### Description <!-- Please include a summary of the change. Please also include relevant motivation and context. List any dependencies that are required for this change. --> Disabling a tweak that was automatically favorited no longer unfavorites. Disabling a tweak inside of the catalog menu that was favorited manually or was from a previous tweak browsing session no longer automatically unfavorites. #### Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ### Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings ### AI disclosure: - [ ] Partially AI assisted (clarify which code was AI assisted and briefly explain what it does). - [ ] Fully AI generated (explain what all the generated code does in moderate detail). <!-- Write any explanation required here, but do not generate the explanation using AI!! You must prove you understand what the code in this PR does. -->
2 parents e1d6b85 + b87af7a commit 2c26d92

3 files changed

Lines changed: 51 additions & 7 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
default: patch
3+
---
4+
5+
Fixed tweak automatic favoriting behavior when entering/leaving the catalog.

src/app/components/url-preview/TweakPreviewUrlCard.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ export function TweakPreviewUrlCard({ url }: { url: string }) {
198198
const nextEnabled = enabledTweakFullUrls.filter((u) => u !== url);
199199
patchSettings({
200200
themeRemoteEnabledTweakFullUrls: nextEnabled,
201-
themeRemoteTweakFavorites: pruneTweakFavorites(tweakFavorites, nextEnabled),
202201
});
203202
}
204203
},

src/app/features/settings/cosmetics/ThemeCatalogSettings.tsx

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type ChangeEventHandler, useCallback, useEffect, useMemo, useState } from 'react';
1+
import { type ChangeEventHandler, useCallback, useEffect, useMemo, useRef, useState } from 'react';
22
import { useTimeoutToggle } from '$hooks/useTimeoutToggle';
33
import { copyToClipboard, downloadTextFile } from '$utils/dom';
44
import { useQuery, useQueryClient } from '@tanstack/react-query';
@@ -214,6 +214,9 @@ export function ThemeCatalogSettings({
214214
const [browseOpen, setBrowseOpen] = useState(false);
215215
const [importModalOpen, setImportModalOpen] = useState(false);
216216

217+
const appearanceCatalogBrowseWasOpenRef = useRef(false);
218+
const tweakFavoritesSnapshotAtAppearanceCatalogOpenRef = useRef<Set<string>>(new Set());
219+
217220
useEffect(() => {
218221
if (isAppearanceMode) {
219222
onBrowseOpenChange?.(browseOpen);
@@ -248,6 +251,19 @@ export function ThemeCatalogSettings({
248251
'themeChatAutoPreviewAnyUrl'
249252
);
250253

254+
useEffect(() => {
255+
if (!isAppearanceMode) {
256+
appearanceCatalogBrowseWasOpenRef.current = false;
257+
return;
258+
}
259+
if (browseOpen && !appearanceCatalogBrowseWasOpenRef.current) {
260+
tweakFavoritesSnapshotAtAppearanceCatalogOpenRef.current = new Set(
261+
tweakFavorites.map((f) => f.fullUrl.trim()).filter(Boolean)
262+
);
263+
}
264+
appearanceCatalogBrowseWasOpenRef.current = browseOpen;
265+
}, [browseOpen, isAppearanceMode, tweakFavorites]);
266+
251267
const [themeSearch, setThemeSearch] = useState('');
252268
const [tweakSearch, setTweakSearch] = useState('');
253269
const [kindFilter, setKindFilter] = useState<'all' | 'light' | 'dark'>('all');
@@ -779,7 +795,15 @@ export function ThemeCatalogSettings({
779795
}, [patchSettings]);
780796

781797
const setTweakApplied = useCallback(
782-
async (fullUrl: string, apply: boolean, hint?: { displayName?: string; basename?: string }) => {
798+
async (
799+
fullUrl: string,
800+
apply: boolean,
801+
hint?: {
802+
displayName?: string;
803+
basename?: string;
804+
pruneUnpinnedFavoriteOnDisable?: boolean;
805+
}
806+
) => {
783807
const trimmed = fullUrl.trim();
784808
if (!trimmed) return;
785809

@@ -811,10 +835,25 @@ export function ThemeCatalogSettings({
811835
});
812836
} else {
813837
const nextEnabled = enabledTweakFullUrls.filter((u) => u !== trimmed);
814-
patchSettings({
815-
themeRemoteEnabledTweakFullUrls: nextEnabled,
816-
themeRemoteTweakFavorites: pruneTweakFavorites(tweakFavorites, nextEnabled),
817-
});
838+
if (hint?.pruneUnpinnedFavoriteOnDisable) {
839+
const enabledSet = new Set(nextEnabled);
840+
const inLibraryBeforeThisCatalogVisit =
841+
tweakFavoritesSnapshotAtAppearanceCatalogOpenRef.current;
842+
const nextTweakFavs = tweakFavorites.filter(
843+
(f) =>
844+
f.pinned === true ||
845+
enabledSet.has(f.fullUrl) ||
846+
inLibraryBeforeThisCatalogVisit.has(f.fullUrl.trim())
847+
);
848+
patchSettings({
849+
themeRemoteEnabledTweakFullUrls: nextEnabled,
850+
themeRemoteTweakFavorites: nextTweakFavs,
851+
});
852+
} else {
853+
patchSettings({
854+
themeRemoteEnabledTweakFullUrls: nextEnabled,
855+
});
856+
}
818857
}
819858
},
820859
[enabledTweakFullUrls, patchSettings, prefetchFull, pruneTweakFavorites, tweakFavorites]
@@ -1468,6 +1507,7 @@ export function ThemeCatalogSettings({
14681507
setTweakApplied(row.fullUrl, v, {
14691508
displayName: row.displayName,
14701509
basename: row.basename,
1510+
pruneUnpinnedFavoriteOnDisable: true,
14711511
})
14721512
}
14731513
/>

0 commit comments

Comments
 (0)