Files

408 lines
13 KiB
Vue

<template>
<div class="layout__col-2-scroll layout__col-2-scroll--white-background">
<div class="all-workspaces">
<DashboardVerifyEmail
class="margin-top-0 margin-bottom-2"
></DashboardVerifyEmail>
<WorkspaceInvitation
v-for="invitation in workspaceInvitations"
:key="'invitation-' + invitation.id"
:invitation="invitation"
class="margin-top-0 margin-bottom-2"
></WorkspaceInvitation>
<AllWorkspacesHeader
ref="header"
v-model:search="search"
v-model:selected-types="selectedTypes"
v-model:view-mode="viewMode"
v-model:sort-by="sortBy"
:application-types="filterableApplicationTypes"
@collapse-all="collapseAll"
@expand-all="expandAll"
></AllWorkspacesHeader>
<div v-if="!searchActive" class="all-workspaces__boxes">
<AllWorkspacesWorkspaceBox
v-for="workspace in workspaces"
:key="workspace.id"
:workspace="workspace"
:role-name="roleNameOf(workspace)"
:component-arguments="workspaceComponentArguments"
:component-arguments-loading="workspaceComponentArgumentsLoading"
:applications="filteredApplicationsOf(workspace)"
:total-application-count="applicationsOf(workspace).length"
:collapsed="collapsedIds.has(workspace.id)"
:compact="viewMode === 'compact'"
:sort-by="sortBy"
@toggle-collapsed="toggleCollapsed(workspace.id)"
@select-application="selectApplication"
></AllWorkspacesWorkspaceBox>
</div>
<template v-else>
<section
v-if="matchedWorkspaces.length > 0"
class="all-workspaces__section"
>
<h2 class="all-workspaces__section-title">
{{ $t('allWorkspaces.workspacesSection') }}
</h2>
<div class="all-workspaces__boxes">
<AllWorkspacesWorkspaceBox
v-for="workspace in matchedWorkspaces"
:key="'search-workspace-' + workspace.id"
:workspace="workspace"
:role-name="roleNameOf(workspace)"
:component-arguments="workspaceComponentArguments"
:component-arguments-loading="workspaceComponentArgumentsLoading"
:applications="[]"
:total-application-count="applicationsOf(workspace).length"
:highlight="query"
header-only
></AllWorkspacesWorkspaceBox>
</div>
</section>
<section
v-if="matchedApplications.length > 0"
class="all-workspaces__section"
>
<h2 class="all-workspaces__section-title">
{{ $t('allWorkspaces.itemsSection') }}
</h2>
<div class="all-workspaces__grid">
<AllWorkspacesApplicationCard
v-for="match in matchedApplications"
:key="'search-application-' + match.application.id"
:application="match.application"
:workspace="match.workspace"
:highlight="query"
:sort-by="sortBy"
@click="selectApplication(match.application)"
></AllWorkspacesApplicationCard>
</div>
</section>
<div v-if="noResults" class="all-workspaces__empty">
<div class="all-workspaces__empty-title">
{{ $t('allWorkspaces.noResultsTitle') }}
</div>
<div class="all-workspaces__empty-description">
{{ $t('allWorkspaces.noResultsDescription', { search: query }) }}
</div>
<Button
class="all-workspaces__empty-action"
type="secondary"
tag="a"
@click="search = ''"
>{{ $t('allWorkspaces.clearSearch') }}</Button
>
</div>
</template>
</div>
<TemplateModal
v-if="linkedTemplateSlug || linkedOwnTemplates"
ref="templateModal"
source="link"
/>
</div>
</template>
<script setup>
import { computed, ref, onMounted, onBeforeUnmount } from 'vue'
import { useStore } from 'vuex'
import { useRoute, useRouter, useNuxtApp, useAsyncData } from '#app'
import { useHead } from '#imports'
import DashboardVerifyEmail from '@baserow/modules/core/components/dashboard/DashboardVerifyEmail'
import WorkspaceInvitation from '@baserow/modules/core/components/workspace/WorkspaceInvitation'
import AllWorkspacesHeader from '@baserow/modules/core/components/allWorkspaces/AllWorkspacesHeader'
import AllWorkspacesWorkspaceBox from '@baserow/modules/core/components/allWorkspaces/AllWorkspacesWorkspaceBox'
import AllWorkspacesApplicationCard from '@baserow/modules/core/components/allWorkspaces/AllWorkspacesApplicationCard'
import TemplateModal from '@baserow/modules/core/components/template/TemplateModal'
import { getRoleTranslations } from '@baserow/modules/core/store/workspace'
import { SIDEBAR_TYPES } from '@baserow/modules/core/utils/constants'
import { CORE_ACTION_SCOPES } from '@baserow/modules/core/utils/undoRedoConstants'
import { matchesQuery } from '@baserow/modules/core/utils/search'
import {
isTypeFilterActive,
SORT_BY_CREATED,
SORT_BY_LAST_VIEWED,
getApplicationComparator,
getSearchResultComparator,
sortWorkspaces,
} from '@baserow/modules/core/utils/allWorkspaces'
import { useUserPreference } from '@baserow/modules/core/composables/useUserPreference'
import { provideNow } from '@baserow/modules/core/composables/useNow'
// Role uids meaning the user only has access through a lower scope. Showing them
// as a badge next to a workspace the user can open would be misleading.
const HIDDEN_ROLE_UIDS = ['NO_ACCESS', 'NO_ROLE_LOW_PRIORITY']
definePageMeta({
layout: 'app',
sidebarType: SIDEBAR_TYPES.ALL_WORKSPACES,
middleware: [
'settings',
'authenticated',
'impersonate',
'workspacesAndApplications',
'pendingJobs',
],
})
const store = useStore()
const router = useRouter()
const route = useRoute()
const nuxtApp = useNuxtApp()
const { $registry, $i18n } = nuxtApp
// The page lists the applications of every workspace, so actions performed on
// them in their own workspace scope must be undoable here. Unlike the workspace
// and application scopes, which follow the sidebar, this one belongs to the page
// and must not outlive it.
store.dispatch(
'undoRedo/updateCurrentScopeSet',
CORE_ACTION_SCOPES.allWorkspaces(true)
)
onBeforeUnmount(() => {
store.dispatch(
'undoRedo/updateCurrentScopeSet',
CORE_ACTION_SCOPES.allWorkspaces(false)
)
})
const workspaceInvitations = computed(
() => store.getters['auth/getWorkspaceInvitations']
)
await store.dispatch('auth/fetchWorkspaceInvitations')
/**
* Fetches the data the plugins need for the components they add to the
* workspace boxes, for all workspaces at once. It doesn't block the page, and
* because the data is optional, a failure must not break the page either.
*/
async function fetchWorkspaceComponentArguments() {
let mergedData = { workspaceComponentArguments: { usageData: [] } }
for (const plugin of Object.values($registry.getAll('plugin'))) {
try {
const workspaceData = await plugin.fetchAsyncDashboardData(nuxtApp)
if (workspaceData) {
mergedData = plugin.mergeDashboardData(mergedData, workspaceData)
}
} catch (error) {
console.error(error)
}
}
return mergedData.workspaceComponentArguments
}
const { data: fetchedComponentArguments, status } = useAsyncData(
'all-workspaces-component-arguments',
fetchWorkspaceComponentArguments,
{ lazy: true, server: false }
)
const workspaceComponentArguments = computed(
() => fetchedComponentArguments.value ?? {}
)
// `idle` is the tick before the fetch starts.
const workspaceComponentArgumentsLoading = computed(() =>
['idle', 'pending'].includes(status.value)
)
const header = ref(null)
const search = ref('')
const query = computed(() => search.value.trim())
const searchActive = computed(() => query.value !== '')
// A type that can't be created, like one behind a disabled flag, isn't offered.
const filterableApplicationTypes = $registry
.getOrderedList('application')
.filter((applicationType) => applicationType.canBeCreated())
// Starting with nothing selected means "no filtering", which makes toggling
// the first type on feel nicer than having to deselect all the others.
const selectedTypes = ref([])
const typeFilterActive = computed(() =>
isTypeFilterActive(selectedTypes.value, filterableApplicationTypes.length)
)
const collapsedIds = ref(new Set())
const viewMode = useUserPreference('all_workspaces_view_mode', 'expanded')
const sortBy = useUserPreference('all_workspaces_sort_by', SORT_BY_LAST_VIEWED)
const roleTranslations = getRoleTranslations($registry)
function roleNameOf(workspace) {
if (HIDDEN_ROLE_UIDS.includes(workspace.permissions)) {
return ''
}
return roleTranslations[workspace.permissions]?.name ?? ''
}
// Grouped and sorted once per store change instead of once per workspace per
// render. Note that `ApplicationType.isVisible` is deliberately not applied: it
// depends on granular workspace permissions that are only fetched for the
// selected workspace, and no workspace is selected on this page. The backend
// only returns applications the user has access to anyway.
const applicationsByWorkspaceId = computed(() => {
const grouped = new Map()
for (const application of store.getters['application/getAll']) {
const workspaceId = application.workspace.id
if (!grouped.has(workspaceId)) {
grouped.set(workspaceId, [])
}
grouped.get(workspaceId).push(application)
}
const comparator = getApplicationComparator(sortBy.value)
for (const applications of grouped.values()) {
applications.sort(comparator)
}
return grouped
})
const filteredApplicationsByWorkspaceId = computed(() => {
if (!typeFilterActive.value) {
return applicationsByWorkspaceId.value
}
const filtered = new Map()
for (const [workspaceId, applications] of applicationsByWorkspaceId.value) {
filtered.set(
workspaceId,
applications.filter((application) =>
selectedTypes.value.includes(application.type)
)
)
}
return filtered
})
function applicationsOf(workspace) {
return applicationsByWorkspaceId.value.get(workspace.id) ?? []
}
const workspaces = computed(() =>
sortWorkspaces(
store.getters['workspace/getAllSorted'],
sortBy.value,
filteredApplicationsOf
)
)
function filteredApplicationsOf(workspace) {
return filteredApplicationsByWorkspaceId.value.get(workspace.id) ?? []
}
const matchedWorkspaces = computed(() =>
workspaces.value.filter((workspace) =>
matchesQuery(workspace.name, workspace.id, query.value)
)
)
const matchedApplications = computed(() => {
const matches = workspaces.value.flatMap((workspace) =>
filteredApplicationsOf(workspace)
.filter((application) =>
matchesQuery(application.name, application.id, query.value)
)
.map((application) => ({ application, workspace }))
)
if (sortBy.value !== SORT_BY_CREATED) {
// Matches come grouped per workspace, but this section is a flat list.
const comparator = getSearchResultComparator(sortBy.value)
matches.sort((a, b) => comparator(a.application, b.application))
}
return matches
})
const noResults = computed(
() =>
matchedWorkspaces.value.length === 0 &&
matchedApplications.value.length === 0
)
function toggleCollapsed(workspaceId) {
if (collapsedIds.value.has(workspaceId)) {
collapsedIds.value.delete(workspaceId)
} else {
collapsedIds.value.add(workspaceId)
}
}
function collapseAll() {
collapsedIds.value = new Set(workspaces.value.map(({ id }) => id))
}
function expandAll() {
collapsedIds.value = new Set()
}
async function selectApplication(application) {
if (application._.loading) {
return
}
const type = $registry.get('application', application.type)
await store.dispatch('application/setItemLoading', {
application,
value: true,
})
try {
await type.select(application, { $router: router, $store: store, $i18n })
} finally {
await store.dispatch('application/setItemLoading', {
application,
value: false,
})
}
}
function keydownCapture(event) {
if ((event.ctrlKey || event.metaKey) && event.key?.toLowerCase() === 'k') {
event.preventDefault()
// Stop the app layout's bubble phase listener from opening the workspace
// search modal, because on this page the shortcut focuses the inline search.
event.stopPropagation()
header.value?.focusSearch()
}
}
// The cards show relative dates that must keep ageing while the page is open.
provideNow()
const linkedTemplateSlug =
typeof route.query.template === 'string' ? route.query.template : null
const linkedOwnTemplates = route.query.templates === 'own'
const templateModal = ref(null)
onMounted(() => {
document.addEventListener('keydown', keydownCapture, true)
if (linkedTemplateSlug || linkedOwnTemplates) {
router.replace({
query: { ...route.query, template: undefined, templates: undefined },
})
}
if (linkedTemplateSlug) {
templateModal.value.show(linkedTemplateSlug)
} else if (linkedOwnTemplates) {
templateModal.value.showOwn()
}
})
onBeforeUnmount(() => {
document.removeEventListener('keydown', keydownCapture, true)
})
useHead(() => ({
title: $i18n.t('allWorkspaces.title'),
}))
</script>