-
Notifications
You must be signed in to change notification settings - Fork 1.5k
chore: migrate DirectoryView to Hooks #7359
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5f2088f
chore: unify search announcement results for a11y
OtavioStasiak a06b22f
chore: create useDirectorySearch hook
OtavioStasiak 2491c21
feat: migrate directoryView to hooks
OtavioStasiak 95ed5a8
code improvements
OtavioStasiak c31d760
fix: tests
OtavioStasiak 5d14cf2
Merge branch 'develop' into chore.migrate-directoryview-to-hooks
OtavioStasiak 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
13 changes: 13 additions & 0 deletions
13
app/lib/methods/helpers/announceSearchResultsForAccessibility.ts
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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { AccessibilityInfo } from 'react-native'; | ||
|
|
||
| import I18n from '../../../i18n'; | ||
|
|
||
| export const announceSearchResultsForAccessibility = (count: number): void => { | ||
| if (count < 1) { | ||
| AccessibilityInfo.announceForAccessibility(I18n.t('No_results_found')); | ||
| return; | ||
| } | ||
|
|
||
| const message = count === 1 ? I18n.t('One_result_found') : I18n.t('Search_Results_found', { count: count.toString() }); | ||
| AccessibilityInfo.announceForAccessibility(message); | ||
| }; |
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
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 |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { useEffect, useState } from 'react'; | ||
|
|
||
| import { type IServerRoom } from '../../../definitions'; | ||
| import { announceSearchResultsForAccessibility } from '../../../lib/methods/helpers/announceSearchResultsForAccessibility'; | ||
| import { useDebounce } from '../../../lib/methods/helpers/debounce'; | ||
| import log, { events, logEvent } from '../../../lib/methods/helpers/log'; | ||
| import { getDirectory } from '../../../lib/services/restApi'; | ||
|
|
||
| export const useDirectorySearch = (directoryDefaultView: string) => { | ||
| 'use memo'; | ||
|
|
||
| const [data, setData] = useState<IServerRoom[]>([]); | ||
| const [loading, setLoading] = useState(false); | ||
| const [text, setText] = useState(''); | ||
| const [total, setTotal] = useState(-1); | ||
| const [globalUsers, setGlobalUsers] = useState(true); | ||
| const [type, setType] = useState(directoryDefaultView); | ||
|
|
||
| // useDebounce keeps a ref to the latest callback, so this always reads fresh state | ||
| const load = useDebounce(async ({ newSearch = false }: { newSearch?: boolean } = {}) => { | ||
| if (!newSearch && (loading || data.length === total)) { | ||
| return; | ||
| } | ||
|
|
||
| if (newSearch) { | ||
| setData([]); | ||
| setTotal(-1); | ||
| } | ||
| setLoading(true); | ||
|
|
||
| try { | ||
| const directories = await getDirectory({ | ||
| text, | ||
| type, | ||
| workspace: globalUsers ? 'all' : 'local', | ||
| offset: newSearch ? 0 : data.length, | ||
| count: 50, | ||
| sort: type === 'users' ? { username: 1 } : { usersCount: -1 } | ||
| }); | ||
| if (directories.success) { | ||
| setData(prev => [...(newSearch ? [] : prev), ...(directories.result as IServerRoom[])]); | ||
| setTotal(directories.total); | ||
| setLoading(false); | ||
| // Announce the full total on a fresh search; loadMore pages shouldn't re-announce | ||
| if (newSearch) { | ||
| announceSearchResultsForAccessibility(directories.total); | ||
| } | ||
| } else { | ||
| setLoading(false); | ||
| } | ||
| } catch (e) { | ||
| log(e); | ||
| setLoading(false); | ||
| } | ||
| }, 200); | ||
|
|
||
| const search = () => load({ newSearch: true }); | ||
| const loadMore = () => load({}); | ||
|
|
||
| const onSearchChangeText = (newText: string) => { | ||
| setText(newText); | ||
| search(); | ||
| }; | ||
|
|
||
| const changeType = (newType: string) => { | ||
| setType(newType); | ||
|
|
||
| if (newType === 'users') { | ||
| logEvent(events.DIRECTORY_SEARCH_USERS); | ||
| } else if (newType === 'channels') { | ||
| logEvent(events.DIRECTORY_SEARCH_CHANNELS); | ||
| } else if (newType === 'teams') { | ||
| logEvent(events.DIRECTORY_SEARCH_TEAMS); | ||
| } | ||
|
|
||
| search(); | ||
| }; | ||
|
|
||
| const toggleWorkspace = () => { | ||
| setGlobalUsers(prev => !prev); | ||
| search(); | ||
| }; | ||
|
|
||
| // Run the initial search when the hook mounts; `search` is stable, so this fires once | ||
| useEffect(() => { | ||
| search(); | ||
| }, []); | ||
|
|
||
| return { | ||
| data, | ||
| loading, | ||
| type, | ||
| globalUsers, | ||
| search, | ||
| loadMore, | ||
| onSearchChangeText, | ||
| changeType, | ||
| toggleWorkspace | ||
| }; | ||
| }; | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.