Files
1127c4ba88 Project Level Permissions (#27347)
* fix: update Permission params

* fix: upgrade check permission hook to support project level role

* fix: usePermissionsLoaded

* fix: Permission params can be undefined

* Scaffold new access management UI

* Add validation

* Update roles view

* Add tooltip

* Add button to apply role to all projects

* Update UI to select projects first instead of roles

* Merge master update UI

* Midway trying to implementation project level perms API

* First pass implementating updating project level permissions

* Add client side validation for assigning/removing roles

* Midway implementing new invites

* Integrate most of the project level permissions functionality

* fix: filter out org-level permissions before checking

* Add relevant UI guards in org level pages for project role POV

* Minor refactors

* Small refactors

* More fixes

* Moar refactors

* More fixes

* More fixes

* Refactor update role logic and smack some test cases on it

* Fixes

* Fix type issue

* Fix type

* more fixes, refactors, adding checks...

* MORE fixes

* Add perms checking for replicas

* Add ButtonTooltip component and use them to prevent repetition of pointer events auto for buttons with tooltips

* Convert all buttons with tooltips to use ButtonTooltip

* refactor

* PRettier

* Small fix

* Remove commented out code in organization-invitation-accept-mutation

* fix: switch to use the platform oauth authorizations routes

* Add perms checking for org audit logs and org oauth apps

* PRettier

* Fix incorrect URL for oauth app flow

* Fix incorrect URL for oauth app flow

* Fix

* Add perms checking for warehouse related UI

* Update roles helper icon

* remove unused lib

* Update package lock... again

* Update package lock... again

* Smalllll update

* Update some checks

* Add gate for project level permissions

* Last fix

* update codegen

* Update warehouse endpoint routes

* Fix

---------

Co-authored-by: phamhieu <phamhieu1998@gmail.com>
Co-authored-by: Alaister Young <a@alaisteryoung.com>
2024-07-01 17:59:54 +08:00

123 lines
4.4 KiB
TypeScript

import { CheckSquare } from 'lucide-react'
import Link from 'next/link'
import { useRouter } from 'next/router'
import toast from 'react-hot-toast'
import { useParams } from 'common'
import { useOrganizationAcceptInvitationMutation } from 'data/organization-members/organization-invitation-accept-mutation'
import { useOrganizationDeleteInvitationMutation } from 'data/organization-members/organization-invitation-delete-mutation'
import { useOrganizationInvitationTokenQuery } from 'data/organization-members/organization-invitation-token-query'
import { useProfile } from 'lib/profile'
import { ResponseError } from 'types'
import { Button, Loading, cn } from 'ui'
import { OrganizationInviteError } from './OrganizationInviteError'
export const OrganizationInvite = () => {
const router = useRouter()
const { profile } = useProfile()
const { slug, token, name } = useParams()
const { data, error, isSuccess, isError, isLoading } = useOrganizationInvitationTokenQuery(
{ slug, token },
{
retry: false,
refetchOnWindowFocus: false,
}
)
const hasError =
isError || (isSuccess && (data.token_does_not_exist || data.expired_token || !data.email_match))
const organizationName = data?.organization_name ?? name ?? 'an organization'
const loginRedirectLink = `/?returnTo=${encodeURIComponent(`/join?token=${token}&slug=${slug}`)}`
const { mutate: joinOrganization, isLoading: isJoining } =
useOrganizationAcceptInvitationMutation({
onSuccess: () => {
router.push('/')
},
onError: (error) => {
toast.error(`Failed to join organization: ${error.message}`)
},
})
const { mutate: declineOrganization, isLoading: isDeclining } =
useOrganizationDeleteInvitationMutation({
onSuccess: () => {
router.push('/')
},
onError: (error) => {
toast.error(`Failed to decline invitation: ${error.message}`)
},
})
async function handleJoinOrganization() {
if (!slug) return console.error('Slug is required')
if (!token) return console.error('Token is required')
joinOrganization({ slug, token })
}
async function handleDeclineJoinOrganization() {
if (!slug) return console.error('Slug is required')
if (!data?.invite_id) return console.error('Invite ID is required')
declineOrganization({ slug, id: data.invite_id })
}
return (
<Loading active={profile !== undefined && isLoading}>
<div className="flex flex-col gap-2 px-6 py-8">
<p className="text-sm text-foreground">You have been invited to join </p>
<p className="text-3xl text-foreground">{organizationName}</p>
{slug && <p className="text-xs text-foreground-lighter">{`organization slug: ${slug}`}</p>}
</div>
<div className={cn('border-t border-muted', hasError ? 'bg-alternative' : 'bg-transparent')}>
<div className={cn('flex flex-col gap-4', !isLoading && !hasError && 'px-6 py-4')}>
{profile === undefined && (
<div className="flex flex-col gap-3">
<p className="text-xs text-foreground-lighter">
You will need to sign in to accept this invitation
</p>
<div className="flex justify-center gap-3">
<Button asChild type="default">
<Link href={loginRedirectLink}>Sign in</Link>
</Button>
<Button asChild type="default">
<Link href={loginRedirectLink}>Create an account</Link>
</Button>
</div>
</div>
)}
{hasError && (
<OrganizationInviteError
data={data}
error={error as unknown as ResponseError}
isError={isError}
/>
)}
{isSuccess && (
<div className="flex flex-row items-center justify-center gap-3">
<Button
type="default"
loading={isDeclining}
disabled={isJoining || isDeclining}
onClick={handleDeclineJoinOrganization}
>
Decline
</Button>
<Button
type="primary"
loading={isJoining}
disabled={isJoining || isDeclining}
onClick={handleJoinOrganization}
icon={<CheckSquare />}
>
Join organization
</Button>
</div>
)}
</div>
</div>
</Loading>
)
}