轻松聘
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

41 lines
1.1 KiB

/**
* 隐私中心(截图版)hook。
*/
import { useCallback } from 'react';
import { fetchPrivacyCenter, updatePrivacyCenterSetting } from '../api';
import type { PrivacyCenter, UpdateTogglePayload } from '../types';
import { useAsyncData } from './useAsyncData';
export interface PrivacyCenterState {
privacyCenter: PrivacyCenter | null;
loading: boolean;
error: Error | null;
refresh: () => void;
toggle: (id: string, enabled: boolean) => Promise<void>;
}
export function usePrivacyCenter(): PrivacyCenterState {
const fetcher = useCallback(async (): Promise<PrivacyCenter> => fetchPrivacyCenter(), []);
const { data, loading, error, refresh } = useAsyncData(fetcher);
const toggle = useCallback(
async (id: string, enabled: boolean) => {
const payload: UpdateTogglePayload = { id, enabled };
try {
await updatePrivacyCenterSetting(payload);
refresh();
} catch (err) {
console.error('[usePrivacyCenter] toggle error', err);
}
},
[refresh],
);
return {
privacyCenter: data ?? null,
loading,
error,
refresh,
toggle,
};
}