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.
166 lines
4.5 KiB
166 lines
4.5 KiB
2 years ago
|
import type { MaybeRef } from '@vueuse/core'
|
||
2 years ago
|
import { SqlUiFactory } from 'nocodb-sdk'
|
||
2 years ago
|
import type { OracleUi, ProjectType, TableType } from 'nocodb-sdk'
|
||
2 years ago
|
import { useNuxtApp, useRoute } from '#app'
|
||
2 years ago
|
import type { ProjectMetaInfo } from '~/lib'
|
||
2 years ago
|
import type { ThemeConfig } from '@/composables/useTheme'
|
||
2 years ago
|
import { useInjectionState } from '#imports'
|
||
2 years ago
|
|
||
2 years ago
|
const [setup, use] = useInjectionState((_projectId?: MaybeRef<string>) => {
|
||
2 years ago
|
const { $api } = useNuxtApp()
|
||
2 years ago
|
const route = useRoute()
|
||
2 years ago
|
const { includeM2M } = useGlobal()
|
||
2 years ago
|
const { setTheme, theme } = useTheme()
|
||
2 years ago
|
|
||
|
const projectId = computed(() => (_projectId ? unref(_projectId) : (route.params.projectId as string)))
|
||
|
const project = ref<ProjectType>({})
|
||
|
const tables = ref<TableType[]>([])
|
||
2 years ago
|
const projectRoles = useState<Record<string, boolean>>(USER_PROJECT_ROLES, () => ({}))
|
||
2 years ago
|
const projectMetaInfo = ref<ProjectMetaInfo | undefined>()
|
||
|
|
||
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(() => {
|
||
|
try {
|
||
|
return typeof project.value.meta === 'string' ? JSON.parse(project.value.meta) : project.value.meta
|
||
|
} catch (e) {
|
||
|
return {}
|
||
|
}
|
||
|
})
|
||
|
|
||
2 years ago
|
const projectBaseType = $computed(() => project.value?.bases?.[0]?.type || '')
|
||
|
|
||
|
const sqlUi = computed(
|
||
|
() => SqlUiFactory.create({ client: projectBaseType }) as Exclude<ReturnType<typeof SqlUiFactory['create']>, typeof OracleUi>,
|
||
|
)
|
||
|
|
||
|
const isMysql = computed(() => ['mysql', 'mysql2'].includes(projectBaseType))
|
||
|
const isMssql = computed(() => projectBaseType === 'mssql')
|
||
|
const isPg = computed(() => projectBaseType === 'pg')
|
||
|
const isSharedBase = computed(() => projectType === 'base')
|
||
|
|
||
2 years ago
|
async function loadProjectMetaInfo(force?: boolean) {
|
||
|
if (!projectMetaInfo.value || force) {
|
||
|
const data = await $api.project.metaGet(project.value.id!, {}, {})
|
||
|
projectMetaInfo.value = data
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
async function loadProjectRoles() {
|
||
2 years ago
|
projectRoles.value = {}
|
||
2 years ago
|
if (isSharedBase.value) {
|
||
|
const user = await $api.auth.me(
|
||
|
{},
|
||
|
{
|
||
|
headers: {
|
||
|
'xc-shared-base-id': route.params.projectId,
|
||
|
},
|
||
|
},
|
||
|
)
|
||
|
projectRoles.value = user.roles
|
||
|
} else if (project.value.id) {
|
||
2 years ago
|
const user = await $api.auth.me({ project_id: project.value.id })
|
||
|
projectRoles.value = user.roles
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
async function loadTables() {
|
||
2 years ago
|
if (project.value.id) {
|
||
2 years ago
|
const tablesResponse = await $api.dbTable.list(project.value.id, {
|
||
2 years ago
|
includeM2M: includeM2M.value,
|
||
2 years ago
|
})
|
||
2 years ago
|
if (tablesResponse.list) tables.value = tablesResponse.list
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
async function loadProject() {
|
||
2 years ago
|
if (projectType === 'base') {
|
||
2 years ago
|
const baseData = await $api.public.sharedBaseGet(route.params.projectId as string)
|
||
2 years ago
|
project.value = await $api.project.read(baseData.project_id!)
|
||
2 years ago
|
} else if (projectId.value) {
|
||
2 years ago
|
project.value = await $api.project.read(projectId.value)
|
||
2 years ago
|
} else {
|
||
|
return
|
||
2 years ago
|
}
|
||
2 years ago
|
await loadProjectRoles()
|
||
2 years ago
|
await loadTables()
|
||
2 years ago
|
setTheme(projectMeta.value?.theme)
|
||
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') {
|
||
|
await $api.project.update(projectId.value, data)
|
||
|
} else {
|
||
|
await $api.project.update(projectId.value, { ...data, meta: JSON.stringify(data.meta) })
|
||
|
}
|
||
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
|
setTheme(fullTheme)
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
watch(
|
||
|
() => route.params,
|
||
|
(v) => {
|
||
|
if (!v?.projectId) {
|
||
2 years ago
|
setTheme()
|
||
2 years ago
|
}
|
||
|
},
|
||
|
)
|
||
|
|
||
2 years ago
|
// TODO useProject should only called inside a project for now this doesn't work
|
||
2 years ago
|
onScopeDispose(() => {
|
||
2 years ago
|
project.value = {}
|
||
|
tables.value = []
|
||
|
projectMetaInfo.value = undefined
|
||
|
projectRoles.value = {}
|
||
2 years ago
|
})
|
||
2 years ago
|
|
||
2 years ago
|
return {
|
||
|
project,
|
||
|
tables,
|
||
|
loadProjectRoles,
|
||
|
loadProject,
|
||
2 years ago
|
updateProject,
|
||
2 years ago
|
loadTables,
|
||
|
isMysql,
|
||
2 years ago
|
isMssql,
|
||
2 years ago
|
isPg,
|
||
|
sqlUi,
|
||
|
isSharedBase,
|
||
|
loadProjectMetaInfo,
|
||
|
projectMetaInfo,
|
||
2 years ago
|
projectMeta,
|
||
2 years ago
|
saveTheme,
|
||
2 years ago
|
}
|
||
2 years ago
|
}, 'useProject')
|
||
|
|
||
|
export const provideProject = setup
|
||
|
|
||
|
export function useProject(projectId?: MaybeRef<string>) {
|
||
|
const state = use()
|
||
|
|
||
|
if (!state) {
|
||
|
return setup(projectId)
|
||
|
}
|
||
|
|
||
|
return state
|
||
2 years ago
|
}
|