Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/data/prebuilds/organization-prebuilds-query.ts
2501 views
1
/**
2
* Copyright (c) 2024 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 { useInfiniteQuery } from "@tanstack/react-query";
8
import { prebuildClient } from "../../service/public-api";
9
import { ListOrganizationPrebuildsRequest_Filter } from "@gitpod/public-api/lib/gitpod/v1/prebuild_pb";
10
import { useCurrentOrg } from "../organizations/orgs-query";
11
import { PartialMessage, PlainMessage } from "@bufbuild/protobuf";
12
import type { DeepPartial } from "@gitpod/gitpod-protocol/lib/util/deep-partial";
13
import { Sort } from "@gitpod/public-api/lib/gitpod/v1/sorting_pb";
14
15
type Args = {
16
filter: DeepPartial<PlainMessage<ListOrganizationPrebuildsRequest_Filter>>;
17
sort: PartialMessage<Sort>;
18
organizationId?: string;
19
pageSize: number;
20
};
21
export const useListOrganizationPrebuildsQuery = ({ filter, pageSize, sort, organizationId }: Args) => {
22
const { data: org } = useCurrentOrg();
23
24
return useInfiniteQuery(
25
getListConfigurationsPrebuildsQueryKey(org?.id ?? "", { filter, pageSize, sort }),
26
async ({ pageParam: nextToken }) => {
27
const actualOrganizationId = organizationId ?? org?.id;
28
if (!actualOrganizationId) {
29
throw new Error("No org currently selected");
30
}
31
32
const { prebuilds, pagination } = await prebuildClient.listOrganizationPrebuilds({
33
organizationId: actualOrganizationId,
34
filter,
35
sort: [sort],
36
pagination: { pageSize, token: nextToken },
37
});
38
return {
39
prebuilds,
40
pagination,
41
};
42
},
43
{
44
enabled: !!org,
45
keepPreviousData: true,
46
getNextPageParam: (lastPage) => {
47
// Must ensure we return undefined if there are no more pages
48
return lastPage.pagination?.nextToken || undefined;
49
},
50
},
51
);
52
};
53
54
export const getListConfigurationsPrebuildsQueryKey = (orgId: string, opts: Args) => {
55
return ["prebuilds", "list", orgId, opts];
56
};
57
58