Files

674 lines
23 KiB
Vue

<template>
<Modal
ref="modal"
class="templates-modal"
:full-screen="true"
:close-button="false"
keep-content
@show="open = true"
@hidden="hidden"
>
<div v-if="loading" class="loading-absolute-center"></div>
<template v-else>
<!-- Keyed by the template only, so that the chosen workspace is kept when a
new revision is shown. The preview loads its content then. -->
<TemplateDetails
v-if="selectedTemplate !== null"
:key="selectedTemplate.id"
:template="selectedTemplate"
:categories="selectedTemplateCategories"
:workspace="workspace"
:owner-actions="selectedOwnerActions"
:delete-only="!userTemplatesAllowed"
:source="previewSource"
:tab="['browser', 'admin'].includes(previewSource) ? tab : null"
show-back
class="templates__body"
@back="selectTemplate(null)"
@installed="hide()"
@outdated="templateOutdated"
@edit="openUpdateModal(selectedTemplate, $event)"
@delete="openDeleteModal"
@make-public="openMakePublicModal"
@make-private="openMakePrivateModal"
></TemplateDetails>
<TemplateList
v-show="selectedTemplate === null && tab === 'official'"
:categories="categories"
:initial-category-id="initialCategoryId"
:active="open && selectedTemplate === null && tab === 'official'"
class="templates__body"
@selected="browseTemplate"
>
<template v-if="showSegments" #head>
<SegmentControl
:segments="tabs"
:active-index="tabIndex"
type="pills"
@update:active-index="selectTab(tabs[$event].value)"
></SegmentControl>
</template>
</TemplateList>
<!-- Mounted only while its tab is open, so that it's loaded again with the
changes made in "Your templates". -->
<PublicTemplateList
v-if="showTabs && tab === 'public'"
v-show="selectedTemplate === null"
ref="publicTemplateList"
:active="open && selectedTemplate === null"
:initial-search="sharedSearch"
class="templates__body"
@selected="selectPublicTemplate"
>
<template #head>
<SegmentControl
:segments="tabs"
:active-index="tabIndex"
type="pills"
@update:active-index="selectTab(tabs[$event].value)"
></SegmentControl>
</template>
</PublicTemplateList>
<UserTemplateList
v-if="showTabs && userTemplatesOpened"
v-show="selectedTemplate === null && tab === 'own'"
ref="userTemplateList"
:active="
open &&
selectedTemplate === null &&
tab === 'own' &&
!creating &&
!updating &&
!deleting &&
!makingPublic &&
!makingPrivate
"
:delete-only="!userTemplatesAllowed"
class="templates__body"
@selected="browseTemplate($event, true)"
@create="openCreateModal"
@edit="openUpdateModal"
@delete="openDeleteModal"
@make-public="openMakePublicModal"
@make-private="openMakePrivateModal"
>
<template #head>
<SegmentControl
:segments="tabs"
:active-index="tabIndex"
type="pills"
@update:active-index="selectTab(tabs[$event].value)"
></SegmentControl>
</template>
</UserTemplateList>
<div class="modal__actions">
<a class="modal__close" @click="hide()">
<i class="iconoir-cancel"></i>
</a>
</div>
</template>
<!-- Outside of the loaded content, so that a running job stays monitored
when the modal is shown again. -->
<CreateUserTemplateModal
v-if="showTabs"
ref="createUserTemplateModal"
:categories="categories"
@hidden="creating = false"
@created="templateCreated"
></CreateUserTemplateModal>
<UpdateUserTemplateModal
v-if="showTabs && updatedTemplate !== null"
:key="updatedTemplate.id"
ref="updateUserTemplateModal"
:template="updatedTemplate"
:mode="updateMode"
:categories="categories"
@hidden="updating = false"
@updated="templateUpdated"
@visibility-changed="templateChanged"
></UpdateUserTemplateModal>
<!-- Not keyed, so that a running deletion is still handled after the modal
is opened for another template. -->
<DeleteUserTemplateModal
v-if="showTabs && deletedTemplate !== null"
ref="deleteUserTemplateModal"
:template="deletedTemplate"
@hidden="deleting = false"
@deleted="templateDeleted"
></DeleteUserTemplateModal>
<!-- Not keyed, so that a running request is still handled after the modal is
opened for another template. -->
<MakeTemplatePublicModal
v-if="showTabs && madePublicTemplate !== null"
ref="makeTemplatePublicModal"
:template="madePublicTemplate"
@hidden="makingPublic = false"
@made-public="templateChanged"
></MakeTemplatePublicModal>
<!-- Not keyed, so that a running request is still handled after the modal is
opened for another template. -->
<MakeTemplatePrivateModal
v-if="showTabs && madePrivateTemplate !== null"
ref="makeTemplatePrivateModal"
:template="madePrivateTemplate"
@hidden="makingPrivate = false"
@made-private="templateChanged"
></MakeTemplatePrivateModal>
</Modal>
</template>
<script>
import modal from '@baserow/modules/core/mixins/modal'
import TemplateService from '@baserow/modules/core/services/template'
import { notifyIf } from '@baserow/modules/core/utils/error'
import { isOwnTemplate } from '@baserow/modules/core/utils/userTemplate'
import { captureTemplateEvent } from '@baserow/modules/core/utils/templateAnalytics'
import { MOST_POPULAR_CATEGORY_NAME } from '@baserow/modules/core/utils/templateCategory'
import {
TEMPLATE_MODERATION,
useTemplateModeration,
} from '@baserow/modules/core/composables/useTemplateModeration'
import CreateUserTemplateModal from '@baserow/modules/core/components/template/CreateUserTemplateModal'
import DeleteUserTemplateModal from '@baserow/modules/core/components/template/DeleteUserTemplateModal'
import MakeTemplatePrivateModal from '@baserow/modules/core/components/template/MakeTemplatePrivateModal'
import MakeTemplatePublicModal from '@baserow/modules/core/components/template/MakeTemplatePublicModal'
import PublicTemplateList from '@baserow/modules/core/components/template/PublicTemplateList'
import TemplateDetails from '@baserow/modules/core/components/template/TemplateDetails'
import TemplateList from '@baserow/modules/core/components/template/TemplateList'
import UpdateUserTemplateModal from '@baserow/modules/core/components/template/UpdateUserTemplateModal'
import UserTemplateList from '@baserow/modules/core/components/template/UserTemplateList'
export default {
name: 'TemplateModal',
components: {
CreateUserTemplateModal,
DeleteUserTemplateModal,
MakeTemplatePrivateModal,
MakeTemplatePublicModal,
PublicTemplateList,
TemplateDetails,
TemplateList,
UpdateUserTemplateModal,
UserTemplateList,
},
mixins: [modal],
provide() {
return {
[TEMPLATE_MODERATION]: useTemplateModeration((template) =>
this.showAsNow(template)
),
}
},
props: {
// When no workspace is provided, the user must choose the workspace to install
// the template into.
workspace: {
type: Object,
required: false,
default: null,
},
source: {
type: String,
required: false,
default: null,
},
},
data() {
return {
// The content stays mounted while the modal is closed.
open: false,
loading: true,
categories: [],
selectedTemplate: null,
selectedOwnerActions: false,
previewSource: 'browser',
tab: 'official',
sharedSearch: '',
// The list of the user's templates is loaded when its tab is opened first.
userTemplatesOpened: false,
// Whether the user has templates of their own, only checked while user
// templates are turned off.
ownsTemplates: false,
// While a template is created, its modal takes the search shortcut.
creating: false,
// The user's template that is or was last updated. It's kept after the
// update modal is closed, so that a running job stays monitored.
updatedTemplate: null,
updateMode: 'details',
// While a template is updated, its modal takes the search shortcut.
updating: false,
// The user's template that is or was last asked to be deleted.
deletedTemplate: null,
// While a template is asked to be deleted, its modal takes the search
// shortcut.
deleting: false,
// The user's template that is or was last asked to be made public.
madePublicTemplate: null,
// While a template is asked to be made public, its modal takes the search
// shortcut.
makingPublic: false,
// The user's template that is or was last asked to be made private.
madePrivateTemplate: null,
// While a template is asked to be made private, its modal takes the search
// shortcut.
makingPrivate: false,
// The id of the public template that was last opened, until it's loaded.
selectingPublic: null,
}
},
computed: {
showTabs() {
return this.$store.getters['auth/isAuthenticated']
},
userTemplatesAllowed() {
return Boolean(this.$store.getters['settings/get'].allow_user_templates)
},
tabs() {
const tabs = [
{
value: 'official',
label: this.$t('templateModal.officialTemplates'),
icon: 'baserow-icon-templates',
},
]
if (this.userTemplatesAllowed) {
tabs.push({
value: 'public',
label: this.$t('templateModal.publicTemplates'),
icon: 'iconoir-group',
})
}
if (this.userTemplatesAllowed || this.ownsTemplates) {
tabs.push({
value: 'own',
label: this.$t('templateModal.yourTemplates'),
icon: 'iconoir-box',
})
}
return tabs
},
showSegments() {
return this.showTabs && this.tabs.length > 1
},
// A computed, because the list reads it only when it's mounted, as soon as
// the categories are loaded.
initialCategoryId() {
return this.categories.find(
({ name }) => name === MOST_POPULAR_CATEGORY_NAME
)?.id
},
tabIndex() {
return this.tabs.findIndex((tab) => tab.value === this.tab)
},
selectedTemplateCategories() {
// A user template lists its own categories.
if (this.selectedTemplate.type === 'user') {
return this.selectedTemplate.categories
}
return this.categories.filter((category) =>
category.templates.some((t) => t.id === this.selectedTemplate.id)
)
},
},
watch: {
open(open) {
// The search of the page behind the modal closes all modals, so the modal keeps
// the shortcut while it's open. The list and nested modals listen with a higher
// priority.
this.$priorityBus.$off('start-search', this.searchStarted)
if (open) {
this.$priorityBus.$on(
'start-search',
this.$priorityBus.level.MEDIUM,
this.searchStarted
)
}
},
},
beforeUnmount() {
this.$priorityBus.$off('start-search', this.searchStarted)
},
methods: {
isOwnTemplate,
/**
* Opens the modal on the details of the template with the provided id or
* slug, or else on the list (on the default template without user templates).
*/
async show(templateId = null, ...args) {
const { listError, ownsError } = await this.openAndLoad(...args)
this.previewSource = templateId === null ? 'default' : 'link'
const linked = typeof templateId === 'string'
if (ownsError || (listError && !linked)) {
this.failLoading(ownsError ?? listError)
return
}
this.captureOpened()
const template = this.categories
.flatMap((category) => category.templates)
.find((t) => t.id === templateId || t.slug === templateId)
if (template) {
this.selectTemplate(template)
} else if (linked) {
await this.selectLinkedTemplate(templateId)
if (listError) {
if (this.selectedTemplate === null) {
this.hide()
}
this.loading = false
}
}
},
/**
* Shows the template a link points to that isn't an official one, like a public
* user template.
*/
async selectLinkedTemplate(slug) {
try {
const { data } = await TemplateService(this.$client).fetch(slug)
if (this.selectedTemplate === null) {
this.selectTemplate(data, isOwnTemplate(data))
}
} catch (error) {
if (error.response?.status === 404) {
this.$store.dispatch('toast/error', {
title: this.$t('templateModal.unavailableTitle'),
message: this.$t('templateModal.linkUnavailableDescription'),
})
} else {
notifyIf(error, 'templates')
}
}
},
async showShared(search) {
const { listError, ownsError } = await this.openAndLoad()
if (ownsError || listError) {
this.failLoading(ownsError ?? listError)
return
}
if (this.userTemplatesAllowed) {
this.sharedSearch = search
this.tab = 'public'
}
this.captureOpened()
},
async showOwn() {
const { listError, ownsError } = await this.openAndLoad()
if (ownsError || listError) {
this.failLoading(ownsError ?? listError)
return
}
if (this.tabs.some((tab) => tab.value === 'own')) {
this.selectTab('own')
}
this.captureOpened()
},
/**
* Opens the modal and fetches all the templates and their categories, so
* that the user can browse them, and whether the user owns templates.
*
* @returns {Promise<{listError?: Error, ownsError?: Error}>} The error of
* each that couldn't be fetched.
*/
async openAndLoad(...args) {
modal.methods.show.call(this, ...args)
this.loading = true
this.categories = []
this.selectTemplate(null)
this.tab = 'official'
this.sharedSearch = ''
this.userTemplatesOpened = false
this.ownsTemplates = false
const [list, owns] = await Promise.allSettled([
TemplateService(this.$client).fetchAll(),
this.fetchOwnsTemplates(),
])
if (list.status === 'fulfilled' && owns.status === 'fulfilled') {
this.categories = list.value.data
this.ownsTemplates = owns.value
this.loading = false
}
return { listError: list.reason, ownsError: owns.reason }
},
async fetchOwnsTemplates() {
if (!this.showTabs || this.userTemplatesAllowed) {
return false
}
const { data } = await TemplateService(this.$client).fetchOwn({
limit: 1,
})
return data.count > 0
},
captureOpened() {
captureTemplateEvent(this.$posthog, 'browser_opened', null, {
source: this.source,
tab: this.tab,
})
},
failLoading(error) {
notifyIf(error, 'templates')
this.hide()
},
selectTab(tab) {
this.tab = tab
this.sharedSearch = ''
if (tab === 'own') {
this.userTemplatesOpened = true
}
},
browseTemplate(template, own = false) {
this.previewSource = this.source === 'admin' ? 'admin' : 'browser'
this.selectTemplate(template, own)
},
selectTemplate(template, own = false) {
this.selectedTemplate = template
this.selectedOwnerActions = own
},
/**
* Shows the public template as it is now, which may have been made private or
* deleted since the list was loaded. The list is loaded again then.
*/
async selectPublicTemplate(template) {
const { id, slug } = template
this.selectingPublic = id
try {
const { data } = await TemplateService(this.$client).fetch(slug)
if (this.selectingPublic === id && this.selectedTemplate === null) {
this.browseTemplate(data, isOwnTemplate(data))
}
} catch (error) {
if (error.response?.status === 404) {
this.$store.dispatch('toast/error', {
title: this.$t('templateModal.unavailableTitle'),
message: this.$t('templateModal.unavailableDescription'),
})
this.$refs.publicTemplateList?.refresh()
} else {
notifyIf(error, 'templates')
}
} finally {
if (this.selectingPublic === id) {
this.selectingPublic = null
}
}
},
openCreateModal() {
this.creating = true
this.$refs.createUserTemplateModal.show()
},
templateCreated() {
this.$refs.userTemplateList?.refresh()
},
async showAsNow(template) {
this.$refs.userTemplateList?.refresh()
this.$refs.publicTemplateList?.refresh()
if (this.selectedTemplate?.id === template.id) {
await this.showCurrent(template)
}
},
async openUpdateModal(template, mode) {
// Only one update runs at a time. Its modal is shown again instead, also
// while it's being sent, because mounting the modal of another template
// stops monitoring the running job.
const current = this.$refs.updateUserTemplateModal
if (current?.jobIsRunning || current?.updateLoading) {
this.updating = true
current.show()
return
}
this.updatedTemplate = template
this.updateMode = mode
this.updating = true
// Mounts the modal of another template first.
await this.$nextTick()
this.$refs.updateUserTemplateModal.show()
},
/**
* The list and the opened preview show the new revision. The updated template
* is among the most recently updated ones of the user.
*/
async templateUpdated(job) {
this.$refs.userTemplateList?.refresh()
this.$refs.publicTemplateList?.refresh()
if (
!this.selectedOwnerActions ||
this.selectedTemplate?.id !== job.template
) {
return
}
try {
const { data } = await TemplateService(this.$client).fetchOwn()
const template = data.results.find(({ id }) => id === job.template)
if (template && this.selectedTemplate?.id === job.template) {
this.selectedTemplate = template
}
} catch (error) {
notifyIf(error, 'templates')
}
},
/**
* An install was refused because the template was updated since it was
* shown, in another tab for example, or because another revision of a public
* template was listed since. The preview then shows the new revision.
*/
templateOutdated() {
return this.showAsNow(this.selectedTemplate)
},
async showCurrent({ id, slug }) {
try {
const { data } = await TemplateService(this.$client).fetch(slug)
if (this.selectedTemplate?.id === id) {
this.selectedTemplate = data
}
} catch (error) {
if (error.response?.status !== 404) {
notifyIf(error, 'templates')
} else if (this.selectedTemplate?.id === id) {
this.selectTemplate(null)
}
}
},
async openDeleteModal(template) {
this.deletedTemplate = template
this.deleting = true
// Mounts the modal the first time.
await this.$nextTick()
this.$refs.deleteUserTemplateModal.show()
},
/**
* Shows the changed visibility of a user's template in the lists, its preview
* and its update modal.
*
* @param {object} template The template as listed in the user's templates.
*/
templateChanged(template) {
this.$refs.userTemplateList?.replace(template)
this.$refs.publicTemplateList?.refresh()
if (
this.selectedOwnerActions &&
this.selectedTemplate?.id === template.id
) {
this.selectedTemplate = template
}
if (this.updatedTemplate?.id === template.id) {
this.updatedTemplate = template
}
},
async openMakePublicModal(template) {
this.madePublicTemplate = template
this.makingPublic = true
// Mounts the modal the first time.
await this.$nextTick()
this.$refs.makeTemplatePublicModal.show()
},
async openMakePrivateModal(template) {
this.madePrivateTemplate = template
this.makingPrivate = true
// Mounts the modal the first time.
await this.$nextTick()
this.$refs.makeTemplatePrivateModal.show()
},
/**
* The deleted template disappears from the list, its preview and its update
* modal, which stops following a job of the template. A template that was
* already deleted may be left out of the list's count already, so the list is
* loaded again instead.
*/
templateDeleted(templateId, alreadyDeleted, requestedAt) {
if (alreadyDeleted) {
this.$refs.userTemplateList?.refresh()
} else {
this.$refs.userTemplateList?.remove(templateId, requestedAt)
}
this.$refs.publicTemplateList?.refresh()
if (
this.selectedOwnerActions &&
this.selectedTemplate?.id === templateId
) {
this.selectTemplate(null)
}
if (this.updatedTemplate?.id === templateId) {
this.updatedTemplate = null
this.updating = false
}
this.recheckOwnsTemplates()
},
/**
* While user templates are off, drops the "Your templates" tab and opens the
* official templates once the user has none left. Only an answer of none
* changes the tab: none can be created while off, so a later answer of some
* is an older one, and a failed check keeps the tab.
*/
async recheckOwnsTemplates() {
if (!this.ownsTemplates) {
return
}
const ownsTemplates = await this.fetchOwnsTemplates().catch(() => true)
if (!ownsTemplates) {
this.ownsTemplates = false
this.userTemplatesOpened = false
if (this.tab === 'own') {
this.selectTab('official')
}
}
},
/**
* Called however the modal is closed, also with escape or a click outside. The
* content stays mounted, so the template is cleared to unmount its preview,
* whose search would otherwise keep answering the shortcut.
*/
hidden() {
this.open = false
// A public template still being opened isn't shown once the modal is closed.
this.selectingPublic = null
this.selectTemplate(null)
},
searchStarted({ event }) {
event.preventDefault()
},
},
}
</script>