Path: blob/main/components/dashboard/src/data/prebuilds/organization-prebuilds-query.ts
2501 views
/**1* Copyright (c) 2024 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { useInfiniteQuery } from "@tanstack/react-query";7import { prebuildClient } from "../../service/public-api";8import { ListOrganizationPrebuildsRequest_Filter } from "@gitpod/public-api/lib/gitpod/v1/prebuild_pb";9import { useCurrentOrg } from "../organizations/orgs-query";10import { PartialMessage, PlainMessage } from "@bufbuild/protobuf";11import type { DeepPartial } from "@gitpod/gitpod-protocol/lib/util/deep-partial";12import { Sort } from "@gitpod/public-api/lib/gitpod/v1/sorting_pb";1314type Args = {15filter: DeepPartial<PlainMessage<ListOrganizationPrebuildsRequest_Filter>>;16sort: PartialMessage<Sort>;17organizationId?: string;18pageSize: number;19};20export const useListOrganizationPrebuildsQuery = ({ filter, pageSize, sort, organizationId }: Args) => {21const { data: org } = useCurrentOrg();2223return useInfiniteQuery(24getListConfigurationsPrebuildsQueryKey(org?.id ?? "", { filter, pageSize, sort }),25async ({ pageParam: nextToken }) => {26const actualOrganizationId = organizationId ?? org?.id;27if (!actualOrganizationId) {28throw new Error("No org currently selected");29}3031const { prebuilds, pagination } = await prebuildClient.listOrganizationPrebuilds({32organizationId: actualOrganizationId,33filter,34sort: [sort],35pagination: { pageSize, token: nextToken },36});37return {38prebuilds,39pagination,40};41},42{43enabled: !!org,44keepPreviousData: true,45getNextPageParam: (lastPage) => {46// Must ensure we return undefined if there are no more pages47return lastPage.pagination?.nextToken || undefined;48},49},50);51};5253export const getListConfigurationsPrebuildsQueryKey = (orgId: string, opts: Args) => {54return ["prebuilds", "list", orgId, opts];55};565758