Files

111 lines
4.1 KiB
TypeScript
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
import { useQuery, useMutation, useQueryClient, type QueryClient } from '@tanstack/react-query';
import { removeListItems, upsertListItem } from '@/api/queryData';
import {
clientCreateServerSubuserMutation,
clientDeleteServerSubuserMutation,
clientListServerSubusersOptions,
clientListServerSubusersQueryKey,
clientUpdateServerSubuserMutation,
} from '@/api/generated/@tanstack/react-query.gen';
import type {
ClientCreateServerSubuserData,
ClientDeleteServerSubuserData,
ClientListServerSubusersData,
ClientListServerSubusersResponse,
ClientServerSubuserAttributes,
ClientServerSubuserResource,
ClientUpdateServerSubuserData,
Options,
} from '@/api/generated';
import { notifyHttpError } from '@/plugins/notifications';
export type SubuserPermission = ClientServerSubuserAttributes['permissions'][number];
export type Subuser = ClientServerSubuserResource;
export interface CreateOrUpdateSubuserValues {
email: string;
permissions: SubuserPermission[];
}
const serverSubusersInput = (uuid: string): Options<ClientListServerSubusersData> => ({
path: { server_uuid: uuid },
});
export const serverSubusersQueryOptions = (uuid: string) => clientListServerSubusersOptions(serverSubusersInput(uuid));
export const useServerSubusers = (uuid: string) =>
useQuery({ ...serverSubusersQueryOptions(uuid), enabled: uuid.length > 0 });
export const createServerSubuserInput = (
uuid: string,
values: CreateOrUpdateSubuserValues
): Options<ClientCreateServerSubuserData> => ({
path: { server_uuid: uuid },
body: values,
});
2026-10-12 15:16:43 -04:00
/** `subuser` is the subuser's user uuid. */
2026-10-05 18:10:18 -04:00
export const updateServerSubuserInput = (
uuid: string,
2026-10-12 15:16:43 -04:00
subuser: string,
2026-10-05 18:10:18 -04:00
values: CreateOrUpdateSubuserValues
): Options<ClientUpdateServerSubuserData> => ({
2026-10-12 15:16:43 -04:00
path: { server_uuid: uuid, user: subuser },
2026-10-05 18:10:18 -04:00
body: { permissions: values.permissions },
});
2026-10-12 15:16:43 -04:00
/** `subuser` is the subuser's user uuid. */
export const deleteServerSubuserInput = (uuid: string, subuser: string): Options<ClientDeleteServerSubuserData> => ({
path: { server_uuid: uuid, user: subuser },
2026-10-05 18:10:18 -04:00
});
const upsertSubuser = async (queryClient: QueryClient, uuid: string, subuser: Subuser) => {
await queryClient.cancelQueries({ queryKey: clientListServerSubusersQueryKey(serverSubusersInput(uuid)) });
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
return queryClient.setQueryData<ClientListServerSubusersResponse>(
clientListServerSubusersQueryKey(serverSubusersInput(uuid)),
(current) => upsertListItem(current, subuser, (item) => item.attributes.uuid === subuser.attributes.uuid)
);
};
const removeSubuser = async (queryClient: QueryClient, uuid: string, subuserUuid: string) => {
await queryClient.cancelQueries({ queryKey: clientListServerSubusersQueryKey(serverSubusersInput(uuid)) });
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
return queryClient.setQueryData<ClientListServerSubusersResponse>(
clientListServerSubusersQueryKey(serverSubusersInput(uuid)),
(current) => removeListItems(current, (subuser) => subuser.attributes.uuid === subuserUuid)
);
};
export const useCreateSubuser = () => {
const queryClient = useQueryClient();
return useMutation({
...clientCreateServerSubuserMutation(),
onSuccess: (subuser, { path }) => upsertSubuser(queryClient, path.server_uuid, subuser),
onError: (error) => notifyHttpError(error, 'Unable to invite subuser'),
});
};
export const useUpdateSubuser = () => {
const queryClient = useQueryClient();
return useMutation({
...clientUpdateServerSubuserMutation(),
onSuccess: (subuser, { path }) => upsertSubuser(queryClient, path.server_uuid, subuser),
onError: (error) => notifyHttpError(error, 'Unable to update subuser'),
});
};
2026-10-12 15:16:43 -04:00
/** Reports failures; the caller announces success, since it knows the subuser's email. */
export const useDeleteSubuser = () => {
2026-10-05 18:10:18 -04:00
const queryClient = useQueryClient();
return useMutation({
...clientDeleteServerSubuserMutation(),
2026-10-12 15:16:43 -04:00
onSuccess: (_data, { path }) => removeSubuser(queryClient, path.server_uuid, path.user),
2026-10-05 18:10:18 -04:00
onError: (error) => notifyHttpError(error, 'Unable to remove subuser'),
});
};