mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix catalog filter counts persistence between subplebbits and page refreshes
This commit is contained in:
@@ -56,6 +56,20 @@ const FiltersTable = ({ onSave }: { onSave: () => void }) => {
|
||||
onSave();
|
||||
}, [saveAndApplyFilters, localFilterItems, onSave]);
|
||||
|
||||
const handleKeyDown = useCallback(
|
||||
(e: KeyboardEvent) => {
|
||||
if (e.key === 'Enter') {
|
||||
handleSave();
|
||||
}
|
||||
},
|
||||
[handleSave],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
document.addEventListener('keydown', handleKeyDown);
|
||||
return () => document.removeEventListener('keydown', handleKeyDown);
|
||||
}, [handleKeyDown]);
|
||||
|
||||
const updateLocalFilterItem = useCallback((index: number, item: any) => {
|
||||
setLocalFilterItems((prev) => prev.map((f, i) => (i === index ? item : f)));
|
||||
}, []);
|
||||
|
||||
@@ -95,7 +95,23 @@ const useCatalogFeedRows = (columnCount: number, feed: any, isFeedLoaded: boolea
|
||||
}
|
||||
|
||||
return _feed;
|
||||
}, [accountComments, feed, description, rules, address, isFeedLoaded, createdAt, title, shortAddress, avatarUrl, t, isInAllView, multisub, hideThreadsWithoutImages]);
|
||||
}, [
|
||||
accountComments,
|
||||
feed,
|
||||
description,
|
||||
rules,
|
||||
address,
|
||||
isFeedLoaded,
|
||||
createdAt,
|
||||
title,
|
||||
shortAddress,
|
||||
avatarUrl,
|
||||
t,
|
||||
isInAllView,
|
||||
multisub,
|
||||
hideThreadsWithoutImages,
|
||||
searchText,
|
||||
]);
|
||||
|
||||
const rows = useMemo(() => {
|
||||
const rows = [];
|
||||
|
||||
@@ -59,9 +59,13 @@ const useCatalogFiltersStore = create(
|
||||
const updatedFilterItems = state.filterItems.map((item) => {
|
||||
const newItem = { ...item };
|
||||
|
||||
// Reset count and filteredCids (global counters)
|
||||
newItem.count = 0;
|
||||
newItem.filteredCids = new Set();
|
||||
|
||||
// Ensure subplebbitCounts is a Map
|
||||
if (!newItem.subplebbitCounts || !(newItem.subplebbitCounts instanceof Map)) {
|
||||
newItem.subplebbitCounts = new Map(Array.isArray(newItem.subplebbitCounts) ? newItem.subplebbitCounts : []);
|
||||
newItem.subplebbitCounts = new Map();
|
||||
}
|
||||
|
||||
// Ensure subplebbitFilteredCids is a Map
|
||||
@@ -69,9 +73,11 @@ const useCatalogFiltersStore = create(
|
||||
newItem.subplebbitFilteredCids = new Map();
|
||||
}
|
||||
|
||||
// Now safely check if the address exists in the map
|
||||
if (!newItem.subplebbitCounts.has(address)) {
|
||||
// Only initialize for the new address if it doesn't already exist
|
||||
if (!newItem.subplebbitFilteredCids.has(address)) {
|
||||
newItem.subplebbitFilteredCids.set(address, new Set<string>());
|
||||
}
|
||||
if (!newItem.subplebbitCounts.has(address)) {
|
||||
newItem.subplebbitCounts.set(address, 0);
|
||||
}
|
||||
|
||||
@@ -81,13 +87,14 @@ const useCatalogFiltersStore = create(
|
||||
return {
|
||||
currentSubplebbitAddress: address,
|
||||
filterItems: updatedFilterItems,
|
||||
filteredCount: 0,
|
||||
filteredCount: 0, // This will be recalculated below
|
||||
};
|
||||
}
|
||||
|
||||
return { currentSubplebbitAddress: address };
|
||||
});
|
||||
|
||||
// Recalculate the filtered count for the current subplebbit
|
||||
get().recalcFilteredCount();
|
||||
get().updateFilter();
|
||||
} else {
|
||||
@@ -199,12 +206,14 @@ const useCatalogFiltersStore = create(
|
||||
if (newFilterItems[filterIndex]) {
|
||||
const item = newFilterItems[filterIndex];
|
||||
|
||||
// Ensure subplebbitFilteredCids is a Map
|
||||
const subplebbitFilteredCids = new Map(item.subplebbitFilteredCids);
|
||||
if (!subplebbitFilteredCids.has(subplebbitAddress)) {
|
||||
subplebbitFilteredCids.set(subplebbitAddress, new Set());
|
||||
}
|
||||
const cidSet = subplebbitFilteredCids.get(subplebbitAddress)!;
|
||||
|
||||
// Only increment the count if this CID hasn't been counted for this subplebbit yet
|
||||
if (!cidSet.has(cid)) {
|
||||
const newItemFilteredCids = new Set(item.filteredCids);
|
||||
newItemFilteredCids.add(cid);
|
||||
@@ -274,11 +283,30 @@ const useCatalogFiltersStore = create(
|
||||
enabled: item.enabled,
|
||||
hide: item.hide,
|
||||
top: item.top,
|
||||
subplebbitCounts: Array.from(item.subplebbitCounts || new Map()),
|
||||
subplebbitFilteredCids: Array.from((item.subplebbitFilteredCids || new Map()).entries()).map(([key, value]) => [key, Array.from(value)]),
|
||||
})),
|
||||
} as any;
|
||||
},
|
||||
deserialize: (persisted) => {
|
||||
const persistedObj = typeof persisted === 'string' ? JSON.parse(persisted) : persisted;
|
||||
|
||||
if (persistedObj && persistedObj.filterItems) {
|
||||
return {
|
||||
...persistedObj,
|
||||
filterItems: persistedObj.filterItems.map((item: any) => ({
|
||||
text: item.text,
|
||||
enabled: item.enabled,
|
||||
hide: item.hide,
|
||||
top: item.top,
|
||||
count: 0,
|
||||
filteredCids: new Set<string>(),
|
||||
subplebbitCounts: new Map<string, number>(),
|
||||
subplebbitFilteredCids: new Map<string, Set<string>>(),
|
||||
})),
|
||||
filteredCount: 0,
|
||||
};
|
||||
}
|
||||
return persistedObj || persisted;
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
@@ -461,7 +461,7 @@ const Catalog = () => {
|
||||
<div className={styles.content}>
|
||||
<hr />
|
||||
<div className={styles.catalog}>
|
||||
{processedFeed.length !== 0 ? (
|
||||
{processedFeed?.length !== 0 ? (
|
||||
<>
|
||||
<Virtuoso
|
||||
increaseViewportBy={{ bottom: 1200, top: 1200 }}
|
||||
|
||||
Reference in New Issue
Block a user