|
| 1 | +import type {CompareItemsCallback, IsItemInSearchCallback, TableColumn, TableData, TableHandle} from '@components/Table'; |
| 2 | +import Table from '@components/Table'; |
| 3 | + |
| 4 | +import useLocalize from '@hooks/useLocalize'; |
| 5 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 6 | + |
| 7 | +import tokenizedSearch from '@libs/tokenizedSearch'; |
| 8 | + |
| 9 | +import variables from '@styles/variables'; |
| 10 | + |
| 11 | +import type * as OnyxCommon from '@src/types/onyx/OnyxCommon'; |
| 12 | + |
| 13 | +import type {ListRenderItemInfo} from '@shopify/flash-list'; |
| 14 | + |
| 15 | +import React from 'react'; |
| 16 | +import {View} from 'react-native'; |
| 17 | + |
| 18 | +import ReportParticipantsTableRow from './ReportParticipantsTableRow'; |
| 19 | + |
| 20 | +type ReportParticipantsTableColumnKey = 'member' | 'role' | 'actions'; |
| 21 | + |
| 22 | +type ReportParticipantRowData = TableData & { |
| 23 | + /** The accountID of the participant */ |
| 24 | + accountID: number; |
| 25 | + |
| 26 | + /** The participant's login (email/phone) */ |
| 27 | + login: string; |
| 28 | + |
| 29 | + /** The participant's display name */ |
| 30 | + name: string; |
| 31 | + |
| 32 | + /** The participant's formatted email/phone shown as the subtitle */ |
| 33 | + email: string; |
| 34 | + |
| 35 | + /** Whether the participant is an admin of the group chat */ |
| 36 | + isAdmin: boolean; |
| 37 | + |
| 38 | + /** Whether the participant list belongs to a group chat (drives the role column & selection) */ |
| 39 | + isGroupChat: boolean; |
| 40 | + |
| 41 | + /** The pending action for the row, used for offline feedback */ |
| 42 | + pendingAction?: OnyxCommon.PendingAction; |
| 43 | + |
| 44 | + /** Callback invoked when the row is pressed (outside of selection mode) */ |
| 45 | + action: () => void; |
| 46 | +}; |
| 47 | + |
| 48 | +type ReportParticipantsTableProps = { |
| 49 | + ref?: React.Ref<TableHandle<ReportParticipantRowData, ReportParticipantsTableColumnKey, string>>; |
| 50 | + |
| 51 | + /** The rows to render in the table */ |
| 52 | + members: ReportParticipantRowData[]; |
| 53 | + |
| 54 | + /** Whether the participant list belongs to a group chat */ |
| 55 | + isGroupChat: boolean; |
| 56 | + |
| 57 | + /** Whether multi-selection is enabled */ |
| 58 | + selectionEnabled: boolean; |
| 59 | + |
| 60 | + /** The list of selected row keys */ |
| 61 | + selectedKeys: string[]; |
| 62 | + |
| 63 | + /** Whether to show the find-member search bar */ |
| 64 | + shouldShowSearchBar: boolean; |
| 65 | + |
| 66 | + /** Callback when the set of selected rows changes */ |
| 67 | + onRowSelectionChange: (selectedRowKeys: string[]) => void; |
| 68 | +}; |
| 69 | + |
| 70 | +export default function ReportParticipantsTable({ref, members, isGroupChat, selectionEnabled, selectedKeys, shouldShowSearchBar, onRowSelectionChange}: ReportParticipantsTableProps) { |
| 71 | + const styles = useThemeStyles(); |
| 72 | + const {translate, localeCompare} = useLocalize(); |
| 73 | + |
| 74 | + const columns: Array<TableColumn<ReportParticipantsTableColumnKey>> = [ |
| 75 | + { |
| 76 | + key: 'member', |
| 77 | + label: translate('common.member'), |
| 78 | + sortable: false, |
| 79 | + }, |
| 80 | + ...(isGroupChat |
| 81 | + ? [ |
| 82 | + { |
| 83 | + key: 'role' as const, |
| 84 | + label: translate('common.role'), |
| 85 | + sortable: false, |
| 86 | + width: variables.workspaceMembersRoleColumnWidth, |
| 87 | + }, |
| 88 | + ] |
| 89 | + : []), |
| 90 | + { |
| 91 | + key: 'actions', |
| 92 | + label: '', |
| 93 | + width: variables.tableCaretColumnWidth, |
| 94 | + sortable: false, |
| 95 | + }, |
| 96 | + ]; |
| 97 | + |
| 98 | + const compareItems: CompareItemsCallback<ReportParticipantRowData, ReportParticipantsTableColumnKey> = (item1, item2) => |
| 99 | + localeCompare(item1.name.toLowerCase(), item2.name.toLowerCase()); |
| 100 | + |
| 101 | + const isItemInSearch: IsItemInSearchCallback<ReportParticipantRowData> = (item, searchValue) => { |
| 102 | + const results = tokenizedSearch([item], searchValue, (option) => [option.name, option.email, option.login]); |
| 103 | + return results.length > 0; |
| 104 | + }; |
| 105 | + |
| 106 | + const renderItem = ({item, index}: ListRenderItemInfo<ReportParticipantRowData>) => ( |
| 107 | + <ReportParticipantsTableRow |
| 108 | + item={item} |
| 109 | + rowIndex={index} |
| 110 | + shouldUseNarrowTableLayout |
| 111 | + /> |
| 112 | + ); |
| 113 | + |
| 114 | + return ( |
| 115 | + <Table |
| 116 | + ref={ref} |
| 117 | + data={members} |
| 118 | + columns={columns} |
| 119 | + selectedKeys={selectedKeys} |
| 120 | + selectionEnabled={selectionEnabled} |
| 121 | + shouldEnableSelectionInNarrowPaneModal |
| 122 | + initialSortColumn="member" |
| 123 | + title={translate('common.members')} |
| 124 | + renderItem={renderItem} |
| 125 | + compareItems={compareItems} |
| 126 | + isItemInSearch={isItemInSearch} |
| 127 | + keyExtractor={(item) => item.keyForList} |
| 128 | + onRowSelectionChange={onRowSelectionChange} |
| 129 | + > |
| 130 | + {shouldShowSearchBar && ( |
| 131 | + <View style={[styles.mb3, styles.mh5]}> |
| 132 | + <Table.SearchBar |
| 133 | + label={translate('selectionList.findMember')} |
| 134 | + style={[styles.mb0, styles.mh0]} |
| 135 | + /> |
| 136 | + </View> |
| 137 | + )} |
| 138 | + <Table.Header /> |
| 139 | + <Table.Body /> |
| 140 | + </Table> |
| 141 | + ); |
| 142 | +} |
| 143 | + |
| 144 | +export type {ReportParticipantsTableColumnKey, ReportParticipantRowData}; |
0 commit comments