-
Notifications
You must be signed in to change notification settings - Fork 285
feat: added the organization filter #2174
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 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0305dc4
added the organization filter
vallieres e70f9b4
Merge branch 'main' into add-organization-filters
vallieres 56b8859
case insensitive orgs, moved filter code, removed detailed notif warning
vallieres e5c5d03
Merge branch 'add-organization-filters' of github.com:vallieres/gitif…
vallieres 61e1ac7
small tweaks
setchy 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
76 changes: 76 additions & 0 deletions
76
src/renderer/components/filters/OrganizationFilter.test.tsx
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,76 @@ | ||
| import { fireEvent, render, screen } from '@testing-library/react'; | ||
|
|
||
| import { mockSettings } from '../../__mocks__/state-mocks'; | ||
| import { AppContext } from '../../context/App'; | ||
| import { OrganizationFilter } from './OrganizationFilter'; | ||
|
|
||
| const mockUpdateFilter = jest.fn(); | ||
|
|
||
| describe('components/filters/OrganizationFilter.tsx', () => { | ||
| beforeEach(() => { | ||
| mockUpdateFilter.mockReset(); | ||
| }); | ||
|
|
||
| it('should render itself & its children', () => { | ||
| const props = { | ||
| updateFilter: mockUpdateFilter, | ||
| settings: mockSettings, | ||
| }; | ||
|
|
||
| render( | ||
| <AppContext.Provider value={props}> | ||
| <OrganizationFilter /> | ||
| </AppContext.Provider>, | ||
| ); | ||
|
|
||
| expect(screen.getByText('Organizations')).toBeInTheDocument(); | ||
| expect(screen.getByText('Include:')).toBeInTheDocument(); | ||
| expect(screen.getByText('Exclude:')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should handle organization includes', () => { | ||
| const props = { | ||
| updateFilter: mockUpdateFilter, | ||
| settings: mockSettings, | ||
| }; | ||
|
|
||
| render( | ||
| <AppContext.Provider value={props}> | ||
| <OrganizationFilter /> | ||
| </AppContext.Provider>, | ||
| ); | ||
|
|
||
| const includeInput = screen.getByTitle('Include organizations'); | ||
| fireEvent.change(includeInput, { target: { value: 'microsoft' } }); | ||
| fireEvent.blur(includeInput); | ||
|
|
||
| expect(mockUpdateFilter).toHaveBeenCalledWith( | ||
| 'filterIncludeOrganizations', | ||
| 'microsoft', | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| it('should handle organization excludes', () => { | ||
| const props = { | ||
| updateFilter: mockUpdateFilter, | ||
| settings: mockSettings, | ||
| }; | ||
|
|
||
| render( | ||
| <AppContext.Provider value={props}> | ||
| <OrganizationFilter /> | ||
| </AppContext.Provider>, | ||
| ); | ||
|
|
||
| const excludeInput = screen.getByTitle('Exclude organizations'); | ||
| fireEvent.change(excludeInput, { target: { value: 'github' } }); | ||
| fireEvent.blur(excludeInput); | ||
|
|
||
| expect(mockUpdateFilter).toHaveBeenCalledWith( | ||
| 'filterExcludeOrganizations', | ||
| 'github', | ||
| true, | ||
| ); | ||
| }); | ||
| }); |
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,208 @@ | ||
| import { type FC, useContext, useEffect, useState } from 'react'; | ||
|
|
||
| import { | ||
| CheckCircleFillIcon, | ||
| NoEntryFillIcon, | ||
| OrganizationIcon, | ||
| } from '@primer/octicons-react'; | ||
| import { Box, Stack, Text, TextInputWithTokens } from '@primer/react'; | ||
|
|
||
| import { AppContext } from '../../context/App'; | ||
| import { IconColor, type Organization } from '../../types'; | ||
| import { | ||
| hasExcludeOrganizationFilters, | ||
| hasIncludeOrganizationFilters, | ||
| } from '../../utils/notifications/filters/organizations'; | ||
| import { Tooltip } from '../fields/Tooltip'; | ||
| import { Title } from '../primitives/Title'; | ||
| import { RequiresDetailedNotificationWarning } from './RequiresDetailedNotificationsWarning'; | ||
|
|
||
| type InputToken = { | ||
| id: number; | ||
| text: string; | ||
| }; | ||
|
|
||
| const tokenEvents = ['Enter', 'Tab', ' ', ',']; | ||
|
|
||
| export const OrganizationFilter: FC = () => { | ||
| const { updateFilter, settings } = useContext(AppContext); | ||
|
|
||
| // biome-ignore lint/correctness/useExhaustiveDependencies: we only want to run this effect on organization filter changes | ||
| useEffect(() => { | ||
| if (!hasIncludeOrganizationFilters(settings)) { | ||
| setIncludeOrganizations([]); | ||
| } | ||
|
|
||
| if (!hasExcludeOrganizationFilters(settings)) { | ||
| setExcludeOrganizations([]); | ||
| } | ||
| }, [ | ||
| settings.filterIncludeOrganizations, | ||
| settings.filterExcludeOrganizations, | ||
| ]); | ||
|
|
||
| const mapValuesToTokens = (values: string[]): InputToken[] => { | ||
| return values.map((value, index) => ({ | ||
| id: index, | ||
| text: value, | ||
| })); | ||
| }; | ||
|
|
||
| const [includeOrganizations, setIncludeOrganizations] = useState< | ||
| InputToken[] | ||
| >(mapValuesToTokens(settings.filterIncludeOrganizations)); | ||
|
|
||
| const addIncludeOrganizationsToken = ( | ||
| event: | ||
| | React.KeyboardEvent<HTMLInputElement> | ||
| | React.FocusEvent<HTMLInputElement>, | ||
| ) => { | ||
| const value = (event.target as HTMLInputElement).value.trim(); | ||
|
|
||
| if ( | ||
| value.length > 0 && | ||
| !includeOrganizations.some((v) => v.text === value) | ||
| ) { | ||
| setIncludeOrganizations([ | ||
| ...includeOrganizations, | ||
| { id: includeOrganizations.length, text: value }, | ||
| ]); | ||
| updateFilter('filterIncludeOrganizations', value as Organization, true); | ||
|
|
||
| (event.target as HTMLInputElement).value = ''; | ||
| } | ||
| }; | ||
|
|
||
| const removeIncludeOrganizationToken = (tokenId: string | number) => { | ||
| const value = | ||
| includeOrganizations.find((v) => v.id === tokenId)?.text || ''; | ||
| updateFilter('filterIncludeOrganizations', value as Organization, false); | ||
|
|
||
| setIncludeOrganizations( | ||
| includeOrganizations.filter((v) => v.id !== tokenId), | ||
| ); | ||
| }; | ||
|
|
||
| const includeOrganizationsKeyDown = ( | ||
| event: React.KeyboardEvent<HTMLInputElement>, | ||
| ) => { | ||
| if (tokenEvents.includes(event.key)) { | ||
| addIncludeOrganizationsToken(event); | ||
| } | ||
| }; | ||
|
|
||
| const [excludeOrganizations, setExcludeOrganizations] = useState< | ||
| InputToken[] | ||
| >(mapValuesToTokens(settings.filterExcludeOrganizations)); | ||
|
|
||
| const addExcludeOrganizationsToken = ( | ||
| event: | ||
| | React.KeyboardEvent<HTMLInputElement> | ||
| | React.FocusEvent<HTMLInputElement>, | ||
| ) => { | ||
| const value = (event.target as HTMLInputElement).value.trim(); | ||
|
|
||
| if ( | ||
| value.length > 0 && | ||
| !excludeOrganizations.some((v) => v.text === value) | ||
| ) { | ||
| setExcludeOrganizations([ | ||
| ...excludeOrganizations, | ||
| { id: excludeOrganizations.length, text: value }, | ||
| ]); | ||
| updateFilter('filterExcludeOrganizations', value as Organization, true); | ||
|
|
||
| (event.target as HTMLInputElement).value = ''; | ||
| } | ||
| }; | ||
|
|
||
| const removeExcludeOrganizationToken = (tokenId: string | number) => { | ||
| const value = | ||
| excludeOrganizations.find((v) => v.id === tokenId)?.text || ''; | ||
| updateFilter('filterExcludeOrganizations', value as Organization, false); | ||
|
|
||
| setExcludeOrganizations( | ||
| excludeOrganizations.filter((v) => v.id !== tokenId), | ||
| ); | ||
| }; | ||
|
|
||
| const excludeOrganizationsKeyDown = ( | ||
| event: React.KeyboardEvent<HTMLInputElement>, | ||
| ) => { | ||
| if (tokenEvents.includes(event.key)) { | ||
| addExcludeOrganizationsToken(event); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <fieldset id="filter-organizations"> | ||
| <Stack direction="horizontal" gap="condensed" align="baseline"> | ||
| <Title icon={OrganizationIcon}>Organizations</Title> | ||
| <Tooltip | ||
| name="tooltip-filter-organizations" | ||
| tooltip={ | ||
| <Stack direction="vertical" gap="condensed"> | ||
| <Text>Filter notifications by organization.</Text> | ||
| <RequiresDetailedNotificationWarning /> | ||
| </Stack> | ||
| } | ||
| /> | ||
| </Stack> | ||
| <Stack direction="vertical" gap="condensed"> | ||
| <Stack | ||
| direction="horizontal" | ||
| gap="condensed" | ||
| align="center" | ||
| className="text-sm" | ||
| > | ||
| <Box className="font-medium text-gitify-font w-28"> | ||
| <Stack direction="horizontal" gap="condensed" align="center"> | ||
| <CheckCircleFillIcon className={IconColor.GREEN} /> | ||
| <Text>Include:</Text> | ||
| </Stack> | ||
| </Box> | ||
| <TextInputWithTokens | ||
| title="Include organizations" | ||
| tokens={includeOrganizations} | ||
| onTokenRemove={removeIncludeOrganizationToken} | ||
| onKeyDown={includeOrganizationsKeyDown} | ||
| onBlur={addIncludeOrganizationsToken} | ||
| size="small" | ||
| disabled={ | ||
| !settings.detailedNotifications || | ||
| hasExcludeOrganizationFilters(settings) | ||
| } | ||
| block | ||
| /> | ||
| </Stack> | ||
|
|
||
| <Stack | ||
| direction="horizontal" | ||
| gap="condensed" | ||
| align="center" | ||
| className="text-sm" | ||
| > | ||
| <Box className="font-medium text-gitify-font w-28"> | ||
| <Stack direction="horizontal" gap="condensed" align="center"> | ||
| <NoEntryFillIcon className={IconColor.RED} /> | ||
| <Text>Exclude:</Text> | ||
| </Stack> | ||
| </Box> | ||
| <TextInputWithTokens | ||
| title="Exclude organizations" | ||
| tokens={excludeOrganizations} | ||
| onTokenRemove={removeExcludeOrganizationToken} | ||
| onKeyDown={excludeOrganizationsKeyDown} | ||
| onBlur={addExcludeOrganizationsToken} | ||
| size="small" | ||
| disabled={ | ||
| !settings.detailedNotifications || | ||
| hasIncludeOrganizationFilters(settings) | ||
| } | ||
| block | ||
| /> | ||
| </Stack> | ||
| </Stack> | ||
| </fieldset> | ||
| ); | ||
| }; | ||
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
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.