Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/data/organizations/invite-query.ts
2501 views
1
/**
2
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
8
import { useCallback } from "react";
9
import { organizationClient } from "../../service/public-api";
10
import { useCurrentOrg } from "./orgs-query";
11
12
export function useInviteInvalidator() {
13
const organizationId = useCurrentOrg().data?.id;
14
const queryClient = useQueryClient();
15
return useCallback(() => {
16
queryClient.invalidateQueries(getQueryKey(organizationId));
17
}, [organizationId, queryClient]);
18
}
19
20
export function useInvitationId() {
21
const organizationId = useCurrentOrg().data?.id;
22
const query = useQuery<string, Error>(
23
getQueryKey(organizationId),
24
async () => {
25
const response = await organizationClient.getOrganizationInvitation({
26
organizationId,
27
});
28
return response.invitationId;
29
},
30
{
31
enabled: !!organizationId,
32
},
33
);
34
return query;
35
}
36
37
export function useResetInvitationId() {
38
const invalidate = useInviteInvalidator();
39
return useMutation<void, Error, string>({
40
mutationFn: async (orgId) => {
41
if (!orgId) {
42
throw new Error("No current organization selected");
43
}
44
45
await organizationClient.resetOrganizationInvitation({
46
organizationId: orgId,
47
});
48
//TODO update useInvitation Query
49
},
50
onSuccess(updatedOrg) {
51
invalidate();
52
},
53
});
54
}
55
56
function getQueryKey(organizationId: string | undefined) {
57
return ["invitationId", organizationId || "undefined"];
58
}
59
60