Files
speckle-server/packages/frontend-2/lib/auth/composables/activeUser.ts
T
Kristaps Fabians Geikins 596312ab0e feat(frontend): personal project limit disclaimers & prompts (#4822)
* ProjectsAdd wrapper

* WorkspaceMoveProject wrapper added

* move wrapper finalized

* passing through location

* more cleanup

* model add wrapper

* permissions cleanup

* add invite wrapper

* vue-tippy bugfix

* ViewerLimitsDialog prep

* upgrade limit alert prep

* limit alerts

* movemanager fix

* new add flow

* slug update fix

* add model flow

* invites?

* some extra fixes

* move unmount fix?

* more fixes

* vue-tsc update

* style: remove h-32 for smaller screens

* vue-tsc parser fix

* prep for new viewer limits dialog

* updated viewer dialogs

* comment variant cleanup

* CR comments

---------

Co-authored-by: michalspeckle <michal@speckle.systems>
2025-05-28 12:12:18 +03:00

129 lines
3.0 KiB
TypeScript

import {
Roles,
type MaybeNullOrUndefined,
resolveMixpanelUserId
} from '@speckle/shared'
import { useApolloClient, useQuery } from '@vue/apollo-composable'
import { graphql } from '~~/lib/common/generated/gql'
export const activeUserQuery = graphql(`
query ActiveUserMainMetadata {
activeUser {
id
email
emails {
id
email
verified
}
company
bio
name
role
avatar
isOnboardingFinished
createdAt
verified
notificationPreferences
versions(limit: 0) {
totalCount
}
...ProjectsAdd_User
}
}
`)
export const activeUserProjectsToMoveQuery = graphql(`
query ActiveUserProjectsToMove($filter: UserProjectsFilter) {
activeUser {
id
projects(filter: $filter) {
totalCount
}
}
}
`)
/**
* Lightweight composable to read user id from cache imperatively (useful for logging)
*/
export function useReadUserId() {
const client = useApolloClient().client
return () => {
return client.readQuery({ query: activeUserQuery })?.activeUser?.id
}
}
export function useResolveUserDistinctId() {
return (user: MaybeNullOrUndefined<{ email?: MaybeNullOrUndefined<string> }>) => {
if (!user) return user // null or undefined
if (!user.email) return null
return resolveMixpanelUserId(user.email)
}
}
export function useActiveUserProjectsToMove() {
const { result } = useQuery(activeUserProjectsToMoveQuery, () => ({
filter: {
personalOnly: true,
onlyWithRoles: [Roles.Stream.Owner]
}
}))
const projectsToMoveCount = computed(
() => result.value?.activeUser?.projects?.totalCount
)
const hasProjectsToMove = computed(
() => projectsToMoveCount.value && projectsToMoveCount.value > 0
)
return { projectsToMoveCount, hasProjectsToMove }
}
/**
* Get active user.
* undefined - not yet resolved
* null - resolved that user is a guest
*/
export function useActiveUser() {
const { result, refetch, onResult } = useQuery(activeUserQuery)
const getDistinctId = useResolveUserDistinctId()
const activeUser = computed(() =>
result.value ? result.value.activeUser : undefined
)
const isLoggedIn = computed(() => !!activeUser.value?.id)
const distinctId = computed(() => {
const user = activeUser.value
return getDistinctId(user)
})
const userId = computed(() => activeUser.value?.id)
const isGuest = computed(() => activeUser.value?.role === Roles.Server.Guest)
const isAdmin = computed(() => activeUser.value?.role === Roles.Server.Admin)
const projectVersionCount = computed(() => activeUser.value?.versions.totalCount)
return {
activeUser,
userId,
isLoggedIn,
distinctId,
refetch,
onResult,
isGuest,
isAdmin,
projectVersionCount
}
}
/**
* Prevents setup function from resolving until active user is resolved
*/
export function useWaitForActiveUser() {
const client = useApolloClient().client
return async () => await client.query({ query: activeUserQuery }).catch(() => void 0)
}