Path: blob/main/components/dashboard/src/data/insights/list-workspace-sessions-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*/5import { WorkspaceSession } from "@gitpod/public-api/lib/gitpod/v1/workspace_pb";6import { useInfiniteQuery } from "@tanstack/react-query";7import { workspaceClient } from "../../service/public-api";8import { useCurrentOrg } from "../organizations/orgs-query";9import { Timestamp } from "@bufbuild/protobuf";1011const pageSize = 100;1213type Params = {14from?: Timestamp;15to?: Timestamp;16};17export const useWorkspaceSessions = ({ from, to }: Params = {}) => {18const { data: org } = useCurrentOrg();1920const query = useInfiniteQuery<WorkspaceSession[]>({21queryKey: getAuthProviderDescriptionsQueryKey(org?.id, from, to),22queryFn: async ({ pageParam }) => {23if (!org) {24throw new Error("No org specified");25}2627const response = await workspaceClient.listWorkspaceSessions({28organizationId: org.id,29from,30to,31pagination: {32page: pageParam ?? 0,33pageSize,34},35});3637return response.workspaceSessions;38},39getNextPageParam: (lastPage, pages) => {40const hasMore = lastPage.length === pageSize;41return hasMore ? pages.length : undefined;42},43enabled: !!org,44});4546return query;47};4849export const getAuthProviderDescriptionsQueryKey = (orgId?: string, from?: Timestamp, to?: Timestamp) => [50"workspace-sessions",51{ orgId, from, to },52];535455