use-create-todays-memo.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { useCallback } from 'react';
  2. import { userHomepagePath } from '@growi/core/dist/utils/page-path-utils';
  3. import { format } from 'date-fns';
  4. import { useTranslation } from 'react-i18next';
  5. import { useCreatePageAndTransit } from '~/client/services/create-page';
  6. import { useCurrentUser } from '~/stores/context';
  7. type UseCreateTodaysMemo = () => {
  8. isCreating: boolean,
  9. todaysPath: string | null,
  10. createTodaysMemo: () => Promise<void>,
  11. }
  12. export const useCreateTodaysMemo: UseCreateTodaysMemo = () => {
  13. const { t } = useTranslation('commons');
  14. const { data: currentUser } = useCurrentUser();
  15. const { isCreating, createAndTransit } = useCreatePageAndTransit();
  16. const isCreatable = currentUser != null;
  17. const parentDirName = t('create_page_dropdown.todays.memo');
  18. const now = format(new Date(), 'yyyy/MM/dd');
  19. const todaysPath = isCreatable
  20. ? `${userHomepagePath(currentUser)}/${parentDirName}/${now}`
  21. : null;
  22. const createTodaysMemo = useCallback(async() => {
  23. if (!isCreatable || todaysPath == null) return;
  24. return createAndTransit(
  25. { path: todaysPath },
  26. { shouldCheckPageExists: true },
  27. );
  28. }, [createAndTransit, isCreatable, todaysPath]);
  29. return {
  30. isCreating,
  31. todaysPath,
  32. createTodaysMemo,
  33. };
  34. };