mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 08:18:42 +00:00
408 lines
13 KiB
Vue
408 lines
13 KiB
Vue
<template>
|
|
<div class="template-install">
|
|
<template v-if="installable">
|
|
<!-- Without a workspace there is nothing to pick. The user creates one
|
|
here, or else is told how to get one. -->
|
|
<p v-if="hasNoWorkspace" class="template-install__no-workspace">
|
|
{{
|
|
$hasPermission('create_workspace')
|
|
? $t('templateInstall.noWorkspace')
|
|
: $t('templateInstall.noWorkspaceAskInvite')
|
|
}}
|
|
</p>
|
|
<Dropdown
|
|
v-else-if="isAuthenticated"
|
|
:model-value="selectedWorkspaceId"
|
|
class="template-install__workspace"
|
|
:placeholder="$t('templateInstall.selectWorkspace')"
|
|
:fixed-items="true"
|
|
@update:model-value="chooseWorkspace"
|
|
>
|
|
<DropdownItem
|
|
v-for="workspaceItem in workspaces"
|
|
:key="workspaceItem.id"
|
|
:name="workspaceItem.name"
|
|
:value="workspaceItem.id"
|
|
:disabled="!canInstallInto(workspaceItem)"
|
|
:description="
|
|
canInstallInto(workspaceItem)
|
|
? null
|
|
: $t('templateInstall.workspaceNoPermission')
|
|
"
|
|
></DropdownItem>
|
|
</Dropdown>
|
|
<p v-if="createsIdentities" class="template-install__summary">
|
|
<i class="template-install__summary-icon iconoir-user"></i>
|
|
{{ $t('templateInstall.createsIdentities') }}
|
|
</p>
|
|
</template>
|
|
<div class="template-install__actions">
|
|
<template v-if="installable">
|
|
<Button
|
|
v-if="hasNoWorkspace && $hasPermission('create_workspace')"
|
|
class="template-install__create-workspace"
|
|
full-width
|
|
@click="createWorkspaceModal.show()"
|
|
>{{ $t('templateInstall.createWorkspace') }}</Button
|
|
>
|
|
<Button
|
|
v-else
|
|
class="template-install__use"
|
|
full-width
|
|
:loading="installing"
|
|
:disabled="
|
|
installing ||
|
|
(isAuthenticated &&
|
|
(selectedWorkspace === null ||
|
|
!canInstallInto(selectedWorkspace)))
|
|
"
|
|
@click="use"
|
|
>{{ $t('templateInstall.use') }}</Button
|
|
>
|
|
</template>
|
|
<ButtonIcon
|
|
v-if="hasOtherMenuItems || installableUserTemplate"
|
|
ref="moreButton"
|
|
type="secondary"
|
|
icon="iconoir-more-vert"
|
|
:title="$t('templateInstall.more')"
|
|
@click="contextRef.toggle(moreButton.$el, 'top', 'right', 4)"
|
|
></ButtonIcon>
|
|
</div>
|
|
<Context ref="contextRef">
|
|
<ul class="context__menu">
|
|
<slot name="menu-items" :hide="hideContext"></slot>
|
|
<li
|
|
v-if="copyLinkItem"
|
|
class="context__menu-item"
|
|
:class="{ 'context__menu-item--with-separator': hasMenuItems }"
|
|
>
|
|
<a class="context__menu-item-link" @click.prevent="copyTemplateLink">
|
|
<i class="context__menu-item-icon iconoir-link"></i>
|
|
{{ $t(copyLinkItem.label) }}
|
|
<Copied ref="copiedRef"></Copied>
|
|
</a>
|
|
</li>
|
|
<li
|
|
v-if="installableUserTemplate"
|
|
class="context__menu-item"
|
|
:class="{
|
|
'context__menu-item--with-separator': hasMenuItems && !copyLinkItem,
|
|
}"
|
|
>
|
|
<a
|
|
class="context__menu-item-link"
|
|
:class="{
|
|
'context__menu-item-link--loading': isDownloading(template),
|
|
}"
|
|
@click.prevent="downloadTemplate"
|
|
>
|
|
<i class="context__menu-item-icon iconoir-download"></i>
|
|
{{ $t('templateInstall.download') }}
|
|
</a>
|
|
</li>
|
|
<slot name="menu-items-end" :hide="hideContext"></slot>
|
|
</ul>
|
|
</Context>
|
|
<CreateWorkspaceModal
|
|
v-if="isAuthenticated"
|
|
ref="createWorkspaceModal"
|
|
:navigate="false"
|
|
@created="workspaceCreated"
|
|
></CreateWorkspaceModal>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { computed, onMounted, ref, useSlots, watch } from 'vue'
|
|
import { useStore } from 'vuex'
|
|
import { useNuxtApp, useRouter, useRuntimeConfig } from '#app'
|
|
|
|
import { notifyIf } from '@baserow/modules/core/utils/error'
|
|
import { captureTemplateEvent } from '@baserow/modules/core/utils/templateAnalytics'
|
|
import { signedInUserCheck } from '@baserow/modules/core/utils/auth'
|
|
import { useUserTemplateDownload } from '@baserow/modules/core/composables/useUserTemplateDownload'
|
|
import {
|
|
canInstall,
|
|
getCopyLinkItem,
|
|
} from '@baserow/modules/core/utils/userTemplate'
|
|
import { fetchPermissionsOfWorkspaces } from '@baserow/modules/core/utils/workspace'
|
|
import TemplateService from '@baserow/modules/core/services/template'
|
|
import { useCopyLink } from '@baserow/modules/core/composables/useCopyLink'
|
|
import { isPrivateLinkPage } from '@baserow/modules/core/utils/templatePrivateLink'
|
|
import CreateWorkspaceModal from '@baserow/modules/core/components/workspace/CreateWorkspaceModal'
|
|
|
|
const props = defineProps({
|
|
template: {
|
|
type: Object,
|
|
required: true,
|
|
},
|
|
// The workspace the template is installed into by default. When null, the
|
|
// selected or else the first workspace is used.
|
|
workspace: {
|
|
type: Object,
|
|
required: false,
|
|
default: null,
|
|
},
|
|
copyLinkKind: {
|
|
type: String,
|
|
required: false,
|
|
default: 'sharing',
|
|
},
|
|
// Whether the latest revision of a user template is installed, which only its
|
|
// owner may. Otherwise it's the public revision, or the owner's latest one
|
|
// while it isn't public.
|
|
latest: {
|
|
type: Boolean,
|
|
required: false,
|
|
default: false,
|
|
},
|
|
// The applications of the template, which tell whether the install creates
|
|
// an identity for its Agents.
|
|
applications: {
|
|
type: Array,
|
|
required: false,
|
|
default: () => [],
|
|
},
|
|
source: {
|
|
type: String,
|
|
required: false,
|
|
default: null,
|
|
},
|
|
})
|
|
|
|
// `outdated` tells that the template was updated since it was shown, and must
|
|
// be loaded again.
|
|
const emit = defineEmits(['installed', 'outdated'])
|
|
|
|
const slots = useSlots()
|
|
const store = useStore()
|
|
const router = useRouter()
|
|
const config = useRuntimeConfig()
|
|
const { $client, $hasPermission, $i18n, $posthog, $registry } = useNuxtApp()
|
|
const { copyLink } = useCopyLink()
|
|
|
|
const moreButton = ref(null)
|
|
const contextRef = ref(null)
|
|
const copiedRef = ref(null)
|
|
const createWorkspaceModal = ref(null)
|
|
const installing = ref(false)
|
|
const { isDownloading, download } = useUserTemplateDownload()
|
|
|
|
const installable = computed(() =>
|
|
canInstall(props.template, store.getters['auth/isStaff'])
|
|
)
|
|
|
|
// A separator goes after the slot items, when there are any.
|
|
const hasMenuItems = computed(() => !!slots['menu-items'])
|
|
|
|
const installableUserTemplate = computed(
|
|
() => installable.value && props.template.type === 'user'
|
|
)
|
|
const copyLinkItem = computed(() =>
|
|
getCopyLinkItem(
|
|
props.copyLinkKind,
|
|
props.template,
|
|
router,
|
|
config.public.publicWebFrontendUrl
|
|
)
|
|
)
|
|
const hasOtherMenuItems = computed(
|
|
() => hasMenuItems.value || !!slots['menu-items-end'] || !!copyLinkItem.value
|
|
)
|
|
|
|
const isAuthenticated = computed(() => store.getters['auth/isAuthenticated'])
|
|
|
|
// The workspaces the user can't add applications to are listed, but disabled.
|
|
const workspaces = computed(() => store.getters['workspace/getAllSorted'])
|
|
|
|
const hasNoWorkspace = computed(
|
|
() => isAuthenticated.value && workspaces.value.length === 0
|
|
)
|
|
|
|
// Whether the user picked the workspace, which must then stay selected.
|
|
const workspaceChosen = ref(false)
|
|
|
|
/**
|
|
* Whether the template can be installed into the workspace. Until the
|
|
* permissions of the workspace are fetched, the global ones are used, and the
|
|
* backend checks again.
|
|
*/
|
|
function canInstallInto(workspace) {
|
|
return $hasPermission('workspace.create_application', workspace, workspace.id)
|
|
}
|
|
|
|
/**
|
|
* The provided, else the selected workspace if the template can be installed
|
|
* into it, otherwise the first one it can be installed into.
|
|
*/
|
|
function getDefaultWorkspaceId() {
|
|
// Without a selected workspace, `getSelected` is an empty object.
|
|
const preferred = [
|
|
props.workspace,
|
|
store.getters['workspace/getSelected'],
|
|
].filter((workspace) => workspace?.id !== undefined)
|
|
const candidates = [...preferred, ...workspaces.value]
|
|
const workspace = candidates.find(canInstallInto) || candidates[0]
|
|
return workspace?.id ?? null
|
|
}
|
|
|
|
const selectedWorkspaceId = ref(getDefaultWorkspaceId())
|
|
|
|
const selectedWorkspace = computed(
|
|
() =>
|
|
workspaces.value.find(({ id }) => id === selectedWorkspaceId.value) || null
|
|
)
|
|
|
|
/**
|
|
* Whether the install creates the identity the template's Agents work as, with
|
|
* access to only the template's other applications. That takes roles, and the
|
|
* permission to create such identities, which the backend checks.
|
|
*/
|
|
const createsIdentities = computed(() => {
|
|
const workspace = selectedWorkspace.value
|
|
if (!isAuthenticated.value || workspace === null) {
|
|
return false
|
|
}
|
|
return (
|
|
props.applications.some(
|
|
(application) =>
|
|
$registry.exists('application', application.type) &&
|
|
$registry
|
|
.get('application', application.type)
|
|
.createsIdentityOnInstall(application)
|
|
) &&
|
|
Object.values($registry.getAll('plugin')).some((plugin) =>
|
|
plugin.canCreateScopedIdentities(workspace)
|
|
)
|
|
)
|
|
})
|
|
|
|
function chooseWorkspace(workspaceId) {
|
|
workspaceChosen.value = true
|
|
selectedWorkspaceId.value = workspaceId
|
|
}
|
|
|
|
/**
|
|
* The permissions of a workspace are only fetched when needed, so the ones of the
|
|
* listed workspaces are fetched here. A workspace the template can't be installed
|
|
* into is then replaced with one it can, unless the user picked it.
|
|
*/
|
|
async function loadPermissions() {
|
|
if (!isAuthenticated.value || !installable.value) {
|
|
return
|
|
}
|
|
await fetchPermissionsOfWorkspaces(store, workspaces.value)
|
|
if (
|
|
!workspaceChosen.value &&
|
|
(selectedWorkspace.value === null ||
|
|
!canInstallInto(selectedWorkspace.value))
|
|
) {
|
|
selectedWorkspaceId.value = getDefaultWorkspaceId()
|
|
}
|
|
}
|
|
|
|
onMounted(loadPermissions)
|
|
watch(installable, (now) => now && loadPermissions())
|
|
|
|
async function goToLogin() {
|
|
const current = router.currentRoute.value
|
|
const { fullPath: original } = isPrivateLinkPage(current)
|
|
? current
|
|
: templateRoute()
|
|
await router.push({ name: 'login', query: { original } })
|
|
}
|
|
|
|
async function use() {
|
|
const template = props.template
|
|
const source = props.source
|
|
const capture = (name, properties) =>
|
|
source &&
|
|
captureTemplateEvent($posthog, name, template, { ...properties, source })
|
|
capture('install_clicked', { signed_in: isAuthenticated.value })
|
|
if (!isAuthenticated.value) {
|
|
await goToLogin()
|
|
return
|
|
}
|
|
|
|
installing.value = true
|
|
const workspaceId = selectedWorkspaceId.value
|
|
const isSameUser = signedInUserCheck(store)
|
|
|
|
try {
|
|
const { data } = await TemplateService($client).asyncInstall(
|
|
workspaceId,
|
|
props.template.id,
|
|
{ latest: props.latest, revisionId: props.template.revision_id ?? null }
|
|
)
|
|
// The user logged out while installing.
|
|
if (!isSameUser()) {
|
|
return
|
|
}
|
|
// The user stays where they are. The toast shows the progress, unless the
|
|
// sidebar shows it, and then the result. Added before the job, whose end
|
|
// updates the toast.
|
|
await store.dispatch('toast/add', {
|
|
type: 'installTemplate',
|
|
data: { jobId: data.id, template_name: props.template.name, workspaceId },
|
|
})
|
|
await store.dispatch('job/create', data)
|
|
emit('installed')
|
|
} catch (error) {
|
|
if (!isSameUser()) {
|
|
return
|
|
}
|
|
capture('install_failed', {
|
|
stage: 'request',
|
|
error_code: error.handler?.code ?? 'unknown',
|
|
})
|
|
notifyIf(error, 'template')
|
|
// Another revision was made public, or the owner updated the template in
|
|
// another tab, since it was shown. Nothing was installed.
|
|
if (error.handler.code === 'ERROR_TEMPLATE_UPDATED') {
|
|
emit('outdated')
|
|
}
|
|
} finally {
|
|
installing.value = false
|
|
}
|
|
}
|
|
|
|
// The user stays on the template, which is then installed into the new workspace.
|
|
function workspaceCreated(workspace) {
|
|
chooseWorkspace(workspace.id)
|
|
}
|
|
|
|
function copyTemplateLink() {
|
|
return copyLink(copyLinkItem.value.url, copiedRef.value, props.template, {
|
|
source: props.source,
|
|
})
|
|
}
|
|
|
|
function hideContext() {
|
|
// The component can be gone after a request, for example when the modal closed.
|
|
contextRef.value?.hide()
|
|
}
|
|
|
|
/**
|
|
* The menu stays open, showing the loading state, until the download started.
|
|
*/
|
|
async function downloadTemplate() {
|
|
if (!isAuthenticated.value) {
|
|
await goToLogin()
|
|
return
|
|
}
|
|
const template = props.template
|
|
const onOutdated = () => emit('outdated')
|
|
if (await download(template, { onOutdated, source: props.source })) {
|
|
hideContext()
|
|
}
|
|
}
|
|
|
|
function templateRoute() {
|
|
return router.resolve({
|
|
name: 'template',
|
|
params: { slug: props.template.slug },
|
|
})
|
|
}
|
|
</script>
|