mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
222 lines
5.4 KiB
222 lines
5.4 KiB
2 years ago
|
import type { BaseType, OracleUi, ProjectType, TableType } from 'nocodb-sdk'
|
||
2 years ago
|
import { SqlUiFactory } from 'nocodb-sdk'
|
||
|
import { isString } from '@vueuse/core'
|
||
|
import {
|
||
2 years ago
|
ClientType,
|
||
2 years ago
|
computed,
|
||
|
createEventHook,
|
||
2 years ago
|
createSharedComposable,
|
||
2 years ago
|
ref,
|
||
|
useApi,
|
||
|
useGlobal,
|
||
|
useNuxtApp,
|
||
2 years ago
|
useRoles,
|
||
2 years ago
|
useRouter,
|
||
2 years ago
|
useTheme,
|
||
|
} from '#imports'
|
||
2 years ago
|
import type { ProjectMetaInfo, ThemeConfig } from '~/lib'
|
||
2 years ago
|
|
||
2 years ago
|
export const project = createSharedComposable(() => {
|
||
2 years ago
|
const { $e } = useNuxtApp()
|
||
|
|
||
|
const { api, isLoading } = useApi()
|
||
|
|
||
2 years ago
|
const router = useRouter()
|
||
|
|
||
|
const route = $(router.currentRoute)
|
||
2 years ago
|
|
||
2 years ago
|
const { includeM2M } = useGlobal()
|
||
2 years ago
|
|
||
2 years ago
|
const { setTheme, theme } = useTheme()
|
||
2 years ago
|
|
||
2 years ago
|
const { projectRoles, loadProjectRoles } = useRoles()
|
||
|
|
||
2 years ago
|
const projectLoadedHook = createEventHook<ProjectType>()
|
||
|
|
||
2 years ago
|
const project = ref<ProjectType>({})
|
||
2 years ago
|
|
||
2 years ago
|
const bases = computed<BaseType[]>(() => project.value?.bases || [])
|
||
2 years ago
|
|
||
2 years ago
|
const tables = ref<TableType[]>([])
|
||
2 years ago
|
|
||
2 years ago
|
const projectMetaInfo = ref<ProjectMetaInfo | undefined>()
|
||
|
|
||
2 years ago
|
const lastOpenedViewMap = ref<Record<string, string>>({})
|
||
|
|
||
2 years ago
|
const forcedProjectId = ref<string>()
|
||
2 years ago
|
|
||
2 years ago
|
const projectId = computed(() => forcedProjectId.value || (route.params.projectId as string))
|
||
2 years ago
|
|
||
2 years ago
|
// todo: refactor path param name and variable name
|
||
2 years ago
|
const projectType = $computed(() => route.params.projectType as string)
|
||
2 years ago
|
|
||
2 years ago
|
const projectMeta = computed<Record<string, any>>(() => {
|
||
2 years ago
|
const defaultMeta = {
|
||
|
showNullAndEmptyInFilter: false,
|
||
|
}
|
||
2 years ago
|
try {
|
||
2 years ago
|
return (isString(project.value.meta) ? JSON.parse(project.value.meta) : project.value.meta) ?? defaultMeta
|
||
2 years ago
|
} catch (e) {
|
||
2 years ago
|
return defaultMeta
|
||
2 years ago
|
}
|
||
|
})
|
||
|
|
||
2 years ago
|
const sqlUis = computed(() => {
|
||
|
const temp: Record<string, any> = {}
|
||
|
for (const base of bases.value) {
|
||
|
if (base.id) {
|
||
|
temp[base.id] = SqlUiFactory.create({ client: base.type }) as Exclude<
|
||
|
ReturnType<typeof SqlUiFactory['create']>,
|
||
|
typeof OracleUi
|
||
|
>
|
||
|
}
|
||
|
}
|
||
|
return temp
|
||
|
})
|
||
|
|
||
2 years ago
|
function getBaseType(baseId?: string) {
|
||
2 years ago
|
return bases.value.find((base) => base.id === baseId)?.type || ClientType.MYSQL
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
function isMysql(baseId?: string) {
|
||
2 years ago
|
return ['mysql', ClientType.MYSQL].includes(getBaseType(baseId))
|
||
|
}
|
||
|
|
||
2 years ago
|
function isMssql(baseId?: string) {
|
||
2 years ago
|
return getBaseType(baseId) === 'mssql'
|
||
|
}
|
||
|
|
||
2 years ago
|
function isPg(baseId?: string) {
|
||
2 years ago
|
return getBaseType(baseId) === 'pg'
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
function isXcdbBase(baseId?: string) {
|
||
|
return bases.value.find((base) => base.id === baseId)?.is_meta
|
||
|
}
|
||
|
|
||
2 years ago
|
const isSharedBase = computed(() => projectType === 'base')
|
||
|
|
||
2 years ago
|
async function loadProjectMetaInfo(force?: boolean) {
|
||
|
if (!projectMetaInfo.value || force) {
|
||
2 years ago
|
projectMetaInfo.value = await api.project.metaGet(project.value.id!, {}, {})
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
async function loadTables() {
|
||
|
if (project.value.id) {
|
||
|
const tablesResponse = await api.dbTable.list(project.value.id, {
|
||
2 years ago
|
includeM2M: includeM2M.value,
|
||
2 years ago
|
})
|
||
2 years ago
|
|
||
2 years ago
|
if (tablesResponse.list) {
|
||
|
tables.value = tablesResponse.list.filter((table) => bases.value.find((base) => base.id === table.base_id)?.enabled)
|
||
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
async function loadProject(withTheme = true, forcedId?: string) {
|
||
2 years ago
|
if (forcedId) forcedProjectId.value = forcedId
|
||
2 years ago
|
if (projectType === 'base') {
|
||
2 years ago
|
try {
|
||
|
const baseData = await api.public.sharedBaseGet(route.params.projectId as string)
|
||
2 years ago
|
|
||
2 years ago
|
project.value = await api.project.read(baseData.project_id!)
|
||
2 years ago
|
} catch (e: any) {
|
||
2 years ago
|
if (e?.response?.status === 404) {
|
||
2 years ago
|
return router.push('/error/404')
|
||
2 years ago
|
}
|
||
|
throw e
|
||
|
}
|
||
2 years ago
|
} else if (projectId.value) {
|
||
2 years ago
|
project.value = await api.project.read(projectId.value)
|
||
2 years ago
|
} else {
|
||
2 years ago
|
console.warn('Project id not found')
|
||
2 years ago
|
return
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
await loadProjectRoles(project.value.id || projectId.value, isSharedBase.value, projectId.value)
|
||
2 years ago
|
|
||
2 years ago
|
await loadTables()
|
||
2 years ago
|
|
||
2 years ago
|
if (withTheme) setTheme(projectMeta.value?.theme)
|
||
2 years ago
|
|
||
2 years ago
|
return projectLoadedHook.trigger(project.value)
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
async function updateProject(data: Partial<ProjectType>) {
|
||
2 years ago
|
if (projectType === 'base') {
|
||
2 years ago
|
return
|
||
|
}
|
||
2 years ago
|
if (data.meta && typeof data.meta === 'string') {
|
||
2 years ago
|
await api.project.update(projectId.value, data)
|
||
2 years ago
|
} else {
|
||
2 years ago
|
await api.project.update(projectId.value, { ...data, meta: JSON.stringify(data.meta) })
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
async function saveTheme(_theme: Partial<ThemeConfig>) {
|
||
|
const fullTheme = {
|
||
|
primaryColor: theme.value.primaryColor,
|
||
|
accentColor: theme.value.accentColor,
|
||
|
..._theme,
|
||
|
}
|
||
|
|
||
2 years ago
|
await updateProject({
|
||
2 years ago
|
color: fullTheme.primaryColor,
|
||
2 years ago
|
meta: {
|
||
2 years ago
|
...projectMeta.value,
|
||
2 years ago
|
theme: fullTheme,
|
||
2 years ago
|
},
|
||
2 years ago
|
})
|
||
2 years ago
|
|
||
2 years ago
|
setTheme(fullTheme)
|
||
2 years ago
|
|
||
|
$e('c:themes:change')
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
async function hasEmptyOrNullFilters() {
|
||
|
return await api.project.hasEmptyOrNullFilters(projectId.value)
|
||
|
}
|
||
|
|
||
2 years ago
|
const reset = () => {
|
||
2 years ago
|
project.value = {}
|
||
|
tables.value = []
|
||
|
projectMetaInfo.value = undefined
|
||
|
projectRoles.value = {}
|
||
2 years ago
|
setTheme()
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
watch(
|
||
|
() => route.params.projectType,
|
||
|
(n) => {
|
||
|
if (!n) reset()
|
||
|
},
|
||
|
{ immediate: true },
|
||
|
)
|
||
|
|
||
2 years ago
|
return {
|
||
|
project,
|
||
2 years ago
|
bases,
|
||
2 years ago
|
tables,
|
||
|
loadProjectRoles,
|
||
|
loadProject,
|
||
2 years ago
|
updateProject,
|
||
2 years ago
|
loadTables,
|
||
|
isMysql,
|
||
2 years ago
|
isMssql,
|
||
2 years ago
|
isPg,
|
||
2 years ago
|
sqlUis,
|
||
2 years ago
|
isSharedBase,
|
||
|
loadProjectMetaInfo,
|
||
|
projectMetaInfo,
|
||
2 years ago
|
projectMeta,
|
||
2 years ago
|
saveTheme,
|
||
2 years ago
|
projectLoadedHook: projectLoadedHook.on,
|
||
2 years ago
|
reset,
|
||
|
isLoading,
|
||
2 years ago
|
lastOpenedViewMap,
|
||
2 years ago
|
isXcdbBase,
|
||
2 years ago
|
hasEmptyOrNullFilters,
|
||
2 years ago
|
}
|
||
2 years ago
|
})
|