Skip to content
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

feat: add handle filter tokens on blur #1837

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 41 additions & 31 deletions src/renderer/components/filters/UserHandleFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ export const UserHandleFilter: FC = () => {
mapValuesToTokens(settings.filterIncludeHandles),
);

const addIncludeHandlesToken = (
event:
| React.KeyboardEvent<HTMLInputElement>
| React.FocusEvent<HTMLInputElement>,
) => {
const value = (event.target as HTMLInputElement).value.trim();

if (value.length > 0 && !includeHandles.some((v) => v.text === value)) {
setIncludeHandles([
...includeHandles,
{ id: includeHandles.length, text: value },
]);
updateFilter('filterIncludeHandles', value as UserHandle, true);

(event.target as HTMLInputElement).value = '';
}
};

const removeIncludeHandleToken = (tokenId: string | number) => {
const value = includeHandles.find((v) => v.id === tokenId)?.text || '';
updateFilter('filterIncludeHandles', value as UserHandle, false);
Expand All @@ -58,29 +76,33 @@ export const UserHandleFilter: FC = () => {
const includeHandlesKeyDown = (
event: React.KeyboardEvent<HTMLInputElement>,
) => {
const value = (event.target as HTMLInputElement).value.trim();
if (tokenEvents.includes(event.key)) {
addIncludeHandlesToken(event);
}
};

if (
tokenEvents.includes(event.key) &&
!includeHandles.some((v) => v.text === value) &&
value.length > 0
) {
event.preventDefault();
const [excludeHandles, setExcludeHandles] = useState<InputToken[]>(
mapValuesToTokens(settings.filterExcludeHandles),
);

setIncludeHandles([
...includeHandles,
{ id: includeHandles.length, text: value },
const addExcludeHandlesToken = (
event:
| React.KeyboardEvent<HTMLInputElement>
| React.FocusEvent<HTMLInputElement>,
) => {
const value = (event.target as HTMLInputElement).value.trim();

if (value.length > 0 && !excludeHandles.some((v) => v.text === value)) {
setExcludeHandles([
...excludeHandles,
{ id: excludeHandles.length, text: value },
]);
updateFilter('filterIncludeHandles', value as UserHandle, true);
updateFilter('filterExcludeHandles', value as UserHandle, true);

(event.target as HTMLInputElement).value = '';
}
};

const [excludeHandles, setExcludeHandles] = useState<InputToken[]>(
mapValuesToTokens(settings.filterExcludeHandles),
);

const removeExcludeHandleToken = (tokenId: string | number) => {
const value = excludeHandles.find((v) => v.id === tokenId)?.text || '';
updateFilter('filterExcludeHandles', value as UserHandle, false);
Expand All @@ -91,22 +113,8 @@ export const UserHandleFilter: FC = () => {
const excludeHandlesKeyDown = (
event: React.KeyboardEvent<HTMLInputElement>,
) => {
const value = (event.target as HTMLInputElement).value.trim();

if (
tokenEvents.includes(event.key) &&
!excludeHandles.some((v) => v.text === value) &&
value.length > 0
) {
event.preventDefault();

setExcludeHandles([
...excludeHandles,
{ id: excludeHandles.length, text: value },
]);
updateFilter('filterExcludeHandles', value as UserHandle, true);

(event.target as HTMLInputElement).value = '';
if (tokenEvents.includes(event.key)) {
addExcludeHandlesToken(event);
}
};

Expand Down Expand Up @@ -146,6 +154,7 @@ export const UserHandleFilter: FC = () => {
tokens={includeHandles}
onTokenRemove={removeIncludeHandleToken}
onKeyDown={includeHandlesKeyDown}
onBlur={addIncludeHandlesToken}
size="small"
disabled={
!settings.detailedNotifications ||
Expand All @@ -172,6 +181,7 @@ export const UserHandleFilter: FC = () => {
tokens={excludeHandles}
onTokenRemove={removeExcludeHandleToken}
onKeyDown={excludeHandlesKeyDown}
onBlur={addExcludeHandlesToken}
size="small"
disabled={
!settings.detailedNotifications ||
Expand Down
Loading