2025-03-06 03:14:24 +08:00
|
|
|
import { useMutation, useQuery } from '@tanstack/react-query';
|
2025-02-18 08:30:05 +08:00
|
|
|
import {
|
|
|
|
getUser,
|
|
|
|
getUser_server,
|
|
|
|
updateOrganizationUser,
|
|
|
|
getMyUserInfo,
|
2025-03-11 12:36:25 +08:00
|
|
|
getMyUserInfo_server,
|
|
|
|
getUserFavorites,
|
|
|
|
getUserFavorites_server,
|
|
|
|
createUserFavorite,
|
|
|
|
deleteUserFavorite,
|
|
|
|
updateUserFavorites,
|
2025-03-14 04:31:54 +08:00
|
|
|
inviteUser,
|
2025-03-11 12:36:25 +08:00
|
|
|
getUserList,
|
2025-03-14 04:31:54 +08:00
|
|
|
getUserList_server
|
2025-02-18 08:30:05 +08:00
|
|
|
} from './requests';
|
2025-03-08 07:02:56 +08:00
|
|
|
import { useMemoizedFn } from '@/hooks';
|
2025-01-18 00:46:13 +08:00
|
|
|
import { QueryClient, useQueryClient } from '@tanstack/react-query';
|
2025-02-15 12:02:25 +08:00
|
|
|
import { queryKeys } from '@/api/query_keys';
|
2025-03-13 04:28:32 +08:00
|
|
|
import { useBusterNotifications } from '@/context/BusterNotifications';
|
2025-03-20 01:41:41 +08:00
|
|
|
import { useCreateOrganization } from '../organizations';
|
2025-02-15 12:02:25 +08:00
|
|
|
|
|
|
|
export const useGetMyUserInfo = () => {
|
2025-03-06 03:14:24 +08:00
|
|
|
return useQuery({
|
2025-02-18 13:22:32 +08:00
|
|
|
...queryKeys.userGetUserMyself,
|
2025-03-12 01:15:37 +08:00
|
|
|
queryFn: getMyUserInfo,
|
2025-02-15 12:20:22 +08:00
|
|
|
enabled: false //This is a server only query
|
2025-02-15 12:02:25 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2025-02-18 08:30:05 +08:00
|
|
|
export const prefetchGetMyUserInfo = async (
|
|
|
|
params: Parameters<typeof getMyUserInfo_server>[0],
|
|
|
|
queryClientProp?: QueryClient
|
|
|
|
) => {
|
2025-02-15 12:02:25 +08:00
|
|
|
const queryClient = queryClientProp || new QueryClient();
|
2025-02-18 08:30:05 +08:00
|
|
|
const initialData = await getMyUserInfo_server(params);
|
2025-02-15 12:02:25 +08:00
|
|
|
await queryClient.prefetchQuery({
|
2025-02-18 13:22:32 +08:00
|
|
|
...queryKeys.userGetUserMyself,
|
2025-02-19 05:15:01 +08:00
|
|
|
queryFn: () => initialData!,
|
2025-02-18 08:30:05 +08:00
|
|
|
initialData
|
2025-02-15 12:02:25 +08:00
|
|
|
});
|
2025-04-22 05:01:17 +08:00
|
|
|
return queryClient;
|
2025-02-15 12:02:25 +08:00
|
|
|
};
|
2025-01-18 00:46:13 +08:00
|
|
|
|
|
|
|
export const useGetUser = (params: Parameters<typeof getUser>[0]) => {
|
2025-02-15 12:20:22 +08:00
|
|
|
const queryFn = useMemoizedFn(() => getUser(params));
|
2025-03-06 03:14:24 +08:00
|
|
|
return useQuery({
|
2025-02-18 13:22:32 +08:00
|
|
|
...queryKeys.userGetUser(params.userId),
|
2025-03-06 03:14:24 +08:00
|
|
|
queryFn
|
2025-01-18 00:46:13 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useUpdateUser = () => {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
const mutationFn = useMemoizedFn(async (params: Parameters<typeof updateOrganizationUser>[0]) => {
|
2025-02-18 13:22:32 +08:00
|
|
|
const options = queryKeys.userGetUser(params.userId);
|
2025-02-15 12:20:22 +08:00
|
|
|
queryClient.setQueryData(options.queryKey, (oldData) => {
|
|
|
|
return {
|
|
|
|
...oldData!,
|
|
|
|
...params
|
|
|
|
};
|
|
|
|
});
|
2025-01-18 00:46:13 +08:00
|
|
|
const res = await updateOrganizationUser(params);
|
|
|
|
return res;
|
|
|
|
});
|
|
|
|
|
2025-03-06 03:14:24 +08:00
|
|
|
return useMutation({
|
2025-01-18 00:46:13 +08:00
|
|
|
mutationFn: mutationFn
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const prefetchGetUser = async (userId: string, queryClientProp?: QueryClient) => {
|
|
|
|
const queryClient = queryClientProp || new QueryClient();
|
|
|
|
await queryClient.prefetchQuery({
|
2025-02-18 13:22:32 +08:00
|
|
|
...queryKeys.userGetUser(userId),
|
2025-01-18 00:46:13 +08:00
|
|
|
queryFn: () => getUser_server({ userId })
|
|
|
|
});
|
|
|
|
return queryClient;
|
|
|
|
};
|
2025-03-11 12:36:25 +08:00
|
|
|
|
|
|
|
export const useGetUserFavorites = () => {
|
|
|
|
const queryFn = useMemoizedFn(async () => getUserFavorites());
|
|
|
|
return useQuery({
|
|
|
|
...queryKeys.favoritesGetList,
|
|
|
|
queryFn
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const prefetchGetUserFavorites = async (queryClientProp?: QueryClient) => {
|
|
|
|
const queryClient = queryClientProp || new QueryClient();
|
|
|
|
await queryClient.prefetchQuery({
|
|
|
|
...queryKeys.favoritesGetList,
|
|
|
|
queryFn: () => getUserFavorites_server()
|
|
|
|
});
|
|
|
|
return queryClient;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useAddUserFavorite = () => {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: createUserFavorite,
|
|
|
|
onMutate: (params) => {
|
|
|
|
queryClient.setQueryData(queryKeys.favoritesGetList.queryKey, (prev) => {
|
2025-04-09 23:54:40 +08:00
|
|
|
const prevIds = prev?.map((p) => p.id) || [];
|
|
|
|
const dedupedAdd = params.filter((p) => !prevIds.includes(p.id));
|
|
|
|
return [...dedupedAdd, ...(prev || [])];
|
2025-03-11 12:36:25 +08:00
|
|
|
});
|
|
|
|
},
|
2025-03-14 04:31:54 +08:00
|
|
|
onSuccess: (data) => {
|
|
|
|
queryClient.setQueryData(queryKeys.favoritesGetList.queryKey, data);
|
2025-03-11 12:36:25 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useDeleteUserFavorite = () => {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: deleteUserFavorite,
|
2025-03-14 04:31:54 +08:00
|
|
|
onMutate: (id) => {
|
2025-03-11 12:36:25 +08:00
|
|
|
queryClient.setQueryData(queryKeys.favoritesGetList.queryKey, (prev) => {
|
2025-04-09 23:54:40 +08:00
|
|
|
return prev?.filter((fav) => !id.includes(fav.id));
|
2025-03-11 12:36:25 +08:00
|
|
|
});
|
|
|
|
},
|
2025-03-14 04:31:54 +08:00
|
|
|
onSuccess: (data) => {
|
|
|
|
queryClient.setQueryData(queryKeys.favoritesGetList.queryKey, data);
|
2025-03-11 12:36:25 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useUpdateUserFavorites = () => {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: updateUserFavorites,
|
|
|
|
onMutate: (params) => {
|
|
|
|
queryClient.setQueryData(queryKeys.favoritesGetList.queryKey, (prev) => {
|
|
|
|
return prev?.filter((fav, index) => {
|
2025-03-22 12:21:42 +08:00
|
|
|
const id = fav.id;
|
|
|
|
const favorite = (prev || []).find((f) => f.id === id)!;
|
2025-03-11 12:36:25 +08:00
|
|
|
return { ...favorite, index };
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2025-03-20 01:41:41 +08:00
|
|
|
export const useGetUserList = (params: Parameters<typeof getUserList>[0]) => {
|
2025-03-11 12:36:25 +08:00
|
|
|
const queryFn = useMemoizedFn(() => getUserList(params));
|
|
|
|
|
|
|
|
return useQuery({
|
|
|
|
...queryKeys.userGetUserList(params),
|
|
|
|
queryFn
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const prefetchGetUserList = async (
|
2025-03-20 01:41:41 +08:00
|
|
|
params: Parameters<typeof getUserList>[0],
|
2025-03-11 12:36:25 +08:00
|
|
|
queryClientProp?: QueryClient
|
|
|
|
) => {
|
|
|
|
const queryClient = queryClientProp || new QueryClient();
|
|
|
|
await queryClient.prefetchQuery({
|
|
|
|
...queryKeys.userGetUserList(params),
|
|
|
|
queryFn: () => getUserList_server(params)
|
|
|
|
});
|
|
|
|
return queryClient;
|
|
|
|
};
|
2025-03-13 04:28:32 +08:00
|
|
|
|
|
|
|
export const useInviteUser = () => {
|
2025-03-14 04:31:54 +08:00
|
|
|
const queryClient = useQueryClient();
|
2025-03-13 04:28:32 +08:00
|
|
|
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: inviteUser,
|
|
|
|
onSuccess: () => {
|
2025-03-14 04:31:54 +08:00
|
|
|
const user = queryClient.getQueryData(queryKeys.userGetUserMyself.queryKey);
|
|
|
|
const teamId = user?.organizations?.[0]?.id;
|
|
|
|
if (teamId) {
|
|
|
|
queryClient.invalidateQueries({
|
2025-04-26 04:46:18 +08:00
|
|
|
queryKey: [queryKeys.userGetUserList({ team_id: teamId }).queryKey],
|
|
|
|
refetchType: 'all'
|
2025-03-14 04:31:54 +08:00
|
|
|
});
|
|
|
|
}
|
2025-03-13 04:28:32 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useCreateUserOrganization = () => {
|
|
|
|
const { data: userResponse, refetch: refetchUserResponse } = useGetMyUserInfo();
|
|
|
|
const { mutateAsync: createOrganization } = useCreateOrganization();
|
|
|
|
const { mutateAsync: updateUserInfo } = useUpdateUser();
|
|
|
|
|
|
|
|
const onCreateUserOrganization = useMemoizedFn(
|
|
|
|
async ({ name, company }: { name: string; company: string }) => {
|
|
|
|
const alreadyHasOrganization = !!userResponse?.organizations?.[0];
|
|
|
|
|
|
|
|
if (!alreadyHasOrganization) await createOrganization({ name: company });
|
|
|
|
if (userResponse)
|
|
|
|
await updateUserInfo({
|
|
|
|
userId: userResponse.user.id,
|
|
|
|
name
|
|
|
|
});
|
|
|
|
|
|
|
|
await refetchUserResponse();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return onCreateUserOrganization;
|
|
|
|
};
|