tianji/src/client/hooks/useConfig.ts

34 lines
806 B
TypeScript
Raw Normal View History

2024-03-06 18:14:32 +00:00
import { once } from 'lodash-es';
import { AppRouterOutput, trpc } from '../api/trpc';
2024-03-06 18:14:32 +00:00
import { anonymousTelemetryUrl } from '../utils/env';
const defaultGlobalConfig: AppRouterOutput['global']['config'] = {
allowRegister: false,
2024-02-17 03:45:11 +00:00
alphaMode: false,
2024-03-06 18:14:32 +00:00
disableAnonymousTelemetry: false,
2024-08-02 19:44:43 +00:00
authProvider: [],
};
2024-03-06 18:14:32 +00:00
const callAnonymousTelemetry = once(() => {
fetch(anonymousTelemetryUrl);
});
/**
* Fetch settings from server
*/
export function useGlobalConfig(): AppRouterOutput['global']['config'] {
const { data = defaultGlobalConfig } = trpc.global.config.useQuery(
undefined,
{
staleTime: 1000 * 60 * 60 * 1, // 1 hour
2024-03-06 18:14:32 +00:00
onSuccess(data) {
if (data.disableAnonymousTelemetry !== true) {
callAnonymousTelemetry();
}
},
}
);
return data;
}