Files
speckle-server/packages/frontend-2/lib/settings/composables/management.ts
T
2025-06-04 10:59:56 +02:00

98 lines
2.9 KiB
TypeScript

import {
settingsUpdateWorkspaceMutation,
settingsAddWorkspaceDomainMutation
} from '~/lib/settings/graphql/mutations'
import { useMutation, useApolloClient } from '@vue/apollo-composable'
import {
convertThrowIntoFetchResult,
getFirstErrorMessage,
getCacheId
} from '~~/lib/common/helpers/graphql'
import { ToastNotificationType, useGlobalToast } from '~~/lib/common/composables/toast'
import type {
WorkspaceUpdateInput,
AddDomainToWorkspaceInput
} from '~~/lib/common/generated/gql/graphql'
import type { Workspace } from '~/lib/common/generated/gql/graphql'
export function useUpdateWorkspace() {
const { mutate, loading } = useMutation(settingsUpdateWorkspaceMutation)
const { triggerNotification } = useGlobalToast()
return {
mutate: async (input: WorkspaceUpdateInput) => {
const result = await mutate({ input }).catch(convertThrowIntoFetchResult)
if (result?.data) {
triggerNotification({
type: ToastNotificationType.Success,
title: 'Workspace updated'
})
} else {
const errorMessage = getFirstErrorMessage(result?.errors)
triggerNotification({
type: ToastNotificationType.Danger,
title: 'Workspace update failed',
description: errorMessage
})
}
return result
},
loading
}
}
export function useAddWorkspaceDomain() {
const apollo = useApolloClient().client
const { triggerNotification } = useGlobalToast()
return {
mutate: async (input: AddDomainToWorkspaceInput) => {
const result = await apollo
.mutate({
mutation: settingsAddWorkspaceDomainMutation,
variables: {
input: {
domain: input.domain,
workspaceId: input.workspaceId
}
},
update: (cache, res) => {
const { data } = res
if (!data?.workspaceMutations) return
cache.modify<Workspace>({
id: getCacheId('Workspace', input.workspaceId),
fields: {
discoverabilityEnabled() {
return (
data?.workspaceMutations.addDomain.discoverabilityEnabled || false
)
},
domains() {
return [...(data?.workspaceMutations.addDomain.domains || [])]
}
}
})
}
})
.catch(convertThrowIntoFetchResult)
if (result?.data) {
triggerNotification({
type: ToastNotificationType.Success,
title: 'Domain added',
description: `The verified domain ${input.domain} has been added to your workspace`
})
} else {
triggerNotification({
type: ToastNotificationType.Danger,
title: 'Failed to add verified domain',
description: getFirstErrorMessage(result?.errors)
})
}
}
}
}