mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix catalog filter count per subplebbit address
This commit is contained in:
@@ -5,7 +5,7 @@ import styles from './catalog-filters.module.css';
|
|||||||
|
|
||||||
const FiltersTable = ({ onSave }: { onSave: () => void }) => {
|
const FiltersTable = ({ onSave }: { onSave: () => void }) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { filterItems, saveAndApplyFilters } = useCatalogFiltersStore();
|
const { filterItems, saveAndApplyFilters, currentSubplebbitAddress } = useCatalogFiltersStore();
|
||||||
|
|
||||||
const [localFilterItems, setLocalFilterItems] = useState(
|
const [localFilterItems, setLocalFilterItems] = useState(
|
||||||
filterItems.map((item) => ({
|
filterItems.map((item) => ({
|
||||||
@@ -127,7 +127,9 @@ const FiltersTable = ({ onSave }: { onSave: () => void }) => {
|
|||||||
×
|
×
|
||||||
</span>
|
</span>
|
||||||
</td>
|
</td>
|
||||||
<td className={styles.filterHits}>{item.count > 0 && `x${item.count}`}</td>
|
<td className={styles.filterHits}>
|
||||||
|
{currentSubplebbitAddress && (item.subplebbitCounts?.get(currentSubplebbitAddress) ?? 0) > 0 && `x${item.subplebbitCounts?.get(currentSubplebbitAddress)}`}
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
))}
|
))}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|||||||
@@ -50,7 +50,50 @@ const useCatalogFiltersStore = create(
|
|||||||
filteredCount: 0,
|
filteredCount: 0,
|
||||||
filteredCids: new Set<string>(),
|
filteredCids: new Set<string>(),
|
||||||
currentSubplebbitAddress: null,
|
currentSubplebbitAddress: null,
|
||||||
setCurrentSubplebbitAddress: (address: string | null) => set({ currentSubplebbitAddress: address }),
|
setCurrentSubplebbitAddress: (address: string | null) => {
|
||||||
|
const prevAddress = get().currentSubplebbitAddress;
|
||||||
|
|
||||||
|
if (address !== prevAddress) {
|
||||||
|
set((state) => {
|
||||||
|
if (address) {
|
||||||
|
const updatedFilterItems = state.filterItems.map((item) => {
|
||||||
|
const newItem = { ...item };
|
||||||
|
|
||||||
|
// Ensure subplebbitCounts is a Map
|
||||||
|
if (!newItem.subplebbitCounts || !(newItem.subplebbitCounts instanceof Map)) {
|
||||||
|
newItem.subplebbitCounts = new Map(Array.isArray(newItem.subplebbitCounts) ? newItem.subplebbitCounts : []);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure subplebbitFilteredCids is a Map
|
||||||
|
if (!newItem.subplebbitFilteredCids || !(newItem.subplebbitFilteredCids instanceof Map)) {
|
||||||
|
newItem.subplebbitFilteredCids = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now safely check if the address exists in the map
|
||||||
|
if (!newItem.subplebbitCounts.has(address)) {
|
||||||
|
newItem.subplebbitFilteredCids.set(address, new Set<string>());
|
||||||
|
newItem.subplebbitCounts.set(address, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return newItem;
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
currentSubplebbitAddress: address,
|
||||||
|
filterItems: updatedFilterItems,
|
||||||
|
filteredCount: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return { currentSubplebbitAddress: address };
|
||||||
|
});
|
||||||
|
|
||||||
|
get().recalcFilteredCount();
|
||||||
|
get().updateFilter();
|
||||||
|
} else {
|
||||||
|
set({ currentSubplebbitAddress: address });
|
||||||
|
}
|
||||||
|
},
|
||||||
searchText: '',
|
searchText: '',
|
||||||
setSearchFilter: (text: string) => {
|
setSearchFilter: (text: string) => {
|
||||||
set({ searchText: text });
|
set({ searchText: text });
|
||||||
@@ -100,6 +143,9 @@ const useCatalogFiltersStore = create(
|
|||||||
filter: (comment: Comment) => {
|
filter: (comment: Comment) => {
|
||||||
if (!comment?.cid) return true;
|
if (!comment?.cid) return true;
|
||||||
|
|
||||||
|
const currentSubplebbit = state.currentSubplebbitAddress;
|
||||||
|
|
||||||
|
// Apply search filter
|
||||||
if (state.searchText.trim() !== '') {
|
if (state.searchText.trim() !== '') {
|
||||||
const searchPattern = state.searchText.toLowerCase();
|
const searchPattern = state.searchText.toLowerCase();
|
||||||
const title = comment?.title?.toLowerCase() || '';
|
const title = comment?.title?.toLowerCase() || '';
|
||||||
@@ -110,21 +156,36 @@ const useCatalogFiltersStore = create(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply content filters
|
||||||
const { filterItems } = state;
|
const { filterItems } = state;
|
||||||
let shouldHide = false;
|
let shouldHide = false;
|
||||||
|
|
||||||
for (let i = 0; i < filterItems.length; i++) {
|
for (let i = 0; i < filterItems.length; i++) {
|
||||||
const item = filterItems[i];
|
const item = filterItems[i];
|
||||||
if (item.enabled && item.text.trim() !== '') {
|
if (item.enabled && item.text.trim() !== '') {
|
||||||
const pattern = item.text.toLowerCase();
|
const pattern = item.text.toLowerCase();
|
||||||
const title = comment?.title?.toLowerCase() || '';
|
const title = comment?.title?.toLowerCase() || '';
|
||||||
const content = comment?.content?.toLowerCase() || '';
|
const content = comment?.content?.toLowerCase() || '';
|
||||||
|
|
||||||
if (title.includes(pattern) || content.includes(pattern)) {
|
if (title.includes(pattern) || content.includes(pattern)) {
|
||||||
|
// If we have a current subplebbit and this is a match, increment the count
|
||||||
|
if (currentSubplebbit && comment.subplebbitAddress === currentSubplebbit) {
|
||||||
|
// We need to use a timeout to avoid modifying state during a state update
|
||||||
|
setTimeout(() => {
|
||||||
|
const filterIndex = filterItems.findIndex((f) => f.text === item.text && f.enabled);
|
||||||
|
if (filterIndex !== -1) {
|
||||||
|
get().incrementFilterCount(filterIndex, comment.cid, comment.subplebbitAddress);
|
||||||
|
}
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
|
||||||
if (item.hide) {
|
if (item.hide) {
|
||||||
shouldHide = true;
|
shouldHide = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return !shouldHide;
|
return !shouldHide;
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|||||||
Reference in New Issue
Block a user