modal.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. import { SWRResponse } from 'swr';
  2. import { IPageToDeleteWithMeta, IPageToRenameWithMeta } from '~/interfaces/page';
  3. import {
  4. OnDuplicatedFunction, OnRenamedFunction, OnDeletedFunction, OnPutBackedFunction,
  5. } from '~/interfaces/ui';
  6. import { IUserGroupHasId } from '~/interfaces/user';
  7. import { useStaticSWR } from './use-static-swr';
  8. /*
  9. * PageCreateModal
  10. */
  11. type CreateModalStatus = {
  12. isOpened: boolean,
  13. path?: string,
  14. }
  15. type CreateModalStatusUtils = {
  16. open(path?: string): Promise<CreateModalStatus | undefined>
  17. close(): Promise<CreateModalStatus | undefined>
  18. }
  19. export const usePageCreateModal = (status?: CreateModalStatus): SWRResponse<CreateModalStatus, Error> & CreateModalStatusUtils => {
  20. const initialData: CreateModalStatus = { isOpened: false };
  21. const swrResponse = useStaticSWR<CreateModalStatus, Error>('pageCreateModalStatus', status, { fallbackData: initialData });
  22. return {
  23. ...swrResponse,
  24. open: (path?: string) => swrResponse.mutate({ isOpened: true, path }),
  25. close: () => swrResponse.mutate({ isOpened: false }),
  26. };
  27. };
  28. /*
  29. * PageDeleteModal
  30. */
  31. export type IDeleteModalOption = {
  32. onDeleted?: OnDeletedFunction,
  33. }
  34. type DeleteModalStatus = {
  35. isOpened: boolean,
  36. pages?: IPageToDeleteWithMeta[],
  37. opts?: IDeleteModalOption,
  38. }
  39. type DeleteModalStatusUtils = {
  40. open(
  41. pages?: IPageToDeleteWithMeta[],
  42. opts?: IDeleteModalOption,
  43. ): Promise<DeleteModalStatus | undefined>,
  44. close(): Promise<DeleteModalStatus | undefined>,
  45. }
  46. export const usePageDeleteModal = (status?: DeleteModalStatus): SWRResponse<DeleteModalStatus, Error> & DeleteModalStatusUtils => {
  47. const initialData: DeleteModalStatus = {
  48. isOpened: false,
  49. pages: [],
  50. };
  51. const swrResponse = useStaticSWR<DeleteModalStatus, Error>('deleteModalStatus', status, { fallbackData: initialData });
  52. return {
  53. ...swrResponse,
  54. open: (
  55. pages?: IPageToDeleteWithMeta[],
  56. opts?: IDeleteModalOption,
  57. ) => swrResponse.mutate({
  58. isOpened: true, pages, opts,
  59. }),
  60. close: () => swrResponse.mutate({ isOpened: false }),
  61. };
  62. };
  63. /*
  64. * EmptyTrashModal
  65. */
  66. type IEmptyTrashModalOption = {
  67. onEmptiedTrash?: () => void,
  68. canDelepeAllPages: boolean,
  69. }
  70. type EmptyTrashModalStatus = {
  71. isOpened: boolean,
  72. pages?: IPageToDeleteWithMeta[],
  73. opts?: IEmptyTrashModalOption,
  74. }
  75. type EmptyTrashModalStatusUtils = {
  76. open(
  77. pages?: IPageToDeleteWithMeta[],
  78. opts?: IEmptyTrashModalOption,
  79. ): Promise<EmptyTrashModalStatus | undefined>,
  80. close(): Promise<EmptyTrashModalStatus | undefined>,
  81. }
  82. export const useEmptyTrashModal = (status?: EmptyTrashModalStatus): SWRResponse<EmptyTrashModalStatus, Error> & EmptyTrashModalStatusUtils => {
  83. const initialData: EmptyTrashModalStatus = {
  84. isOpened: false,
  85. pages: [],
  86. };
  87. const swrResponse = useStaticSWR<EmptyTrashModalStatus, Error>('emptyTrashModalStatus', status, { fallbackData: initialData });
  88. return {
  89. ...swrResponse,
  90. open: (
  91. pages?: IPageToDeleteWithMeta[],
  92. opts?: IEmptyTrashModalOption,
  93. ) => swrResponse.mutate({
  94. isOpened: true, pages, opts,
  95. }),
  96. close: () => swrResponse.mutate({ isOpened: false }),
  97. };
  98. };
  99. /*
  100. * PageDuplicateModal
  101. */
  102. export type IPageForPageDuplicateModal = {
  103. pageId: string,
  104. path: string
  105. }
  106. export type IDuplicateModalOption = {
  107. onDuplicated?: OnDuplicatedFunction,
  108. }
  109. type DuplicateModalStatus = {
  110. isOpened: boolean,
  111. page?: IPageForPageDuplicateModal,
  112. opts?: IDuplicateModalOption,
  113. }
  114. type DuplicateModalStatusUtils = {
  115. open(
  116. page?: IPageForPageDuplicateModal,
  117. opts?: IDuplicateModalOption
  118. ): Promise<DuplicateModalStatus | undefined>
  119. close(): Promise<DuplicateModalStatus | undefined>
  120. }
  121. export const usePageDuplicateModal = (status?: DuplicateModalStatus): SWRResponse<DuplicateModalStatus, Error> & DuplicateModalStatusUtils => {
  122. const initialData: DuplicateModalStatus = { isOpened: false };
  123. const swrResponse = useStaticSWR<DuplicateModalStatus, Error>('duplicateModalStatus', status, { fallbackData: initialData });
  124. return {
  125. ...swrResponse,
  126. open: (
  127. page?: IPageForPageDuplicateModal,
  128. opts?: IDuplicateModalOption,
  129. ) => swrResponse.mutate({ isOpened: true, page, opts }),
  130. close: () => swrResponse.mutate({ isOpened: false }),
  131. };
  132. };
  133. /*
  134. * PageRenameModal
  135. */
  136. export type IRenameModalOption = {
  137. onRenamed?: OnRenamedFunction,
  138. }
  139. type RenameModalStatus = {
  140. isOpened: boolean,
  141. page?: IPageToRenameWithMeta,
  142. opts?: IRenameModalOption
  143. }
  144. type RenameModalStatusUtils = {
  145. open(
  146. page?: IPageToRenameWithMeta,
  147. opts?: IRenameModalOption
  148. ): Promise<RenameModalStatus | undefined>
  149. close(): Promise<RenameModalStatus | undefined>
  150. }
  151. export const usePageRenameModal = (status?: RenameModalStatus): SWRResponse<RenameModalStatus, Error> & RenameModalStatusUtils => {
  152. const initialData: RenameModalStatus = { isOpened: false };
  153. const swrResponse = useStaticSWR<RenameModalStatus, Error>('renameModalStatus', status, { fallbackData: initialData });
  154. return {
  155. ...swrResponse,
  156. open: (
  157. page?: IPageToRenameWithMeta,
  158. opts?: IRenameModalOption,
  159. ) => swrResponse.mutate({
  160. isOpened: true, page, opts,
  161. }),
  162. close: () => swrResponse.mutate({ isOpened: false }),
  163. };
  164. };
  165. /*
  166. * PutBackPageModal
  167. */
  168. export type IPageForPagePutBackModal = {
  169. pageId: string,
  170. path: string
  171. }
  172. export type IPutBackPageModalOption = {
  173. onPutBacked?: OnPutBackedFunction,
  174. }
  175. type PutBackPageModalStatus = {
  176. isOpened: boolean,
  177. page?: IPageForPagePutBackModal,
  178. opts?: IPutBackPageModalOption,
  179. }
  180. type PutBackPageModalUtils = {
  181. open(
  182. page?: IPageForPagePutBackModal,
  183. opts?: IPutBackPageModalOption,
  184. ): Promise<PutBackPageModalStatus | undefined>
  185. close():Promise<PutBackPageModalStatus | undefined>
  186. }
  187. export const usePutBackPageModal = (status?: PutBackPageModalStatus): SWRResponse<PutBackPageModalStatus, Error> & PutBackPageModalUtils => {
  188. const initialData: PutBackPageModalStatus = {
  189. isOpened: false,
  190. page: { pageId: '', path: '' },
  191. };
  192. const swrResponse = useStaticSWR<PutBackPageModalStatus, Error>('putBackPageModalStatus', status, { fallbackData: initialData });
  193. return {
  194. ...swrResponse,
  195. open: (
  196. page: IPageForPagePutBackModal, opts?: IPutBackPageModalOption,
  197. ) => swrResponse.mutate({
  198. isOpened: true, page, opts,
  199. }),
  200. close: () => swrResponse.mutate({ isOpened: false, page: { pageId: '', path: '' } }),
  201. };
  202. };
  203. /*
  204. * PagePresentationModal
  205. */
  206. type PresentationModalStatus = {
  207. isOpened: boolean,
  208. href?: string
  209. }
  210. type PresentationModalStatusUtils = {
  211. open(href: string): Promise<PresentationModalStatus | undefined>
  212. close(): Promise<PresentationModalStatus | undefined>
  213. }
  214. export const usePagePresentationModal = (
  215. status?: PresentationModalStatus,
  216. ): SWRResponse<PresentationModalStatus, Error> & PresentationModalStatusUtils => {
  217. const initialData: PresentationModalStatus = {
  218. isOpened: false, href: '?presentation=1',
  219. };
  220. const swrResponse = useStaticSWR<PresentationModalStatus, Error>('presentationModalStatus', status, { fallbackData: initialData });
  221. return {
  222. ...swrResponse,
  223. open: (href: string) => swrResponse.mutate({ isOpened: true, href }),
  224. close: () => swrResponse.mutate({ isOpened: false }),
  225. };
  226. };
  227. /*
  228. * PrivateLegacyPagesMigrationModal
  229. */
  230. export type ILegacyPrivatePage = { pageId: string, path: string };
  231. export type PrivateLegacyPagesMigrationModalSubmitedHandler = (pages: ILegacyPrivatePage[], isRecursively?: boolean) => void;
  232. type PrivateLegacyPagesMigrationModalStatus = {
  233. isOpened: boolean,
  234. pages?: ILegacyPrivatePage[],
  235. onSubmited?: PrivateLegacyPagesMigrationModalSubmitedHandler,
  236. }
  237. type PrivateLegacyPagesMigrationModalStatusUtils = {
  238. open(pages: ILegacyPrivatePage[], onSubmited?: PrivateLegacyPagesMigrationModalSubmitedHandler): Promise<PrivateLegacyPagesMigrationModalStatus | undefined>,
  239. close(): Promise<PrivateLegacyPagesMigrationModalStatus | undefined>,
  240. }
  241. export const usePrivateLegacyPagesMigrationModal = (
  242. status?: PrivateLegacyPagesMigrationModalStatus,
  243. ): SWRResponse<PrivateLegacyPagesMigrationModalStatus, Error> & PrivateLegacyPagesMigrationModalStatusUtils => {
  244. const initialData: PrivateLegacyPagesMigrationModalStatus = {
  245. isOpened: false,
  246. pages: [],
  247. };
  248. const swrResponse = useStaticSWR<PrivateLegacyPagesMigrationModalStatus, Error>('privateLegacyPagesMigrationModal', status, { fallbackData: initialData });
  249. return {
  250. ...swrResponse,
  251. open: (pages, onSubmited?) => swrResponse.mutate({
  252. isOpened: true, pages, onSubmited,
  253. }),
  254. close: () => swrResponse.mutate({ isOpened: false, pages: [], onSubmited: undefined }),
  255. };
  256. };
  257. /*
  258. * DescendantsPageListModal
  259. */
  260. type DescendantsPageListModalStatus = {
  261. isOpened: boolean,
  262. path?: string,
  263. }
  264. type DescendantsPageListUtils = {
  265. open(path: string): Promise<DescendantsPageListModalStatus | undefined>
  266. close(): Promise<DuplicateModalStatus | undefined>
  267. }
  268. export const useDescendantsPageListModal = (
  269. status?: DescendantsPageListModalStatus,
  270. ): SWRResponse<DescendantsPageListModalStatus, Error> & DescendantsPageListUtils => {
  271. const initialData: DescendantsPageListModalStatus = { isOpened: false };
  272. const swrResponse = useStaticSWR<DescendantsPageListModalStatus, Error>('descendantsPageListModalStatus', status, { fallbackData: initialData });
  273. return {
  274. ...swrResponse,
  275. open: (path: string) => swrResponse.mutate({ isOpened: true, path }),
  276. close: () => swrResponse.mutate({ isOpened: false }),
  277. };
  278. };
  279. /*
  280. * PageAccessoriesModal
  281. */
  282. export const PageAccessoriesModalContents = {
  283. PageHistory: 'PageHistory',
  284. Attachment: 'Attachment',
  285. ShareLink: 'ShareLink',
  286. } as const;
  287. export type PageAccessoriesModalContents = typeof PageAccessoriesModalContents[keyof typeof PageAccessoriesModalContents];
  288. type PageAccessoriesModalStatus = {
  289. isOpened: boolean,
  290. onOpened?: (initialActivatedContents: PageAccessoriesModalContents) => void,
  291. }
  292. type PageAccessoriesModalUtils = {
  293. open(activatedContents: PageAccessoriesModalContents): void
  294. close(): void
  295. }
  296. export const usePageAccessoriesModal = (): SWRResponse<PageAccessoriesModalStatus, Error> & PageAccessoriesModalUtils => {
  297. const initialStatus = { isOpened: false };
  298. const swrResponse = useStaticSWR<PageAccessoriesModalStatus, Error>('pageAccessoriesModalStatus', undefined, { fallbackData: initialStatus });
  299. return {
  300. ...swrResponse,
  301. open: (activatedContents: PageAccessoriesModalContents) => {
  302. if (swrResponse.data == null) {
  303. return;
  304. }
  305. swrResponse.mutate({ isOpened: true });
  306. if (swrResponse.data.onOpened != null) {
  307. swrResponse.data.onOpened(activatedContents);
  308. }
  309. },
  310. close: () => {
  311. if (swrResponse.data == null) {
  312. return;
  313. }
  314. swrResponse.mutate({ isOpened: false });
  315. },
  316. };
  317. };
  318. /*
  319. * UpdateUserGroupConfirmModal
  320. */
  321. type UpdateUserGroupConfirmModalStatus = {
  322. isOpened: boolean,
  323. targetGroup?: IUserGroupHasId,
  324. updateData?: Partial<IUserGroupHasId>,
  325. onConfirm?: (targetGroup: IUserGroupHasId, updateData: Partial<IUserGroupHasId>, forceUpdateParents: boolean) => any,
  326. }
  327. type UpdateUserGroupConfirmModalUtils = {
  328. open(targetGroup: IUserGroupHasId, updateData: Partial<IUserGroupHasId>, onConfirm?: (...args: any[]) => any): Promise<void>,
  329. close(): Promise<void>,
  330. }
  331. export const useUpdateUserGroupConfirmModal = (): SWRResponse<UpdateUserGroupConfirmModalStatus, Error> & UpdateUserGroupConfirmModalUtils => {
  332. const initialStatus: UpdateUserGroupConfirmModalStatus = { isOpened: false };
  333. const swrResponse = useStaticSWR<UpdateUserGroupConfirmModalStatus, Error>('updateParentConfirmModal', undefined, { fallbackData: initialStatus });
  334. return {
  335. ...swrResponse,
  336. async open(targetGroup: IUserGroupHasId, updateData: Partial<IUserGroupHasId>, onConfirm?: (...args: any[]) => any) {
  337. await swrResponse.mutate({
  338. isOpened: true, targetGroup, updateData, onConfirm,
  339. });
  340. },
  341. async close() {
  342. await swrResponse.mutate({ isOpened: false });
  343. },
  344. };
  345. };
  346. /*
  347. * ShortcutsModal
  348. */
  349. type ShortcutsModalStatus = {
  350. isOpened: boolean,
  351. }
  352. type ShortcutsModalUtils = {
  353. open(): void,
  354. close(): void,
  355. }
  356. export const useShortcutsModal = (): SWRResponse<ShortcutsModalStatus, Error> & ShortcutsModalUtils => {
  357. const initialStatus: ShortcutsModalStatus = { isOpened: false };
  358. const swrResponse = useStaticSWR<ShortcutsModalStatus, Error>('shortcutsModal', undefined, { fallbackData: initialStatus });
  359. return {
  360. ...swrResponse,
  361. open() {
  362. swrResponse.mutate({ isOpened: true });
  363. },
  364. close() {
  365. swrResponse.mutate({ isOpened: false });
  366. },
  367. };
  368. };