|
|
@@ -43,11 +43,16 @@ const PageCreateModal: React.FC = () => {
|
|
|
const { createTemplate } = useCreateTemplatePage();
|
|
|
|
|
|
const isReachable = useAtomValue(isSearchServiceReachableAtom);
|
|
|
- const userHomepagePath = pagePathUtils.userHomepagePath(currentUser);
|
|
|
- const isCreatable = isCreatablePage(pathname) || isUsersHomepage(pathname);
|
|
|
- const pageNameInputInitialValue = isCreatable ? pathUtils.addTrailingSlash(pathname) : '/';
|
|
|
- const now = format(new Date(), 'yyyy/MM/dd');
|
|
|
- const todaysParentPath = [userHomepagePath, t('create_page_dropdown.todays.memo', { ns: 'commons' }), now].join('/');
|
|
|
+
|
|
|
+ // Memoize computed values
|
|
|
+ const userHomepagePath = useMemo(() => pagePathUtils.userHomepagePath(currentUser), [currentUser]);
|
|
|
+ const isCreatable = useMemo(() => isCreatablePage(pathname) || isUsersHomepage(pathname), [pathname]);
|
|
|
+ const pageNameInputInitialValue = useMemo(() => (isCreatable ? pathUtils.addTrailingSlash(pathname) : '/'), [isCreatable, pathname]);
|
|
|
+ const now = useMemo(() => format(new Date(), 'yyyy/MM/dd'), []);
|
|
|
+ const todaysParentPath = useMemo(
|
|
|
+ () => [userHomepagePath, t('create_page_dropdown.todays.memo', { ns: 'commons' }), now].join('/'),
|
|
|
+ [userHomepagePath, t, now],
|
|
|
+ );
|
|
|
|
|
|
const [todayInput, setTodayInput] = useState('');
|
|
|
const [pageNameInput, setPageNameInput] = useState(pageNameInputInitialValue);
|
|
|
@@ -55,41 +60,38 @@ const PageCreateModal: React.FC = () => {
|
|
|
const [isMatchedWithUserHomepagePath, setIsMatchedWithUserHomepagePath] = useState(false);
|
|
|
|
|
|
const checkIsUsersHomepageDebounce = useMemo(() => {
|
|
|
- const checkIsUsersHomepage = () => {
|
|
|
- setIsMatchedWithUserHomepagePath(isUsersHomepage(pageNameInput));
|
|
|
- };
|
|
|
-
|
|
|
- return debounce(1000, checkIsUsersHomepage);
|
|
|
- }, [pageNameInput]);
|
|
|
+ return debounce(1000, (input: string) => {
|
|
|
+ setIsMatchedWithUserHomepagePath(isUsersHomepage(input));
|
|
|
+ });
|
|
|
+ }, []);
|
|
|
|
|
|
useEffect(() => {
|
|
|
if (isOpened) {
|
|
|
- checkIsUsersHomepageDebounce();
|
|
|
+ checkIsUsersHomepageDebounce(pageNameInput);
|
|
|
}
|
|
|
}, [isOpened, checkIsUsersHomepageDebounce, pageNameInput]);
|
|
|
|
|
|
-
|
|
|
- function transitBySubmitEvent(e, transitHandler) {
|
|
|
+ const transitBySubmitEvent = useCallback((e, transitHandler) => {
|
|
|
// prevent page transition by submit
|
|
|
e.preventDefault();
|
|
|
transitHandler();
|
|
|
- }
|
|
|
+ }, []);
|
|
|
|
|
|
/**
|
|
|
* change todayInput
|
|
|
* @param {string} value
|
|
|
*/
|
|
|
- function onChangeTodayInputHandler(value) {
|
|
|
+ const onChangeTodayInputHandler = useCallback((value) => {
|
|
|
setTodayInput(value);
|
|
|
- }
|
|
|
+ }, []);
|
|
|
|
|
|
/**
|
|
|
* change template
|
|
|
* @param {string} value
|
|
|
*/
|
|
|
- function onChangeTemplateHandler(value) {
|
|
|
+ const onChangeTemplateHandler = useCallback((value) => {
|
|
|
setTemplate(value);
|
|
|
- }
|
|
|
+ }, []);
|
|
|
|
|
|
/**
|
|
|
* access today page
|
|
|
@@ -137,7 +139,7 @@ const PageCreateModal: React.FC = () => {
|
|
|
const createInputPageWithToastr = useToastrOnError(createInputPage);
|
|
|
const createTemplateWithToastr = useToastrOnError(createTemplatePage);
|
|
|
|
|
|
- function renderCreateTodayForm() {
|
|
|
+ const renderCreateTodayForm = useMemo(() => {
|
|
|
if (!isOpened) {
|
|
|
return <></>;
|
|
|
}
|
|
|
@@ -180,9 +182,9 @@ const PageCreateModal: React.FC = () => {
|
|
|
</fieldset>
|
|
|
</div>
|
|
|
);
|
|
|
- }
|
|
|
+ }, [isOpened, todaysParentPath, todayInput, t, onChangeTodayInputHandler, transitBySubmitEvent, createTodaysMemoWithToastr]);
|
|
|
|
|
|
- function renderInputPageForm() {
|
|
|
+ const renderInputPageForm = useMemo(() => {
|
|
|
if (!isOpened) {
|
|
|
return <></>;
|
|
|
}
|
|
|
@@ -237,9 +239,9 @@ const PageCreateModal: React.FC = () => {
|
|
|
</fieldset>
|
|
|
</div>
|
|
|
);
|
|
|
- }
|
|
|
+ }, [isOpened, isReachable, pageNameInputInitialValue, createInputPageWithToastr, pageNameInput, isMatchedWithUserHomepagePath, t, transitBySubmitEvent]);
|
|
|
|
|
|
- function renderTemplatePageForm() {
|
|
|
+ const renderTemplatePageForm = useMemo(() => {
|
|
|
if (!isOpened) {
|
|
|
return <></>;
|
|
|
}
|
|
|
@@ -289,7 +291,7 @@ const PageCreateModal: React.FC = () => {
|
|
|
</fieldset>
|
|
|
</div>
|
|
|
);
|
|
|
- }
|
|
|
+ }, [isOpened, pathname, template, onChangeTemplateHandler, createTemplateWithToastr, t]);
|
|
|
|
|
|
return (
|
|
|
<Modal
|
|
|
@@ -304,9 +306,9 @@ const PageCreateModal: React.FC = () => {
|
|
|
{t('New Page')}
|
|
|
</ModalHeader>
|
|
|
<ModalBody>
|
|
|
- {renderCreateTodayForm()}
|
|
|
- {renderInputPageForm()}
|
|
|
- {renderTemplatePageForm()}
|
|
|
+ {renderCreateTodayForm}
|
|
|
+ {renderInputPageForm}
|
|
|
+ {renderTemplatePageForm}
|
|
|
</ModalBody>
|
|
|
</Modal>
|
|
|
|