| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- import { SWRResponse } from 'swr';
- import { IPageToDeleteWithMeta, IPageToRenameWithMeta } from '~/interfaces/page';
- import {
- OnDuplicatedFunction, OnRenamedFunction, OnDeletedFunction, OnPutBackedFunction,
- } from '~/interfaces/ui';
- import { IUserGroupHasId } from '~/interfaces/user';
- import { useStaticSWR } from './use-static-swr';
- /*
- * PageCreateModal
- */
- type CreateModalStatus = {
- isOpened: boolean,
- path?: string,
- }
- type CreateModalStatusUtils = {
- open(path?: string): Promise<CreateModalStatus | undefined>
- close(): Promise<CreateModalStatus | undefined>
- }
- export const usePageCreateModal = (status?: CreateModalStatus): SWRResponse<CreateModalStatus, Error> & CreateModalStatusUtils => {
- const initialData: CreateModalStatus = { isOpened: false };
- const swrResponse = useStaticSWR<CreateModalStatus, Error>('pageCreateModalStatus', status, { fallbackData: initialData });
- return {
- ...swrResponse,
- open: (path?: string) => swrResponse.mutate({ isOpened: true, path }),
- close: () => swrResponse.mutate({ isOpened: false }),
- };
- };
- /*
- * PageDeleteModal
- */
- export type IDeleteModalOption = {
- onDeleted?: OnDeletedFunction,
- }
- type DeleteModalStatus = {
- isOpened: boolean,
- pages?: IPageToDeleteWithMeta[],
- opts?: IDeleteModalOption,
- }
- type DeleteModalStatusUtils = {
- open(
- pages?: IPageToDeleteWithMeta[],
- opts?: IDeleteModalOption,
- ): Promise<DeleteModalStatus | undefined>,
- close(): Promise<DeleteModalStatus | undefined>,
- }
- export const usePageDeleteModal = (status?: DeleteModalStatus): SWRResponse<DeleteModalStatus, Error> & DeleteModalStatusUtils => {
- const initialData: DeleteModalStatus = {
- isOpened: false,
- pages: [],
- };
- const swrResponse = useStaticSWR<DeleteModalStatus, Error>('deleteModalStatus', status, { fallbackData: initialData });
- return {
- ...swrResponse,
- open: (
- pages?: IPageToDeleteWithMeta[],
- opts?: IDeleteModalOption,
- ) => swrResponse.mutate({
- isOpened: true, pages, opts,
- }),
- close: () => swrResponse.mutate({ isOpened: false }),
- };
- };
- /*
- * EmptyTrashModal
- */
- type IEmptyTrashModalOption = {
- onEmptiedTrash?: () => void,
- canDelepeAllPages: boolean,
- }
- type EmptyTrashModalStatus = {
- isOpened: boolean,
- pages?: IPageToDeleteWithMeta[],
- opts?: IEmptyTrashModalOption,
- }
- type EmptyTrashModalStatusUtils = {
- open(
- pages?: IPageToDeleteWithMeta[],
- opts?: IEmptyTrashModalOption,
- ): Promise<EmptyTrashModalStatus | undefined>,
- close(): Promise<EmptyTrashModalStatus | undefined>,
- }
- export const useEmptyTrashModal = (status?: EmptyTrashModalStatus): SWRResponse<EmptyTrashModalStatus, Error> & EmptyTrashModalStatusUtils => {
- const initialData: EmptyTrashModalStatus = {
- isOpened: false,
- pages: [],
- };
- const swrResponse = useStaticSWR<EmptyTrashModalStatus, Error>('emptyTrashModalStatus', status, { fallbackData: initialData });
- return {
- ...swrResponse,
- open: (
- pages?: IPageToDeleteWithMeta[],
- opts?: IEmptyTrashModalOption,
- ) => swrResponse.mutate({
- isOpened: true, pages, opts,
- }),
- close: () => swrResponse.mutate({ isOpened: false }),
- };
- };
- /*
- * PageDuplicateModal
- */
- export type IPageForPageDuplicateModal = {
- pageId: string,
- path: string
- }
- export type IDuplicateModalOption = {
- onDuplicated?: OnDuplicatedFunction,
- }
- type DuplicateModalStatus = {
- isOpened: boolean,
- page?: IPageForPageDuplicateModal,
- opts?: IDuplicateModalOption,
- }
- type DuplicateModalStatusUtils = {
- open(
- page?: IPageForPageDuplicateModal,
- opts?: IDuplicateModalOption
- ): Promise<DuplicateModalStatus | undefined>
- close(): Promise<DuplicateModalStatus | undefined>
- }
- export const usePageDuplicateModal = (status?: DuplicateModalStatus): SWRResponse<DuplicateModalStatus, Error> & DuplicateModalStatusUtils => {
- const initialData: DuplicateModalStatus = { isOpened: false };
- const swrResponse = useStaticSWR<DuplicateModalStatus, Error>('duplicateModalStatus', status, { fallbackData: initialData });
- return {
- ...swrResponse,
- open: (
- page?: IPageForPageDuplicateModal,
- opts?: IDuplicateModalOption,
- ) => swrResponse.mutate({ isOpened: true, page, opts }),
- close: () => swrResponse.mutate({ isOpened: false }),
- };
- };
- /*
- * PageRenameModal
- */
- export type IRenameModalOption = {
- onRenamed?: OnRenamedFunction,
- }
- type RenameModalStatus = {
- isOpened: boolean,
- page?: IPageToRenameWithMeta,
- opts?: IRenameModalOption
- }
- type RenameModalStatusUtils = {
- open(
- page?: IPageToRenameWithMeta,
- opts?: IRenameModalOption
- ): Promise<RenameModalStatus | undefined>
- close(): Promise<RenameModalStatus | undefined>
- }
- export const usePageRenameModal = (status?: RenameModalStatus): SWRResponse<RenameModalStatus, Error> & RenameModalStatusUtils => {
- const initialData: RenameModalStatus = { isOpened: false };
- const swrResponse = useStaticSWR<RenameModalStatus, Error>('renameModalStatus', status, { fallbackData: initialData });
- return {
- ...swrResponse,
- open: (
- page?: IPageToRenameWithMeta,
- opts?: IRenameModalOption,
- ) => swrResponse.mutate({
- isOpened: true, page, opts,
- }),
- close: () => swrResponse.mutate({ isOpened: false }),
- };
- };
- /*
- * PutBackPageModal
- */
- export type IPageForPagePutBackModal = {
- pageId: string,
- path: string
- }
- export type IPutBackPageModalOption = {
- onPutBacked?: OnPutBackedFunction,
- }
- type PutBackPageModalStatus = {
- isOpened: boolean,
- page?: IPageForPagePutBackModal,
- opts?: IPutBackPageModalOption,
- }
- type PutBackPageModalUtils = {
- open(
- page?: IPageForPagePutBackModal,
- opts?: IPutBackPageModalOption,
- ): Promise<PutBackPageModalStatus | undefined>
- close():Promise<PutBackPageModalStatus | undefined>
- }
- export const usePutBackPageModal = (status?: PutBackPageModalStatus): SWRResponse<PutBackPageModalStatus, Error> & PutBackPageModalUtils => {
- const initialData: PutBackPageModalStatus = {
- isOpened: false,
- page: { pageId: '', path: '' },
- };
- const swrResponse = useStaticSWR<PutBackPageModalStatus, Error>('putBackPageModalStatus', status, { fallbackData: initialData });
- return {
- ...swrResponse,
- open: (
- page: IPageForPagePutBackModal, opts?: IPutBackPageModalOption,
- ) => swrResponse.mutate({
- isOpened: true, page, opts,
- }),
- close: () => swrResponse.mutate({ isOpened: false, page: { pageId: '', path: '' } }),
- };
- };
- /*
- * PagePresentationModal
- */
- type PresentationModalStatus = {
- isOpened: boolean,
- href?: string
- }
- type PresentationModalStatusUtils = {
- open(href: string): Promise<PresentationModalStatus | undefined>
- close(): Promise<PresentationModalStatus | undefined>
- }
- export const usePagePresentationModal = (
- status?: PresentationModalStatus,
- ): SWRResponse<PresentationModalStatus, Error> & PresentationModalStatusUtils => {
- const initialData: PresentationModalStatus = {
- isOpened: false, href: '?presentation=1',
- };
- const swrResponse = useStaticSWR<PresentationModalStatus, Error>('presentationModalStatus', status, { fallbackData: initialData });
- return {
- ...swrResponse,
- open: (href: string) => swrResponse.mutate({ isOpened: true, href }),
- close: () => swrResponse.mutate({ isOpened: false }),
- };
- };
- /*
- * PrivateLegacyPagesMigrationModal
- */
- export type ILegacyPrivatePage = { pageId: string, path: string };
- export type PrivateLegacyPagesMigrationModalSubmitedHandler = (pages: ILegacyPrivatePage[], isRecursively?: boolean) => void;
- type PrivateLegacyPagesMigrationModalStatus = {
- isOpened: boolean,
- pages?: ILegacyPrivatePage[],
- onSubmited?: PrivateLegacyPagesMigrationModalSubmitedHandler,
- }
- type PrivateLegacyPagesMigrationModalStatusUtils = {
- open(pages: ILegacyPrivatePage[], onSubmited?: PrivateLegacyPagesMigrationModalSubmitedHandler): Promise<PrivateLegacyPagesMigrationModalStatus | undefined>,
- close(): Promise<PrivateLegacyPagesMigrationModalStatus | undefined>,
- }
- export const usePrivateLegacyPagesMigrationModal = (
- status?: PrivateLegacyPagesMigrationModalStatus,
- ): SWRResponse<PrivateLegacyPagesMigrationModalStatus, Error> & PrivateLegacyPagesMigrationModalStatusUtils => {
- const initialData: PrivateLegacyPagesMigrationModalStatus = {
- isOpened: false,
- pages: [],
- };
- const swrResponse = useStaticSWR<PrivateLegacyPagesMigrationModalStatus, Error>('privateLegacyPagesMigrationModal', status, { fallbackData: initialData });
- return {
- ...swrResponse,
- open: (pages, onSubmited?) => swrResponse.mutate({
- isOpened: true, pages, onSubmited,
- }),
- close: () => swrResponse.mutate({ isOpened: false, pages: [], onSubmited: undefined }),
- };
- };
- /*
- * DescendantsPageListModal
- */
- type DescendantsPageListModalStatus = {
- isOpened: boolean,
- path?: string,
- }
- type DescendantsPageListUtils = {
- open(path: string): Promise<DescendantsPageListModalStatus | undefined>
- close(): Promise<DuplicateModalStatus | undefined>
- }
- export const useDescendantsPageListModal = (
- status?: DescendantsPageListModalStatus,
- ): SWRResponse<DescendantsPageListModalStatus, Error> & DescendantsPageListUtils => {
- const initialData: DescendantsPageListModalStatus = { isOpened: false };
- const swrResponse = useStaticSWR<DescendantsPageListModalStatus, Error>('descendantsPageListModalStatus', status, { fallbackData: initialData });
- return {
- ...swrResponse,
- open: (path: string) => swrResponse.mutate({ isOpened: true, path }),
- close: () => swrResponse.mutate({ isOpened: false }),
- };
- };
- /*
- * PageAccessoriesModal
- */
- export const PageAccessoriesModalContents = {
- PageHistory: 'PageHistory',
- Attachment: 'Attachment',
- ShareLink: 'ShareLink',
- } as const;
- export type PageAccessoriesModalContents = typeof PageAccessoriesModalContents[keyof typeof PageAccessoriesModalContents];
- type PageAccessoriesModalStatus = {
- isOpened: boolean,
- onOpened?: (initialActivatedContents: PageAccessoriesModalContents) => void,
- }
- type PageAccessoriesModalUtils = {
- open(activatedContents: PageAccessoriesModalContents): void
- close(): void
- }
- export const usePageAccessoriesModal = (): SWRResponse<PageAccessoriesModalStatus, Error> & PageAccessoriesModalUtils => {
- const initialStatus = { isOpened: false };
- const swrResponse = useStaticSWR<PageAccessoriesModalStatus, Error>('pageAccessoriesModalStatus', undefined, { fallbackData: initialStatus });
- return {
- ...swrResponse,
- open: (activatedContents: PageAccessoriesModalContents) => {
- if (swrResponse.data == null) {
- return;
- }
- swrResponse.mutate({ isOpened: true });
- if (swrResponse.data.onOpened != null) {
- swrResponse.data.onOpened(activatedContents);
- }
- },
- close: () => {
- if (swrResponse.data == null) {
- return;
- }
- swrResponse.mutate({ isOpened: false });
- },
- };
- };
- /*
- * UpdateUserGroupConfirmModal
- */
- type UpdateUserGroupConfirmModalStatus = {
- isOpened: boolean,
- targetGroup?: IUserGroupHasId,
- updateData?: Partial<IUserGroupHasId>,
- onConfirm?: (targetGroup: IUserGroupHasId, updateData: Partial<IUserGroupHasId>, forceUpdateParents: boolean) => any,
- }
- type UpdateUserGroupConfirmModalUtils = {
- open(targetGroup: IUserGroupHasId, updateData: Partial<IUserGroupHasId>, onConfirm?: (...args: any[]) => any): Promise<void>,
- close(): Promise<void>,
- }
- export const useUpdateUserGroupConfirmModal = (): SWRResponse<UpdateUserGroupConfirmModalStatus, Error> & UpdateUserGroupConfirmModalUtils => {
- const initialStatus: UpdateUserGroupConfirmModalStatus = { isOpened: false };
- const swrResponse = useStaticSWR<UpdateUserGroupConfirmModalStatus, Error>('updateParentConfirmModal', undefined, { fallbackData: initialStatus });
- return {
- ...swrResponse,
- async open(targetGroup: IUserGroupHasId, updateData: Partial<IUserGroupHasId>, onConfirm?: (...args: any[]) => any) {
- await swrResponse.mutate({
- isOpened: true, targetGroup, updateData, onConfirm,
- });
- },
- async close() {
- await swrResponse.mutate({ isOpened: false });
- },
- };
- };
- /*
- * ShortcutsModal
- */
- type ShortcutsModalStatus = {
- isOpened: boolean,
- }
- type ShortcutsModalUtils = {
- open(): void,
- close(): void,
- }
- export const useShortcutsModal = (): SWRResponse<ShortcutsModalStatus, Error> & ShortcutsModalUtils => {
- const initialStatus: ShortcutsModalStatus = { isOpened: false };
- const swrResponse = useStaticSWR<ShortcutsModalStatus, Error>('shortcutsModal', undefined, { fallbackData: initialStatus });
- return {
- ...swrResponse,
- open() {
- swrResponse.mutate({ isOpened: true });
- },
- close() {
- swrResponse.mutate({ isOpened: false });
- },
- };
- };
|