|
| 1 | +import { type FC, useContext, useEffect, useState } from 'react'; |
| 2 | + |
| 3 | +import { |
| 4 | + CheckCircleFillIcon, |
| 5 | + NoEntryFillIcon, |
| 6 | + OrganizationIcon, |
| 7 | +} from '@primer/octicons-react'; |
| 8 | +import { Box, Stack, Text, TextInputWithTokens } from '@primer/react'; |
| 9 | + |
| 10 | +import { AppContext } from '../../context/App'; |
| 11 | +import { IconColor, type Organization } from '../../types'; |
| 12 | +import { |
| 13 | + hasExcludeOrganizationFilters, |
| 14 | + hasIncludeOrganizationFilters, |
| 15 | +} from '../../utils/notifications/filters/organizations'; |
| 16 | +import { Tooltip } from '../fields/Tooltip'; |
| 17 | +import { Title } from '../primitives/Title'; |
| 18 | + |
| 19 | +type InputToken = { |
| 20 | + id: number; |
| 21 | + text: string; |
| 22 | +}; |
| 23 | + |
| 24 | +const tokenEvents = ['Enter', 'Tab', ' ', ',']; |
| 25 | + |
| 26 | +export const OrganizationFilter: FC = () => { |
| 27 | + const { updateFilter, settings } = useContext(AppContext); |
| 28 | + |
| 29 | + // biome-ignore lint/correctness/useExhaustiveDependencies: we only want to run this effect on organization filter changes |
| 30 | + useEffect(() => { |
| 31 | + if (!hasIncludeOrganizationFilters(settings)) { |
| 32 | + setIncludeOrganizations([]); |
| 33 | + } |
| 34 | + |
| 35 | + if (!hasExcludeOrganizationFilters(settings)) { |
| 36 | + setExcludeOrganizations([]); |
| 37 | + } |
| 38 | + }, [ |
| 39 | + settings.filterIncludeOrganizations, |
| 40 | + settings.filterExcludeOrganizations, |
| 41 | + ]); |
| 42 | + |
| 43 | + const mapValuesToTokens = (values: string[]): InputToken[] => { |
| 44 | + return values.map((value, index) => ({ |
| 45 | + id: index, |
| 46 | + text: value, |
| 47 | + })); |
| 48 | + }; |
| 49 | + |
| 50 | + const [includeOrganizations, setIncludeOrganizations] = useState< |
| 51 | + InputToken[] |
| 52 | + >(mapValuesToTokens(settings.filterIncludeOrganizations)); |
| 53 | + |
| 54 | + const addIncludeOrganizationsToken = ( |
| 55 | + event: |
| 56 | + | React.KeyboardEvent<HTMLInputElement> |
| 57 | + | React.FocusEvent<HTMLInputElement>, |
| 58 | + ) => { |
| 59 | + const value = (event.target as HTMLInputElement).value.trim(); |
| 60 | + |
| 61 | + if ( |
| 62 | + value.length > 0 && |
| 63 | + !includeOrganizations.some((v) => v.text === value) |
| 64 | + ) { |
| 65 | + setIncludeOrganizations([ |
| 66 | + ...includeOrganizations, |
| 67 | + { id: includeOrganizations.length, text: value }, |
| 68 | + ]); |
| 69 | + updateFilter('filterIncludeOrganizations', value as Organization, true); |
| 70 | + |
| 71 | + (event.target as HTMLInputElement).value = ''; |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + const removeIncludeOrganizationToken = (tokenId: string | number) => { |
| 76 | + const value = |
| 77 | + includeOrganizations.find((v) => v.id === tokenId)?.text || ''; |
| 78 | + updateFilter('filterIncludeOrganizations', value as Organization, false); |
| 79 | + |
| 80 | + setIncludeOrganizations( |
| 81 | + includeOrganizations.filter((v) => v.id !== tokenId), |
| 82 | + ); |
| 83 | + }; |
| 84 | + |
| 85 | + const includeOrganizationsKeyDown = ( |
| 86 | + event: React.KeyboardEvent<HTMLInputElement>, |
| 87 | + ) => { |
| 88 | + if (tokenEvents.includes(event.key)) { |
| 89 | + addIncludeOrganizationsToken(event); |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + const [excludeOrganizations, setExcludeOrganizations] = useState< |
| 94 | + InputToken[] |
| 95 | + >(mapValuesToTokens(settings.filterExcludeOrganizations)); |
| 96 | + |
| 97 | + const addExcludeOrganizationsToken = ( |
| 98 | + event: |
| 99 | + | React.KeyboardEvent<HTMLInputElement> |
| 100 | + | React.FocusEvent<HTMLInputElement>, |
| 101 | + ) => { |
| 102 | + const value = (event.target as HTMLInputElement).value.trim(); |
| 103 | + |
| 104 | + if ( |
| 105 | + value.length > 0 && |
| 106 | + !excludeOrganizations.some((v) => v.text === value) |
| 107 | + ) { |
| 108 | + setExcludeOrganizations([ |
| 109 | + ...excludeOrganizations, |
| 110 | + { id: excludeOrganizations.length, text: value }, |
| 111 | + ]); |
| 112 | + updateFilter('filterExcludeOrganizations', value as Organization, true); |
| 113 | + |
| 114 | + (event.target as HTMLInputElement).value = ''; |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + const removeExcludeOrganizationToken = (tokenId: string | number) => { |
| 119 | + const value = |
| 120 | + excludeOrganizations.find((v) => v.id === tokenId)?.text || ''; |
| 121 | + updateFilter('filterExcludeOrganizations', value as Organization, false); |
| 122 | + |
| 123 | + setExcludeOrganizations( |
| 124 | + excludeOrganizations.filter((v) => v.id !== tokenId), |
| 125 | + ); |
| 126 | + }; |
| 127 | + |
| 128 | + const excludeOrganizationsKeyDown = ( |
| 129 | + event: React.KeyboardEvent<HTMLInputElement>, |
| 130 | + ) => { |
| 131 | + if (tokenEvents.includes(event.key)) { |
| 132 | + addExcludeOrganizationsToken(event); |
| 133 | + } |
| 134 | + }; |
| 135 | + |
| 136 | + return ( |
| 137 | + <fieldset id="filter-organizations"> |
| 138 | + <Stack direction="horizontal" gap="condensed" align="baseline"> |
| 139 | + <Title icon={OrganizationIcon}>Organizations</Title> |
| 140 | + <Tooltip |
| 141 | + name="tooltip-filter-organizations" |
| 142 | + tooltip={ |
| 143 | + <Stack direction="vertical" gap="condensed"> |
| 144 | + <Text>Filter notifications by organization.</Text> |
| 145 | + </Stack> |
| 146 | + } |
| 147 | + /> |
| 148 | + </Stack> |
| 149 | + <Stack direction="vertical" gap="condensed"> |
| 150 | + <Stack |
| 151 | + direction="horizontal" |
| 152 | + gap="condensed" |
| 153 | + align="center" |
| 154 | + className="text-sm" |
| 155 | + > |
| 156 | + <Box className="font-medium text-gitify-font w-28"> |
| 157 | + <Stack direction="horizontal" gap="condensed" align="center"> |
| 158 | + <CheckCircleFillIcon className={IconColor.GREEN} /> |
| 159 | + <Text>Include:</Text> |
| 160 | + </Stack> |
| 161 | + </Box> |
| 162 | + <TextInputWithTokens |
| 163 | + title="Include organizations" |
| 164 | + tokens={includeOrganizations} |
| 165 | + onTokenRemove={removeIncludeOrganizationToken} |
| 166 | + onKeyDown={includeOrganizationsKeyDown} |
| 167 | + onBlur={addIncludeOrganizationsToken} |
| 168 | + size="small" |
| 169 | + disabled={ |
| 170 | + !settings.detailedNotifications || |
| 171 | + hasExcludeOrganizationFilters(settings) |
| 172 | + } |
| 173 | + block |
| 174 | + /> |
| 175 | + </Stack> |
| 176 | + |
| 177 | + <Stack |
| 178 | + direction="horizontal" |
| 179 | + gap="condensed" |
| 180 | + align="center" |
| 181 | + className="text-sm" |
| 182 | + > |
| 183 | + <Box className="font-medium text-gitify-font w-28"> |
| 184 | + <Stack direction="horizontal" gap="condensed" align="center"> |
| 185 | + <NoEntryFillIcon className={IconColor.RED} /> |
| 186 | + <Text>Exclude:</Text> |
| 187 | + </Stack> |
| 188 | + </Box> |
| 189 | + <TextInputWithTokens |
| 190 | + title="Exclude organizations" |
| 191 | + tokens={excludeOrganizations} |
| 192 | + onTokenRemove={removeExcludeOrganizationToken} |
| 193 | + onKeyDown={excludeOrganizationsKeyDown} |
| 194 | + onBlur={addExcludeOrganizationsToken} |
| 195 | + size="small" |
| 196 | + disabled={ |
| 197 | + !settings.detailedNotifications || |
| 198 | + hasIncludeOrganizationFilters(settings) |
| 199 | + } |
| 200 | + block |
| 201 | + /> |
| 202 | + </Stack> |
| 203 | + </Stack> |
| 204 | + </fieldset> |
| 205 | + ); |
| 206 | +}; |
0 commit comments