|
|
@@ -1,8 +1,7 @@
|
|
|
-import { useCallback, useMemo } from 'react';
|
|
|
+import { useCallback } from 'react';
|
|
|
|
|
|
-import type { SWRResponse } from 'swr';
|
|
|
import useSWR from 'swr';
|
|
|
-import useSWRMutation, { type SWRMutationResponse } from 'swr/mutation';
|
|
|
+import useSWRMutation from 'swr/mutation';
|
|
|
|
|
|
import { apiv3Get, apiv3Put } from '../util/apiv3-client';
|
|
|
|
|
|
@@ -24,66 +23,45 @@ interface ContentDispositionUpdateResponse {
|
|
|
currentDispositionSettings: ContentDispositionSettings;
|
|
|
}
|
|
|
|
|
|
-export const useSWRxContentDispositionSettings = (): SWRResponse<ContentDispositionSettings, Error> => {
|
|
|
- return useSWR(
|
|
|
+interface UseContentDisposition {
|
|
|
+ currentSettings: ContentDispositionSettings | undefined;
|
|
|
+ isLoading: boolean;
|
|
|
+ isUpdating: boolean;
|
|
|
+ updateSettings: (newSettings: ContentDispositionSettings) => Promise<ContentDispositionSettings>;
|
|
|
+}
|
|
|
+
|
|
|
+export const useContentDisposition = (): UseContentDisposition => {
|
|
|
+ const { data, isLoading, mutate } = useSWR(
|
|
|
'/content-disposition-settings/',
|
|
|
- endpoint => apiv3Get<ContentDispositionGetResponse>(endpoint).then((response) => {
|
|
|
- return response.data.currentDispositionSettings;
|
|
|
- }),
|
|
|
+ endpoint => apiv3Get<ContentDispositionGetResponse>(endpoint).then(res => res.data.currentDispositionSettings),
|
|
|
);
|
|
|
-};
|
|
|
|
|
|
-export const useSWRMUTxContentDispositionSettings = (): SWRMutationResponse<
|
|
|
- ContentDispositionSettings,
|
|
|
- Error,
|
|
|
- string,
|
|
|
- ContentDispositionUpdateRequest
|
|
|
-> => {
|
|
|
- return useSWRMutation(
|
|
|
+ const { trigger, isMutating: isUpdating } = useSWRMutation(
|
|
|
'/content-disposition-settings/',
|
|
|
async(endpoint: string, { arg }: { arg: ContentDispositionUpdateRequest }) => {
|
|
|
const response = await apiv3Put<ContentDispositionUpdateResponse>(endpoint, arg);
|
|
|
return response.data.currentDispositionSettings;
|
|
|
},
|
|
|
);
|
|
|
-};
|
|
|
-
|
|
|
-export const useContentDisposition = (): {
|
|
|
- currentSettings: ContentDispositionSettings | undefined;
|
|
|
- isLoading: boolean;
|
|
|
- isUpdating: boolean;
|
|
|
- updateSettings: (newSettings: ContentDispositionSettings) => Promise<ContentDispositionSettings>;
|
|
|
-} => {
|
|
|
- const {
|
|
|
- data, isLoading, mutate,
|
|
|
- } = useSWRxContentDispositionSettings();
|
|
|
- const { trigger, isMutating } = useSWRMUTxContentDispositionSettings();
|
|
|
-
|
|
|
- const inlineMimeTypesStr = data?.inlineMimeTypes?.join(',');
|
|
|
- const attachmentMimeTypesStr = data?.attachmentMimeTypes?.join(',');
|
|
|
-
|
|
|
- // eslint-disable-next-line react-hooks/exhaustive-deps -- intentionally using array contents instead of data object reference
|
|
|
- const memoizedData = useMemo(() => data, [inlineMimeTypesStr, attachmentMimeTypesStr]);
|
|
|
- const currentSettings = memoizedData;
|
|
|
|
|
|
const updateSettings = useCallback(async(newSettings: ContentDispositionSettings): Promise<ContentDispositionSettings> => {
|
|
|
-
|
|
|
const request: ContentDispositionUpdateRequest = {
|
|
|
newInlineMimeTypes: newSettings.inlineMimeTypes,
|
|
|
newAttachmentMimeTypes: newSettings.attachmentMimeTypes,
|
|
|
};
|
|
|
|
|
|
const updatedData = await trigger(request);
|
|
|
- mutate(updatedData, { revalidate: true });
|
|
|
+
|
|
|
+ // Update local cache and avoid an unnecessary extra GET request
|
|
|
+ await mutate(updatedData, { revalidate: false });
|
|
|
|
|
|
return updatedData;
|
|
|
}, [trigger, mutate]);
|
|
|
|
|
|
-
|
|
|
return {
|
|
|
- currentSettings,
|
|
|
+ currentSettings: data,
|
|
|
isLoading,
|
|
|
- isUpdating: isMutating,
|
|
|
+ isUpdating,
|
|
|
updateSettings,
|
|
|
};
|
|
|
};
|