buster/apps/web/src/api/buster_rest/security/queryRequests.ts

123 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-07-09 06:48:13 +08:00
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { securityQueryKeys } from '@/api/query_keys/security';
import {
getWorkspaceSettings,
getInviteLink,
getApprovedDomains,
updateWorkspaceSettings,
updateInviteLinks,
refreshInviteLink,
addApprovedDomain,
removeApprovedDomain
} from './requests';
2025-07-09 23:42:16 +08:00
import type {
GetApprovedDomainsResponse,
GetWorkspaceSettingsResponse
} from '@buster/server-shared/security';
2025-07-09 06:48:13 +08:00
export const useGetWorkspaceSettings = () => {
return useQuery({
...securityQueryKeys.securityGetWorkspaceSettings,
2025-07-09 23:42:16 +08:00
queryFn: getWorkspaceSettings,
initialData: {
restrict_new_user_invitations: false,
default_role: 'viewer',
default_datasets: []
} satisfies GetWorkspaceSettingsResponse
2025-07-09 06:48:13 +08:00
});
};
export const useGetInviteLink = () => {
return useQuery({
...securityQueryKeys.securityInviteLink,
queryFn: getInviteLink
});
};
export const useGetApprovedDomains = () => {
return useQuery({
...securityQueryKeys.securityApprovedDomains,
2025-07-09 23:42:16 +08:00
queryFn: getApprovedDomains,
initialData: []
2025-07-09 06:48:13 +08:00
});
};
export const useUpdateWorkspaceSettings = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: updateWorkspaceSettings,
onSuccess: (data) => {
queryClient.setQueryData(securityQueryKeys.securityGetWorkspaceSettings.queryKey, data);
}
});
};
export const useUpdateInviteLinks = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: updateInviteLinks,
2025-07-09 07:20:31 +08:00
onMutate: (variables) => {
queryClient.setQueryData(securityQueryKeys.securityInviteLink.queryKey, (prev) => {
if (!prev) return prev;
return {
...prev,
...variables
};
});
},
2025-07-09 06:48:13 +08:00
onSuccess: (data) => {
queryClient.setQueryData(securityQueryKeys.securityInviteLink.queryKey, data);
}
});
};
export const useRefreshInviteLink = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: refreshInviteLink,
onSuccess: (data) => {
queryClient.setQueryData(securityQueryKeys.securityInviteLink.queryKey, data);
}
});
};
export const useAddApprovedDomain = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: addApprovedDomain,
2025-07-09 07:20:31 +08:00
onMutate: (variables) => {
queryClient.setQueryData(securityQueryKeys.securityApprovedDomains.queryKey, (prev) => {
if (!prev) return prev;
return {
...prev,
...variables.domains.map((domain) => ({
domain,
created_at: new Date().toISOString()
}))
} satisfies GetApprovedDomainsResponse;
});
},
2025-07-09 06:48:13 +08:00
onSuccess: (data) => {
queryClient.setQueryData(securityQueryKeys.securityApprovedDomains.queryKey, data);
}
});
};
export const useRemoveApprovedDomain = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: removeApprovedDomain,
2025-07-09 07:20:31 +08:00
onMutate: (variables) => {
queryClient.setQueryData(securityQueryKeys.securityApprovedDomains.queryKey, (prev) => {
if (!prev) return prev;
return prev.filter(
(domain) => !variables.domains.includes(domain.domain)
) satisfies GetApprovedDomainsResponse;
});
},
2025-07-09 06:48:13 +08:00
onSuccess: (data) => {
queryClient.setQueryData(securityQueryKeys.securityApprovedDomains.queryKey, data);
}
});
};