-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathPageCreateButton.tsx
81 lines (68 loc) · 2.71 KB
/
PageCreateButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import React, { useState } from 'react';
import { Dropdown } from 'reactstrap';
import { useCreateTemplatePage } from '~/client/services/create-page';
import { useToastrOnError } from '~/client/services/use-toastr-on-error';
import { usePageCreateModal } from '~/stores/modal';
import { useCurrentPagePath } from '~/stores/page';
import { CreateButton } from './CreateButton';
import { DropendMenu } from './DropendMenu';
import { DropendToggle } from './DropendToggle';
import { useCreateNewPage, useCreateTodaysMemo } from './hooks';
export const PageCreateButton = React.memo((): JSX.Element => {
const [isHovered, setIsHovered] = useState(false);
const [dropdownOpen, setDropdownOpen] = useState(false);
const { open: openPageCreateModal } = usePageCreateModal();
const { data: currentPagePath } = useCurrentPagePath();
const { createNewPage, isCreating: isNewPageCreating } = useCreateNewPage();
// TODO: https://redmine.weseek.co.jp/issues/138806
const { createTodaysMemo, isCreating: isTodaysPageCreating, todaysPath } = useCreateTodaysMemo();
// TODO: https://redmine.weseek.co.jp/issues/138805
const {
createTemplate,
isCreating: isTemplatePageCreating, isCreatable: isTemplatePageCreatable,
} = useCreateTemplatePage();
const createNewPageWithToastr = useToastrOnError(createNewPage);
const createTodaysMemoWithToastr = useToastrOnError(createTodaysMemo);
const createTemplateWithToastr = useToastrOnError(createTemplate);
const onMouseEnterHandler = () => {
setIsHovered(true);
};
const onMouseLeaveHandler = () => {
setIsHovered(false);
setDropdownOpen(false);
};
const toggle = () => setDropdownOpen(!dropdownOpen);
return (
<div
className="d-flex flex-row mt-2"
onMouseEnter={onMouseEnterHandler}
onMouseLeave={onMouseLeaveHandler}
data-testid="grw-page-create-button"
>
<div className="btn-group flex-grow-1">
<CreateButton
className="z-2"
onClick={createNewPageWithToastr}
disabled={isNewPageCreating || isTodaysPageCreating || isTemplatePageCreating}
/>
</div>
{ isHovered && (
<Dropdown
isOpen={dropdownOpen}
toggle={toggle}
direction="end"
className="position-absolute"
>
<DropendToggle />
<DropendMenu
onClickCreateNewPage={createNewPageWithToastr}
onClickOpenPageCreateModal={() => openPageCreateModal(currentPagePath)}
onClickCreateTodaysMemo={createTodaysMemoWithToastr}
onClickCreateTemplate={isTemplatePageCreatable ? createTemplateWithToastr : undefined}
todaysPath={todaysPath}
/>
</Dropdown>
)}
</div>
);
});