-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(client): extract heavy parts of add tag form
- Loading branch information
1 parent
76b61bb
commit a37b0f8
Showing
3 changed files
with
105 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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,67 @@ | ||
import { AddCircleOutline as PlusIcon } from '@mui/icons-material'; | ||
import { Button, IconButton } from '@mui/material'; | ||
import { INTERNAL_TAG_PREFIX } from '@structure/common'; | ||
import { useCallback, useState } from 'react'; | ||
import useShortcut from '../hooks/useShortcut'; | ||
import { SHORTCUTS } from '../utils/keyboard'; | ||
import AddTagForm, { AddTagFormProps } from './AddTagForm'; | ||
import TooltipWithShortcut from './TooltipWithShortcut'; | ||
|
||
const AddTagButtonOrForm = ({ | ||
currentTags, | ||
withShortcuts, | ||
...tagFormProps | ||
}: AddTagFormProps & { | ||
withShortcuts?: boolean; | ||
}) => { | ||
const [addingNew, setAddingNew] = useState<boolean>(false); | ||
|
||
const showNewTagForm = useCallback((): void => { | ||
setAddingNew(true); | ||
}, [setAddingNew]); | ||
|
||
const hideNewTagForm = useCallback((): void => { | ||
setAddingNew(false); | ||
}, [setAddingNew]); | ||
|
||
useShortcut(withShortcuts ? SHORTCUTS.ADD_TAG : null, showNewTagForm, true); | ||
|
||
if (addingNew) { | ||
return ( | ||
<AddTagForm | ||
currentTags={currentTags} | ||
onHideTagForm={hideNewTagForm} | ||
{...tagFormProps} | ||
/> | ||
); | ||
} | ||
|
||
if ( | ||
currentTags.filter((tag) => !tag.name.startsWith(INTERNAL_TAG_PREFIX)) | ||
.length === 0 | ||
) { | ||
return ( | ||
<TooltipWithShortcut | ||
title="" | ||
shortcut={withShortcuts ? SHORTCUTS.ADD_TAG : undefined} | ||
> | ||
<Button variant="outlined" size="small" onClick={showNewTagForm}> | ||
Add tag | ||
</Button> | ||
</TooltipWithShortcut> | ||
); | ||
} | ||
|
||
return ( | ||
<TooltipWithShortcut | ||
title="Add tag" | ||
shortcut={withShortcuts ? SHORTCUTS.ADD_TAG : undefined} | ||
> | ||
<IconButton size="small" onClick={showNewTagForm}> | ||
<PlusIcon /> | ||
</IconButton> | ||
</TooltipWithShortcut> | ||
); | ||
}; | ||
|
||
export default AddTagButtonOrForm; |
This file contains 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 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