use-create-todays-memo.tsx 1.3 KB

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