069f64afc9
* feat: user guest role switching in FE1 * removed stream create buttons * fe1 done * fe1 - specifying role in invite dialogs * fe1 - bulk invites * WIP FE2 changes * fe1: allow role select condition fixes * xtra limitations on createForOnboarding * more invite creation validations * no longer able to set guest as project owner in invite * preparations for server role select in invite dialog * team management dialog done * server invite dialog updated * hiding invite dialog * fixed mocks
54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import { Roles } from '@speckle/shared'
|
|
import { useApolloClient, useQuery } from '@vue/apollo-composable'
|
|
import { graphql } from '~~/lib/common/generated/gql'
|
|
import md5 from '~~/lib/common/helpers/md5'
|
|
|
|
export const activeUserQuery = graphql(`
|
|
query ActiveUserMainMetadata {
|
|
activeUser {
|
|
id
|
|
email
|
|
name
|
|
role
|
|
avatar
|
|
isOnboardingFinished
|
|
createdAt
|
|
verified
|
|
}
|
|
}
|
|
`)
|
|
|
|
/**
|
|
* Get active user.
|
|
* undefined - not yet resolved
|
|
* null - resolved that user is a guest
|
|
*/
|
|
export function useActiveUser() {
|
|
const { result, refetch, onResult } = useQuery(activeUserQuery)
|
|
|
|
const activeUser = computed(() =>
|
|
result.value ? result.value.activeUser : undefined
|
|
)
|
|
const isLoggedIn = computed(() => !!activeUser.value?.id)
|
|
const distinctId = computed(() => {
|
|
const user = activeUser.value
|
|
if (!user) return user // null or undefined
|
|
if (!user.email) return null
|
|
|
|
return '@' + md5(user.email.toLowerCase()).toUpperCase()
|
|
})
|
|
|
|
const isGuest = computed(() => activeUser.value?.role === Roles.Server.Guest)
|
|
const isAdmin = computed(() => activeUser.value?.role === Roles.Server.Admin)
|
|
|
|
return { activeUser, isLoggedIn, distinctId, refetch, onResult, isGuest, isAdmin }
|
|
}
|
|
|
|
/**
|
|
* Prevnets setup function from resolving until active user is resolved
|
|
*/
|
|
export async function useWaitForActiveUser() {
|
|
const client = useApolloClient().client
|
|
await client.query({ query: activeUserQuery }).catch(() => void 0)
|
|
}
|