-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dashboard] use organization v2 shapes
- Loading branch information
1 parent
0e8f0de
commit b86086e
Showing
27 changed files
with
683 additions
and
500 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
components/dashboard/src/data/organizations/invite-query.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License.AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; | ||
import { useCallback } from "react"; | ||
import { organizationClient } from "../../service/public-api"; | ||
|
||
export function useInviteInvalidator(organizationId: string | undefined) { | ||
const queryClient = useQueryClient(); | ||
return useCallback(() => { | ||
queryClient.invalidateQueries(getQueryKey(organizationId)); | ||
}, [organizationId, queryClient]); | ||
} | ||
|
||
export function useInvitationId(organizationId: string | undefined) { | ||
const query = useQuery<string, Error>( | ||
getQueryKey(organizationId), | ||
async () => { | ||
const response = await organizationClient.getOrganizationInvitation({ | ||
organizationId, | ||
}); | ||
return response.invitationId; | ||
}, | ||
{ | ||
enabled: !!organizationId, | ||
}, | ||
); | ||
return query; | ||
} | ||
|
||
export function useResetInvitationId(organizationId: string | undefined) { | ||
const invalidate = useInviteInvalidator(organizationId); | ||
return useMutation<void, Error, string>({ | ||
mutationFn: async (orgId) => { | ||
if (!orgId) { | ||
throw new Error("No current organization selected"); | ||
} | ||
|
||
await organizationClient.resetOrganizationInvitation({ | ||
organizationId: orgId, | ||
}); | ||
}, | ||
onSuccess(updatedOrg) { | ||
invalidate(); | ||
}, | ||
}); | ||
} | ||
|
||
function getQueryKey(organizationId: string | undefined) { | ||
return ["invitationId", organizationId || "undefined"]; | ||
} |
50 changes: 50 additions & 0 deletions
50
components/dashboard/src/data/organizations/members-query.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License.AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { OrganizationMember, OrganizationRole } from "@gitpod/public-api/lib/gitpod/experimental/v2/organization_pb"; | ||
import { useQuery, useQueryClient } from "@tanstack/react-query"; | ||
import { useCallback, useMemo } from "react"; | ||
import { organizationClient } from "../../service/public-api"; | ||
import { useCurrentUser } from "../../user-context"; | ||
|
||
export function useOrganizationMembersInvalidator(organizationId: string | undefined) { | ||
const queryClient = useQueryClient(); | ||
return useCallback(() => { | ||
queryClient.invalidateQueries(getQueryKey(organizationId)); | ||
}, [organizationId, queryClient]); | ||
} | ||
|
||
export function useListOrganizationMembers(organizationId: string | undefined) { | ||
const query = useQuery<OrganizationMember[], Error>( | ||
getQueryKey(organizationId), | ||
async () => { | ||
const response = await organizationClient.listOrganizationMembers({ | ||
organizationId, | ||
pagination: { | ||
pageSize: 1000, | ||
}, | ||
}); | ||
return response.members; | ||
}, | ||
{ | ||
enabled: !!organizationId, | ||
}, | ||
); | ||
return query; | ||
} | ||
|
||
export function useIsOwner(organizationId: string | undefined): boolean { | ||
const user = useCurrentUser(); | ||
const members = useListOrganizationMembers(organizationId); | ||
const isOwner = useMemo(() => { | ||
return members?.data?.some((m) => m.userId === user?.id && m.role === OrganizationRole.OWNER); | ||
}, [members?.data, user?.id]); | ||
return !!isOwner; | ||
} | ||
|
||
function getQueryKey(organizationId: string | undefined) { | ||
return ["listOrganizationMembers", organizationId || "undefined"]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License.AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { Organization } from "@gitpod/public-api/lib/gitpod/experimental/v2/organization_pb"; | ||
import { Timestamp } from "@bufbuild/protobuf"; | ||
import { hydrate, dehydrate } from "./setup"; | ||
|
||
test("set and get proto message", async () => { | ||
const now = new Date(); | ||
const org = new Organization({ | ||
creationTime: Timestamp.fromDate(now), | ||
id: "test-id", | ||
name: "test-name", | ||
slug: "test-slug", | ||
}); | ||
|
||
expect(rehydrate(org).creationTime!.toDate()).toStrictEqual(now); | ||
}); | ||
|
||
function rehydrate<T>(obj: T): T { | ||
const dehydrated = dehydrate(obj); | ||
const str = JSON.stringify(dehydrated); | ||
const fromStorage = JSON.parse(str); | ||
const hydrated = hydrate(fromStorage); | ||
return hydrated; | ||
} |
Oops, something went wrong.