Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/data/insights/list-workspace-sessions-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
import { WorkspaceSession } from "@gitpod/public-api/lib/gitpod/v1/workspace_pb";
7
import { useInfiniteQuery } from "@tanstack/react-query";
8
import { workspaceClient } from "../../service/public-api";
9
import { useCurrentOrg } from "../organizations/orgs-query";
10
import { Timestamp } from "@bufbuild/protobuf";
11
12
const pageSize = 100;
13
14
type Params = {
15
from?: Timestamp;
16
to?: Timestamp;
17
};
18
export const useWorkspaceSessions = ({ from, to }: Params = {}) => {
19
const { data: org } = useCurrentOrg();
20
21
const query = useInfiniteQuery<WorkspaceSession[]>({
22
queryKey: getAuthProviderDescriptionsQueryKey(org?.id, from, to),
23
queryFn: async ({ pageParam }) => {
24
if (!org) {
25
throw new Error("No org specified");
26
}
27
28
const response = await workspaceClient.listWorkspaceSessions({
29
organizationId: org.id,
30
from,
31
to,
32
pagination: {
33
page: pageParam ?? 0,
34
pageSize,
35
},
36
});
37
38
return response.workspaceSessions;
39
},
40
getNextPageParam: (lastPage, pages) => {
41
const hasMore = lastPage.length === pageSize;
42
return hasMore ? pages.length : undefined;
43
},
44
enabled: !!org,
45
});
46
47
return query;
48
};
49
50
export const getAuthProviderDescriptionsQueryKey = (orgId?: string, from?: Timestamp, to?: Timestamp) => [
51
"workspace-sessions",
52
{ orgId, from, to },
53
];
54
55