-
Notifications
You must be signed in to change notification settings - Fork 5
fix: search ref problem #1602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix: search ref problem #1602
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,17 @@ export default function SearchBox ({ address, myVotedReferendaIndexes, referenda | |
}; | ||
}), []); | ||
|
||
const applySearchFilter = useCallback((referendaToFilter: LatestReferenda[], keyword: string) => { | ||
const filtered = referendaToFilter.filter((r) => | ||
(filter.advanced.refIndex && String(r.post_id) === keyword) || | ||
(filter.advanced.titles && r.title && r.title.toLowerCase().includes(keyword.toLowerCase())) || | ||
(filter.advanced.methods && r.method && r.method.toLowerCase().includes(keyword.toLowerCase())) || | ||
(filter.advanced.proposers && r.proposer === keyword) | ||
); | ||
|
||
return filtered; | ||
}, [filter.advanced.methods, filter.advanced.proposers, filter.advanced.refIndex, filter.advanced.titles]); | ||
|
||
const onAdvanced = useCallback(() => { | ||
setShowAdvanced(!showAdvanced); | ||
}, [showAdvanced]); | ||
|
@@ -79,27 +90,8 @@ export default function SearchBox ({ address, myVotedReferendaIndexes, referenda | |
}, []); | ||
|
||
const onSearch = useCallback((keyword: string) => { | ||
if (!referenda) { | ||
return; | ||
} | ||
|
||
if (!keyword) { | ||
return setFilteredReferenda([...referenda]); | ||
} | ||
|
||
keyword = keyword.trim(); | ||
|
||
setSearchKeyword(keyword); | ||
|
||
const _filtered = referenda?.filter((r) => | ||
(filter.advanced.refIndex && String(r.post_id) === keyword) || | ||
(filter.advanced.titles && r.title && r.title.toLowerCase().includes(keyword.toLowerCase())) || | ||
(filter.advanced.methods && r.method && r.method.toLowerCase().includes(keyword.toLowerCase())) || | ||
(filter.advanced.proposers && r.proposer === keyword) | ||
); | ||
|
||
setFilteredReferenda([..._filtered]); | ||
}, [filter, referenda, setFilteredReferenda]); | ||
}, []); | ||
|
||
const onMyReferenda = useCallback(() => { | ||
filter.myReferenda = !filter.myReferenda; | ||
|
@@ -130,20 +122,17 @@ export default function SearchBox ({ address, myVotedReferendaIndexes, referenda | |
filtered = filtered.filter((r) => filter.status.includes(r.status)); | ||
} | ||
|
||
if (searchKeyword) { | ||
filtered = applySearchFilter(filtered, searchKeyword); | ||
} | ||
|
||
Comment on lines
+125
to
+128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider optimizing Set conversion The current implementation correctly handles search filtering, but the Set conversion for removing duplicates could be optimized. - // to remove duplicates
- const uniqueFiltered = [...new Set(filtered)];
+ // to remove duplicates using post_id as unique identifier
+ const uniqueFiltered = Array.from(
+ new Map(filtered.map(item => [item.post_id, item])).values()
+ ); This approach is more efficient as it:
Also applies to: 132-132 |
||
// to remove duplicates | ||
const uniqueFiltered = [...new Set(filtered)]; | ||
|
||
const isAnyFilterOn = filter.myReferenda || filter.myVotes || !filter.status.includes('All'); | ||
const isAnyFilterOn = filter.myReferenda || filter.myVotes || !filter.status.includes('All') || searchKeyword; | ||
|
||
setFilteredReferenda(isAnyFilterOn ? uniqueFiltered : referenda); | ||
}, [filter, formatted, myVotedReferendaIndexes, referenda, setFilteredReferenda]); | ||
|
||
useEffect(() => { | ||
/** To re-apply search keyword on referenda loadings */ | ||
if (searchKeyword) { | ||
onSearch(searchKeyword); | ||
} | ||
}, [onSearch, referenda, searchKeyword]); | ||
}, [applySearchFilter, filter, formatted, myVotedReferendaIndexes, referenda, searchKeyword, setFilteredReferenda]); | ||
|
||
const onChangeStatus = useCallback((s: number) => { | ||
s = String(s) === 'All' ? 0 : s; | ||
|
@@ -185,7 +174,7 @@ export default function SearchBox ({ address, myVotedReferendaIndexes, referenda | |
} | ||
</Grid> | ||
</Grid> | ||
<Grid alignItems='center' container item justifyContent='flex-start' py='10px' sx={{ color: theme.palette.mode === 'light' ? 'secondary.main' : 'text.primary', cursor: 'pointer', textDecorationLine: 'underline', width: 'fit-content' }} > | ||
<Grid alignItems='center' container item justifyContent='flex-start' py='10px' sx={{ color: theme.palette.mode === 'light' ? 'secondary.main' : 'text.primary', cursor: 'pointer', textDecorationLine: 'underline', width: 'fit-content' }}> | ||
<Checkbox2 | ||
checked={filter.myReferenda} | ||
disabled={!referenda} | ||
|
@@ -194,7 +183,7 @@ export default function SearchBox ({ address, myVotedReferendaIndexes, referenda | |
onChange={onMyReferenda} | ||
/> | ||
</Grid> | ||
<Grid alignItems='center' container item justifyContent='flex-start' py='10px' sx={{ color: theme.palette.mode === 'light' ? 'secondary.main' : 'text.primary', cursor: 'pointer', textDecorationLine: 'underline', width: 'fit-content' }} > | ||
<Grid alignItems='center' container item justifyContent='flex-start' py='10px' sx={{ color: theme.palette.mode === 'light' ? 'secondary.main' : 'text.primary', cursor: 'pointer', textDecorationLine: 'underline', width: 'fit-content' }}> | ||
<Checkbox2 | ||
checked={filter.myVotes} | ||
disabled={!myVotedReferendaIndexes} | ||
|
@@ -241,7 +230,7 @@ export default function SearchBox ({ address, myVotedReferendaIndexes, referenda | |
onChange={() => onFilter('methods')} | ||
/> | ||
</Grid> | ||
<Grid item onClick={onReset} sx={{ color: 'secondary.light', cursor: 'pointer', textDecorationLine: 'underline', ml: '22px' }}> | ||
<Grid item onClick={onReset} sx={{ color: 'secondary.light', cursor: 'pointer', ml: '22px', textDecorationLine: 'underline' }}> | ||
{t('Reset')} | ||
</Grid> | ||
</Grid> | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve search filter robustness
The filtering logic could be made more robust with the following improvements:
📝 Committable suggestion