Migrate to a monorepo structure (#2909)

This commit is contained in:
Charles Bochet
2023-12-10 18:10:54 +01:00
committed by GitHub
parent a70a9281eb
commit 5bdca9de6c
2304 changed files with 37152 additions and 25869 deletions

View File

@ -0,0 +1,32 @@
import React from 'react';
import styled from '@emotion/styled';
const StyledGroup = styled.div`
align-items: center;
color: ${({ theme }) => theme.font.color.light};
font-size: ${({ theme }) => theme.font.size.xs};
font-weight: ${({ theme }) => theme.font.weight.semiBold};
padding-bottom: ${({ theme }) => theme.spacing(2)};
padding-left: ${({ theme }) => theme.spacing(2)};
padding-right: ${({ theme }) => theme.spacing(1)};
padding-top: ${({ theme }) => theme.spacing(2)};
text-transform: uppercase;
user-select: none;
`;
type CommandGroupProps = {
heading: string;
children: React.ReactNode | React.ReactNode[];
};
export const CommandGroup = ({ heading, children }: CommandGroupProps) => {
if (!children || !React.Children.count(children)) {
return null;
}
return (
<div>
<StyledGroup>{heading}</StyledGroup>
{children}
</div>
);
};

View File

@ -0,0 +1,303 @@
import { useState } from 'react';
import styled from '@emotion/styled';
import { useRecoilValue } from 'recoil';
import { useOpenActivityRightDrawer } from '@/activities/hooks/useOpenActivityRightDrawer';
import { Activity } from '@/activities/types/Activity';
import { Company } from '@/companies/types/Company';
import { useKeyboardShortcutMenu } from '@/keyboard-shortcut-menu/hooks/useKeyboardShortcutMenu';
import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords';
import { Person } from '@/people/types/Person';
import { IconNotes } from '@/ui/display/icon';
import { SelectableItem } from '@/ui/layout/selectable-list/components/SelectableItem';
import { SelectableList } from '@/ui/layout/selectable-list/components/SelectableList';
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys';
import { AppHotkeyScope } from '@/ui/utilities/hotkey/types/AppHotkeyScope';
import { useIsMobile } from '@/ui/utilities/responsive/hooks/useIsMobile';
import { ScrollWrapper } from '@/ui/utilities/scroll/components/ScrollWrapper';
import { Avatar } from '@/users/components/Avatar';
import { getLogoUrlFromDomainName } from '~/utils';
import { useCommandMenu } from '../hooks/useCommandMenu';
import { commandMenuCommandsState } from '../states/commandMenuCommandsState';
import { isCommandMenuOpenedState } from '../states/isCommandMenuOpenedState';
import { Command, CommandType } from '../types/Command';
import { CommandGroup } from './CommandGroup';
import { CommandMenuItem } from './CommandMenuItem';
export const StyledDialog = styled.div`
background: ${({ theme }) => theme.background.primary};
border-radius: ${({ theme }) => theme.border.radius.md};
box-shadow: ${({ theme }) => theme.boxShadow.strong};
font-family: ${({ theme }) => theme.font.family};
left: 50%;
max-width: 640px;
overflow: hidden;
padding: 0;
position: fixed;
top: 30%;
transform: ${() =>
useIsMobile() ? 'translateX(-49.5%)' : 'translateX(-50%)'};
width: ${() => (useIsMobile() ? 'calc(100% - 40px)' : '100%')};
z-index: 1000;
`;
export const StyledInput = styled.input`
background: ${({ theme }) => theme.background.primary};
border: none;
border-bottom: 1px solid ${({ theme }) => theme.border.color.light};
border-radius: 0;
color: ${({ theme }) => theme.font.color.primary};
font-size: ${({ theme }) => theme.font.size.lg};
margin: 0;
outline: none;
padding: ${({ theme }) => theme.spacing(5)};
width: ${({ theme }) => `calc(100% - ${theme.spacing(10)})`};
&::placeholder {
color: ${({ theme }) => theme.font.color.light};
}
`;
export const StyledList = styled.div`
background: ${({ theme }) => theme.background.secondary};
height: 400px;
max-height: 400px;
overscroll-behavior: contain;
transition: 100ms ease;
transition-property: height;
`;
export const StyledInnerList = styled.div`
padding-left: ${({ theme }) => theme.spacing(1)};
width: 100%;
`;
export const StyledEmpty = styled.div`
align-items: center;
color: ${({ theme }) => theme.font.color.light};
display: flex;
font-size: ${({ theme }) => theme.font.size.md};
height: 64px;
justify-content: center;
white-space: pre-wrap;
`;
export const CommandMenu = () => {
const { toggleCommandMenu } = useCommandMenu();
const openActivityRightDrawer = useOpenActivityRightDrawer();
const isCommandMenuOpened = useRecoilValue(isCommandMenuOpenedState);
const [search, setSearch] = useState('');
const commandMenuCommands = useRecoilValue(commandMenuCommandsState);
const { closeKeyboardShortcutMenu } = useKeyboardShortcutMenu();
const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setSearch(event.target.value);
};
useScopedHotkeys(
'ctrl+k,meta+k',
() => {
closeKeyboardShortcutMenu();
setSearch('');
toggleCommandMenu();
},
AppHotkeyScope.CommandMenu,
[toggleCommandMenu, setSearch],
);
const { records: people } = useFindManyRecords<Person>({
skip: !isCommandMenuOpened,
objectNameSingular: 'person',
filter: {
or: [
{ name: { firstName: { ilike: `%${search}%` } } },
{ name: { firstName: { ilike: `%${search}%` } } },
],
},
limit: 3,
});
const { records: companies } = useFindManyRecords<Company>({
skip: !isCommandMenuOpened,
objectNameSingular: 'company',
filter: {
name: { ilike: `%${search}%` },
},
limit: 3,
});
const { records: activities } = useFindManyRecords<Activity>({
skip: !isCommandMenuOpened,
objectNameSingular: 'activity',
filter: {
or: [
{ title: { like: `%${search}%` } },
{ body: { like: `%${search}%` } },
],
},
limit: 3,
});
const checkInShortcuts = (cmd: Command, search: string) => {
return (cmd.firstHotKey + (cmd.secondHotKey ?? ''))
.toLowerCase()
.includes(search.toLowerCase());
};
const checkInLabels = (cmd: Command, search: string) => {
if (cmd.label) {
return cmd.label.toLowerCase().includes(search.toLowerCase());
}
return false;
};
const matchingNavigateCommand = commandMenuCommands.filter(
(cmd) =>
(search.length > 0
? checkInShortcuts(cmd, search) || checkInLabels(cmd, search)
: true) && cmd.type === CommandType.Navigate,
);
const matchingCreateCommand = commandMenuCommands.filter(
(cmd) =>
(search.length > 0
? checkInShortcuts(cmd, search) || checkInLabels(cmd, search)
: true) && cmd.type === CommandType.Create,
);
const selectableItemIds = matchingCreateCommand
.map((cmd) => cmd.id)
.concat(matchingNavigateCommand.map((cmd) => cmd.id))
.concat(people.map((person) => person.id))
.concat(companies.map((company) => company.id))
.concat(activities.map((activity) => activity.id));
return (
<>
{isCommandMenuOpened && (
<StyledDialog>
<StyledInput
value={search}
placeholder="Search"
onChange={handleSearchChange}
/>
<StyledList>
<ScrollWrapper>
<StyledInnerList>
<SelectableList
selectableListId="command-menu-list"
selectableItemIds={[selectableItemIds]}
hotkeyScope={AppHotkeyScope.CommandMenu}
onEnter={(_itemId) => {}}
>
{!matchingCreateCommand.length &&
!matchingNavigateCommand.length &&
!people.length &&
!companies.length &&
!activities.length && (
<StyledEmpty>No results found</StyledEmpty>
)}
<CommandGroup heading="Create">
{matchingCreateCommand.map((cmd) => (
<SelectableItem itemId={cmd.id} key={cmd.id}>
<CommandMenuItem
id={cmd.id}
to={cmd.to}
key={cmd.id}
Icon={cmd.Icon}
label={cmd.label}
onClick={cmd.onCommandClick}
firstHotKey={cmd.firstHotKey}
secondHotKey={cmd.secondHotKey}
/>
</SelectableItem>
))}
</CommandGroup>
<CommandGroup heading="Navigate">
{matchingNavigateCommand.map((cmd) => (
<SelectableItem itemId={cmd.id} key={cmd.id}>
<CommandMenuItem
id={cmd.id}
to={cmd.to}
key={cmd.id}
label={cmd.label}
Icon={cmd.Icon}
onClick={cmd.onCommandClick}
firstHotKey={cmd.firstHotKey}
secondHotKey={cmd.secondHotKey}
/>
</SelectableItem>
))}
</CommandGroup>
<CommandGroup heading="People">
{people.map((person) => (
<SelectableItem itemId={person.id} key={person.id}>
<CommandMenuItem
id={person.id}
key={person.id}
to={`object/person/${person.id}`}
label={
person.name.firstName + ' ' + person.name.lastName
}
Icon={() => (
<Avatar
type="rounded"
avatarUrl={null}
colorId={person.id}
placeholder={
person.name.firstName +
' ' +
person.name.lastName
}
/>
)}
/>
</SelectableItem>
))}
</CommandGroup>
<CommandGroup heading="Companies">
{companies.map((company) => (
<SelectableItem itemId={company.id} key={company.id}>
<CommandMenuItem
id={company.id}
key={company.id}
label={company.name}
to={`object/company/${company.id}`}
Icon={() => (
<Avatar
colorId={company.id}
placeholder={company.name}
avatarUrl={getLogoUrlFromDomainName(
company.domainName,
)}
/>
)}
/>
</SelectableItem>
))}
</CommandGroup>
<CommandGroup heading="Notes">
{activities.map((activity) => (
<SelectableItem itemId={activity.id} key={activity.id}>
<CommandMenuItem
id={activity.id}
Icon={IconNotes}
key={activity.id}
label={activity.title ?? ''}
onClick={() => openActivityRightDrawer(activity.id)}
/>
</SelectableItem>
))}
</CommandGroup>
</SelectableList>
</StyledInnerList>
</ScrollWrapper>
</StyledList>
</StyledDialog>
)}
</>
);
};

View File

@ -0,0 +1,45 @@
import { IconArrowUpRight } from '@/ui/display/icon';
import { IconComponent } from '@/ui/display/icon/types/IconComponent';
import { useSelectableList } from '@/ui/layout/selectable-list/hooks/useSelectableList';
import { MenuItemCommand } from '@/ui/navigation/menu-item/components/MenuItemCommand';
import { useCommandMenu } from '../hooks/useCommandMenu';
export type CommandMenuItemProps = {
label: string;
to?: string;
id: string;
onClick?: () => void;
Icon?: IconComponent;
firstHotKey?: string;
secondHotKey?: string;
};
export const CommandMenuItem = ({
label,
to,
id,
onClick,
Icon,
firstHotKey,
secondHotKey,
}: CommandMenuItemProps) => {
const { onItemClick } = useCommandMenu();
if (to && !Icon) {
Icon = IconArrowUpRight;
}
const { isSelectedItemId } = useSelectableList({ itemId: id });
return (
<MenuItemCommand
LeftIcon={Icon}
text={label}
firstHotKey={firstHotKey}
secondHotKey={secondHotKey}
onClick={() => onItemClick(onClick, to)}
isSelected={isSelectedItemId}
/>
);
};

View File

@ -0,0 +1,119 @@
import { useEffect } from 'react';
import { Meta, StoryObj } from '@storybook/react';
import { expect, userEvent, within } from '@storybook/test';
import { useCommandMenu } from '@/command-menu/hooks/useCommandMenu';
import { CommandType } from '@/command-menu/types/Command';
import { IconCheckbox, IconNotes } from '@/ui/display/icon';
import { ComponentWithRouterDecorator } from '~/testing/decorators/ComponentWithRouterDecorator';
import { ObjectMetadataItemsDecorator } from '~/testing/decorators/ObjectMetadataItemsDecorator';
import { SnackBarDecorator } from '~/testing/decorators/SnackBarDecorator';
import { graphqlMocks } from '~/testing/graphqlMocks';
import { sleep } from '~/testing/sleep';
import { CommandMenu } from '../CommandMenu';
const openTimeout = 50;
const meta: Meta<typeof CommandMenu> = {
title: 'Modules/CommandMenu/CommandMenu',
component: CommandMenu,
decorators: [
ObjectMetadataItemsDecorator,
ComponentWithRouterDecorator,
(Story) => {
const { addToCommandMenu, setToIntitialCommandMenu, toggleCommandMenu } =
useCommandMenu();
useEffect(() => {
setToIntitialCommandMenu();
addToCommandMenu([
{
id: 'create-task',
to: '',
label: 'Create Task',
type: CommandType.Create,
Icon: IconCheckbox,
onCommandClick: () => console.log('create task click'),
},
{
id: 'create-note',
to: '',
label: 'Create Note',
type: CommandType.Create,
Icon: IconNotes,
onCommandClick: () => console.log('create note click'),
},
]);
toggleCommandMenu();
}, [addToCommandMenu, setToIntitialCommandMenu, toggleCommandMenu]);
return <Story />;
},
SnackBarDecorator,
],
parameters: {
msw: graphqlMocks,
},
};
export default meta;
type Story = StoryObj<typeof CommandMenu>;
export const DefaultWithoutSearch: Story = {
play: async () => {
const canvas = within(document.body);
expect(await canvas.findByText('Create Task')).toBeInTheDocument();
expect(await canvas.findByText('Go to People')).toBeInTheDocument();
expect(await canvas.findByText('Go to Companies')).toBeInTheDocument();
expect(await canvas.findByText('Go to Opportunities')).toBeInTheDocument();
expect(await canvas.findByText('Go to Settings')).toBeInTheDocument();
expect(await canvas.findByText('Go to Tasks')).toBeInTheDocument();
},
};
export const MatchingPersonCompanyActivityCreateNavigate: Story = {
play: async () => {
const canvas = within(document.body);
const searchInput = await canvas.findByPlaceholderText('Search');
await sleep(openTimeout);
await userEvent.type(searchInput, 'n');
expect(await canvas.findByText('Alexandre Prot')).toBeInTheDocument();
expect(await canvas.findByText('Airbnb')).toBeInTheDocument();
expect(await canvas.findByText('My very first note')).toBeInTheDocument();
expect(await canvas.findByText('Create Note')).toBeInTheDocument();
expect(await canvas.findByText('Go to Companies')).toBeInTheDocument();
},
};
export const OnlyMatchingCreateAndNavigate: Story = {
play: async () => {
const canvas = within(document.body);
const searchInput = await canvas.findByPlaceholderText('Search');
await sleep(openTimeout);
await userEvent.type(searchInput, 'ta');
expect(await canvas.findByText('Create Task')).toBeInTheDocument();
expect(await canvas.findByText('Go to Tasks')).toBeInTheDocument();
},
};
export const AtleastMatchingOnePerson: Story = {
play: async () => {
const canvas = within(document.body);
const searchInput = await canvas.findByPlaceholderText('Search');
await sleep(openTimeout);
await userEvent.type(searchInput, 'alex');
expect(await canvas.findByText('Alexandre Prot')).toBeInTheDocument();
},
};
export const NotMatchingAnything: Story = {
play: async () => {
const canvas = within(document.body);
const searchInput = await canvas.findByPlaceholderText('Search');
await sleep(openTimeout);
await userEvent.type(searchInput, 'asdasdasd');
expect(await canvas.findByText('No results found.')).toBeInTheDocument();
},
};