Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | 1x 12x 12x 12x 12x 12x 12x 12x 4x 12x 1x 12x 12x 12x 11x 11x 4x 4x 11x 12x 1x 11x 12x | import React, { memo, useEffect } from 'react';
import { ScrollView, Text, View } from 'react-native';
import { AvatarList } from '@repo/ui/components/AvatarList';
import { AvatarSearchOverlay } from '@repo/ui/components/AvatarSearchOverlay';
import { preloadImages } from '@repo/ui/components/CachedImage';
import { AvatarIcon } from '@repo/ui/icons/Avatar';
import { MultipleUserIcon } from '@repo/ui/icons/MultipleUser';
import { makeStyles } from '@repo/ui/themes/makeStyles';
import { useTheme } from '@repo/ui/themes/ThemeContext';
import { textStyles } from '@repo/ui/themes/typography';
import {
useKeyboardPadding,
useSearchOverlayAnimation,
} from '@repo/hooks/useSearchOverlayAnimation';
import { useObserverData } from './hooks/useObserverData';
import { useObserverSelection } from './hooks/useObserverSelection';
import { ObserversSkeleton } from './Skeleton';
const ChooseObserver = () => {
const styles = useStyles();
const { theme } = useTheme();
const { defaultObservers, employeeOptions, isLoading, isError } =
useObserverData();
const {
state: {
form,
isSearchOpen,
searchValue,
searchSelectedIds,
employeesFormatted,
observersFormatted,
},
actions: {
setSearchValue,
openSearch,
handleSearchComplete,
handleToggleAvatarInSearchMode,
handleSubmitSearchInput,
toggleObserver,
},
} = useObserverSelection({ employees: employeeOptions, defaultObservers });
// Preload avatars of observers for performance
useEffect(() => {
const uris: string[] = [];
observersFormatted.forEach(item => {
if (item.uri) uris.push(item.uri);
});
if (uris.length > 0) {
preloadImages(uris);
}
}, [observersFormatted]);
const {
searchInputRef,
isSearchInteractive,
shouldHideBaseContent,
searchOverlayAnimatedStyle,
} = useSearchOverlayAnimation({
isSearchOpen,
});
const keyboardPadding = useKeyboardPadding();
const checkEmployeeSelected = () => {
const lowerSearchValue = searchValue.toLowerCase();
const filteredEmployees = observersFormatted.filter(employee => {
const nameLower = employee.name?.toLowerCase() ?? '';
return nameLower.includes(lowerSearchValue);
});
return filteredEmployees.length > 0;
};
if (isError) {
return (
<View style={styles.errorContainer}>
<Text style={styles.errorText} accessibilityRole="alert">
Something went wrong
</Text>
</View>
);
}
return (
<View style={styles.container}>
<ScrollView
keyboardShouldPersistTaps="always"
nestedScrollEnabled
contentContainerStyle={styles.searchContentContainer}
style={styles.searchScrollView}
>
<View
style={[
styles.content,
shouldHideBaseContent && styles.contentHidden,
]}
pointerEvents={shouldHideBaseContent ? 'none' : 'auto'}
>
{isLoading ? (
<View testID="observer-skeleton">
<ObserversSkeleton />
</View>
) : (
<View style={styles.personGroup}>
<Text style={styles.label} accessibilityRole="header">
OBSERVERS
</Text>
<AvatarList
testID="observers-list"
data={observersFormatted}
selectedIds={form.observers}
onAdd={openSearch}
showAddButton
onToggle={toggleObserver}
numberOfLines={4}
addIconTestID="add-observer-btn"
/>
</View>
)}
</View>
</ScrollView>
<AvatarSearchOverlay
isSearchInteractive={isSearchInteractive}
animatedStyle={searchOverlayAnimatedStyle}
keyboardPadding={keyboardPadding}
searchInputRef={searchInputRef}
isLoading={isLoading}
label="observers"
inputAccessibilityLabel="Search observers by name"
isSearchOpen={isSearchOpen}
searchValue={searchValue}
onChangeSearchValue={setSearchValue}
onSubmitSearchInput={handleSubmitSearchInput}
results={employeesFormatted}
selectedIds={searchSelectedIds}
onToggleResult={handleToggleAvatarInSearchMode}
isAlreadySelected={checkEmployeeSelected()}
noMatchText="This user is already selected as an observer"
noResultsText="No matching people found. Please try a different name"
onDismiss={handleSearchComplete}
dismissAccessibilityLabel="Close search"
leadingIcon={<MultipleUserIcon />}
emptyState={
<View accessible={false}>
<AvatarIcon />
</View>
}
isEmptyStateAccessible={false}
inputBorderColor={theme.colors.background.skyMuted}
/>
</View>
);
};
const useStyles = makeStyles(theme => ({
container: {
flex: 1,
backgroundColor: theme.colors.background.pageMuted,
},
content: {
flex: 1,
gap: theme.metrics.spacing[4],
backgroundColor: theme.colors.background.pageMuted,
},
contentHidden: {
opacity: 0,
},
searchScrollView: {
flexGrow: 1,
},
searchContentContainer: {
paddingHorizontal: theme.metrics.spacing[4],
paddingBottom: theme.metrics.spacing[4],
},
label: {
fontWeight: theme.metrics.fontWeight.semiBold,
textTransform: 'uppercase',
color: theme.colors.text.heading,
...textStyles.content.semiBold,
},
errorContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: theme.colors.background.pageMuted,
},
errorText: {
...textStyles.content.regular,
},
personGroup: {
gap: theme.metrics.spacing[4],
},
}));
export default memo(ChooseObserver);
|