| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import useSWR, { SWRResponse } from 'swr';
- import useSWRImmutable from 'swr/immutable';
- import { apiGet } from '~/client/util/apiv1-client';
- import { apiv3Get, apiv3Put } from '~/client/util/apiv3-client';
- import { Nullable } from '~/interfaces/common';
- import { IEditorSettings } from '~/interfaces/editor-settings';
- import { SlackChannels } from '~/interfaces/user-trigger-notification';
- import { useCurrentUser, useDefaultIndentSize, useIsGuestUser } from './context';
- import { localStorageMiddleware } from './middlewares/sync-to-storage';
- import { useStaticSWR } from './use-static-swr';
- type EditorSettingsOperation = {
- update: (updateData: Partial<IEditorSettings>) => void,
- turnOffAskingBeforeDownloadLargeFiles: () => void,
- }
- export const useEditorSettings = (): SWRResponse<IEditorSettings, Error> & EditorSettingsOperation => {
- const { data: currentUser } = useCurrentUser();
- const { data: isGuestUser } = useIsGuestUser();
- const swrResult = useSWRImmutable<IEditorSettings>(
- isGuestUser ? null : ['/personal-setting/editor-settings', currentUser?.username],
- endpoint => apiv3Get(endpoint).then(result => result.data),
- { use: [localStorageMiddleware] }, // store to localStorage for initialization fastly
- );
- return {
- ...swrResult,
- update: (updateData) => {
- const { data, mutate } = swrResult;
- if (data == null) {
- return;
- }
- mutate({ ...data, ...updateData }, false);
- // invoke API
- apiv3Put('/personal-setting/editor-settings', updateData);
- },
- turnOffAskingBeforeDownloadLargeFiles: async() => {
- const { data, mutate } = swrResult;
- if (data == null) {
- return;
- }
- // invoke API
- await apiv3Put('/personal-setting/editor-settings', { textlintSettings: { neverAskBeforeDownloadLargeFiles: true } });
- // revalidate
- mutate();
- },
- };
- };
- export const useIsTextlintEnabled = (): SWRResponse<boolean, Error> => {
- return useStaticSWR<boolean, Error>('isTextlintEnabled', undefined, { fallbackData: false });
- };
- export const useCurrentIndentSize = (): SWRResponse<number, Error> => {
- const { data: defaultIndentSize } = useDefaultIndentSize();
- return useStaticSWR<number, Error>(
- defaultIndentSize == null ? null : 'currentIndentSize',
- undefined,
- { fallbackData: defaultIndentSize },
- );
- };
- /*
- * Slack Notification
- */
- // export const useIsSlackEnabled = (isEnabled?: boolean): SWRResponse<boolean, Error> => {
- // return useStaticSWR('isSlackEnabled', isEnabled, { fallbackData: false });
- // };
- export const useSWRxIsSlackEnabled = (isEnabled: boolean): SWRResponse<boolean, Error> => {
- // console.log({ isEnabled });
- return useSWR(['isSlackEnabled', isEnabled], (key: string, isEnabled: boolean) => isEnabled);
- };
- export const useSWRxSlackChannels = (path: Nullable<string>): SWRResponse<Nullable<string[]>, Error> => {
- const shouldFetch: boolean = path != null;
- return useSWR(
- shouldFetch ? ['/pages.updatePost', path] : null,
- (endpoint, path) => apiGet(endpoint, { path }).then((response: SlackChannels) => response.updatePost),
- );
- };
|