Fix command menu keyboard navigation (#2908)
* Fix CommandMenu Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Matheus <matheus_benini@hotmail.com> * Refactor according to review Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Matheus <matheus_benini@hotmail.com> * Refactor according to review Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Matheus <matheus_benini@hotmail.com> --------- Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com> Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Matheus <matheus_benini@hotmail.com> Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
import { useState } from 'react';
|
import { useMemo, useState } from 'react';
|
||||||
import styled from '@emotion/styled';
|
import styled from '@emotion/styled';
|
||||||
import { useRecoilValue } from 'recoil';
|
import { useRecoilValue } from 'recoil';
|
||||||
|
|
||||||
@ -85,7 +85,7 @@ export const StyledEmpty = styled.div`
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
export const CommandMenu = () => {
|
export const CommandMenu = () => {
|
||||||
const { toggleCommandMenu } = useCommandMenu();
|
const { toggleCommandMenu, onItemClick } = useCommandMenu();
|
||||||
|
|
||||||
const openActivityRightDrawer = useOpenActivityRightDrawer();
|
const openActivityRightDrawer = useOpenActivityRightDrawer();
|
||||||
const isCommandMenuOpened = useRecoilValue(isCommandMenuOpenedState);
|
const isCommandMenuOpened = useRecoilValue(isCommandMenuOpenedState);
|
||||||
@ -141,6 +141,45 @@ export const CommandMenu = () => {
|
|||||||
limit: 3,
|
limit: 3,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const peopleCommands = useMemo(
|
||||||
|
() =>
|
||||||
|
people.map(({ id, name: { firstName, lastName } }) => ({
|
||||||
|
id,
|
||||||
|
label: `${firstName} ${lastName}`,
|
||||||
|
to: `object/person/${id}`,
|
||||||
|
})),
|
||||||
|
[people],
|
||||||
|
);
|
||||||
|
|
||||||
|
const companyCommands = useMemo(
|
||||||
|
() =>
|
||||||
|
companies.map(({ id, name }) => ({
|
||||||
|
id,
|
||||||
|
label: name ?? '',
|
||||||
|
to: `object/company/${id}`,
|
||||||
|
})),
|
||||||
|
[companies],
|
||||||
|
);
|
||||||
|
|
||||||
|
const activityCommands = useMemo(
|
||||||
|
() =>
|
||||||
|
activities.map(({ id, title }) => ({
|
||||||
|
id,
|
||||||
|
label: title ?? '',
|
||||||
|
to: '',
|
||||||
|
onCommandClick: () => openActivityRightDrawer(id),
|
||||||
|
})),
|
||||||
|
[activities, openActivityRightDrawer],
|
||||||
|
);
|
||||||
|
|
||||||
|
const otherCommands = useMemo(() => {
|
||||||
|
return [
|
||||||
|
...peopleCommands,
|
||||||
|
...companyCommands,
|
||||||
|
...activityCommands,
|
||||||
|
] as Command[];
|
||||||
|
}, [peopleCommands, companyCommands, activityCommands]);
|
||||||
|
|
||||||
const checkInShortcuts = (cmd: Command, search: string) => {
|
const checkInShortcuts = (cmd: Command, search: string) => {
|
||||||
return (cmd.firstHotKey + (cmd.secondHotKey ?? ''))
|
return (cmd.firstHotKey + (cmd.secondHotKey ?? ''))
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
@ -191,7 +230,17 @@ export const CommandMenu = () => {
|
|||||||
selectableListId="command-menu-list"
|
selectableListId="command-menu-list"
|
||||||
selectableItemIds={[selectableItemIds]}
|
selectableItemIds={[selectableItemIds]}
|
||||||
hotkeyScope={AppHotkeyScope.CommandMenu}
|
hotkeyScope={AppHotkeyScope.CommandMenu}
|
||||||
onEnter={(_itemId) => {}}
|
onEnter={(itemId) => {
|
||||||
|
const command = [
|
||||||
|
...commandMenuCommands,
|
||||||
|
...otherCommands,
|
||||||
|
].find((cmd) => cmd.id === itemId);
|
||||||
|
|
||||||
|
if (command) {
|
||||||
|
const { to, onCommandClick } = command;
|
||||||
|
onItemClick(onCommandClick, to);
|
||||||
|
}
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
{!matchingCreateCommand.length &&
|
{!matchingCreateCommand.length &&
|
||||||
!matchingNavigateCommand.length &&
|
!matchingNavigateCommand.length &&
|
||||||
|
|||||||
Reference in New Issue
Block a user