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();
|
onSave();
|
||||||
}, [saveAndApplyFilters, localFilterItems, 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) => {
|
const updateLocalFilterItem = useCallback((index: number, item: any) => {
|
||||||
setLocalFilterItems((prev) => prev.map((f, i) => (i === index ? item : f)));
|
setLocalFilterItems((prev) => prev.map((f, i) => (i === index ? item : f)));
|
||||||
}, []);
|
}, []);
|
||||||
|
|||||||
@@ -95,7 +95,23 @@ const useCatalogFeedRows = (columnCount: number, feed: any, isFeedLoaded: boolea
|
|||||||
}
|
}
|
||||||
|
|
||||||
return _feed;
|
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 = useMemo(() => {
|
||||||
const rows = [];
|
const rows = [];
|
||||||
|
|||||||
@@ -59,9 +59,13 @@ const useCatalogFiltersStore = create(
|
|||||||
const updatedFilterItems = state.filterItems.map((item) => {
|
const updatedFilterItems = state.filterItems.map((item) => {
|
||||||
const newItem = { ...item };
|
const newItem = { ...item };
|
||||||
|
|
||||||
|
// Reset count and filteredCids (global counters)
|
||||||
|
newItem.count = 0;
|
||||||
|
newItem.filteredCids = new Set();
|
||||||
|
|
||||||
// Ensure subplebbitCounts is a Map
|
// Ensure subplebbitCounts is a Map
|
||||||
if (!newItem.subplebbitCounts || !(newItem.subplebbitCounts instanceof 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
|
// Ensure subplebbitFilteredCids is a Map
|
||||||
@@ -69,9 +73,11 @@ const useCatalogFiltersStore = create(
|
|||||||
newItem.subplebbitFilteredCids = new Map();
|
newItem.subplebbitFilteredCids = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now safely check if the address exists in the map
|
// Only initialize for the new address if it doesn't already exist
|
||||||
if (!newItem.subplebbitCounts.has(address)) {
|
if (!newItem.subplebbitFilteredCids.has(address)) {
|
||||||
newItem.subplebbitFilteredCids.set(address, new Set<string>());
|
newItem.subplebbitFilteredCids.set(address, new Set<string>());
|
||||||
|
}
|
||||||
|
if (!newItem.subplebbitCounts.has(address)) {
|
||||||
newItem.subplebbitCounts.set(address, 0);
|
newItem.subplebbitCounts.set(address, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,13 +87,14 @@ const useCatalogFiltersStore = create(
|
|||||||
return {
|
return {
|
||||||
currentSubplebbitAddress: address,
|
currentSubplebbitAddress: address,
|
||||||
filterItems: updatedFilterItems,
|
filterItems: updatedFilterItems,
|
||||||
filteredCount: 0,
|
filteredCount: 0, // This will be recalculated below
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return { currentSubplebbitAddress: address };
|
return { currentSubplebbitAddress: address };
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Recalculate the filtered count for the current subplebbit
|
||||||
get().recalcFilteredCount();
|
get().recalcFilteredCount();
|
||||||
get().updateFilter();
|
get().updateFilter();
|
||||||
} else {
|
} else {
|
||||||
@@ -199,12 +206,14 @@ const useCatalogFiltersStore = create(
|
|||||||
if (newFilterItems[filterIndex]) {
|
if (newFilterItems[filterIndex]) {
|
||||||
const item = newFilterItems[filterIndex];
|
const item = newFilterItems[filterIndex];
|
||||||
|
|
||||||
|
// Ensure subplebbitFilteredCids is a Map
|
||||||
const subplebbitFilteredCids = new Map(item.subplebbitFilteredCids);
|
const subplebbitFilteredCids = new Map(item.subplebbitFilteredCids);
|
||||||
if (!subplebbitFilteredCids.has(subplebbitAddress)) {
|
if (!subplebbitFilteredCids.has(subplebbitAddress)) {
|
||||||
subplebbitFilteredCids.set(subplebbitAddress, new Set());
|
subplebbitFilteredCids.set(subplebbitAddress, new Set());
|
||||||
}
|
}
|
||||||
const cidSet = subplebbitFilteredCids.get(subplebbitAddress)!;
|
const cidSet = subplebbitFilteredCids.get(subplebbitAddress)!;
|
||||||
|
|
||||||
|
// Only increment the count if this CID hasn't been counted for this subplebbit yet
|
||||||
if (!cidSet.has(cid)) {
|
if (!cidSet.has(cid)) {
|
||||||
const newItemFilteredCids = new Set(item.filteredCids);
|
const newItemFilteredCids = new Set(item.filteredCids);
|
||||||
newItemFilteredCids.add(cid);
|
newItemFilteredCids.add(cid);
|
||||||
@@ -274,11 +283,30 @@ const useCatalogFiltersStore = create(
|
|||||||
enabled: item.enabled,
|
enabled: item.enabled,
|
||||||
hide: item.hide,
|
hide: item.hide,
|
||||||
top: item.top,
|
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;
|
} 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}>
|
<div className={styles.content}>
|
||||||
<hr />
|
<hr />
|
||||||
<div className={styles.catalog}>
|
<div className={styles.catalog}>
|
||||||
{processedFeed.length !== 0 ? (
|
{processedFeed?.length !== 0 ? (
|
||||||
<>
|
<>
|
||||||
<Virtuoso
|
<Virtuoso
|
||||||
increaseViewportBy={{ bottom: 1200, top: 1200 }}
|
increaseViewportBy={{ bottom: 1200, top: 1200 }}
|
||||||
|
|||||||
Reference in New Issue
Block a user