Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/data/organizations/org-workspace-classes-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 { useQuery, useQueryClient } from "@tanstack/react-query";
8
import { organizationClient } from "../../service/public-api";
9
import { useCallback } from "react";
10
import { useCurrentOrg } from "./orgs-query";
11
import { WorkspaceClass } from "@gitpod/public-api/lib/gitpod/v1/workspace_pb";
12
import { noPersistence } from "../setup";
13
14
export function useOrgWorkspaceClassesQueryInvalidator() {
15
const organizationId = useCurrentOrg().data?.id;
16
const queryClient = useQueryClient();
17
return useCallback(() => {
18
queryClient.invalidateQueries(getQueryKey(organizationId));
19
}, [organizationId, queryClient]);
20
}
21
22
export function useOrgWorkspaceClassesQuery() {
23
const organizationId = useCurrentOrg().data?.id;
24
return useQuery<WorkspaceClass[], Error>(
25
getQueryKey(organizationId),
26
async () => {
27
if (!organizationId) {
28
throw new Error("No org selected.");
29
}
30
31
const settings = await organizationClient.listOrganizationWorkspaceClasses({ organizationId });
32
return settings.workspaceClasses || [];
33
},
34
{
35
enabled: !!organizationId,
36
},
37
);
38
}
39
40
function getQueryKey(organizationId?: string) {
41
// We don't persistence listOrganizationWorkspaceClasses because org settings updated by owner will not notify members to invalidate listOrganizationWorkspaceClasses
42
// TODO: Or we need to handle special ErrorCodes from server somewhere
43
// i.e. CreateAndStartWorkspace respond selected workspace class is not allowed
44
return noPersistence(["listOrganizationWorkspaceClasses", organizationId || "undefined"]);
45
}
46
47