|
|
@ -1,34 +1,25 @@ |
|
|
|
|
|
|
|
import type { MaybeRef } from '@vueuse/core' |
|
|
|
import { SqlUiFactory } from 'nocodb-sdk' |
|
|
|
import { SqlUiFactory } from 'nocodb-sdk' |
|
|
|
import type { OracleUi, ProjectType, TableType } from 'nocodb-sdk' |
|
|
|
import type { OracleUi, ProjectType, TableType } from 'nocodb-sdk' |
|
|
|
import type { MaybeRef } from '@vueuse/core' |
|
|
|
import { useNuxtApp, useRoute } from '#app' |
|
|
|
import { useNuxtApp, useRoute, useState } from '#app' |
|
|
|
|
|
|
|
import type { ProjectMetaInfo } from '~/lib' |
|
|
|
import type { ProjectMetaInfo } from '~/lib' |
|
|
|
import { USER_PROJECT_ROLES } from '~/lib' |
|
|
|
|
|
|
|
import type { ThemeConfig } from '@/composables/useTheme' |
|
|
|
import type { ThemeConfig } from '@/composables/useTheme' |
|
|
|
|
|
|
|
import { useInjectionState } from '#imports' |
|
|
|
|
|
|
|
|
|
|
|
export function useProject(projectId?: MaybeRef<string>) { |
|
|
|
const [setup, use] = useInjectionState((_projectId?: MaybeRef<string>) => { |
|
|
|
const projectRoles = useState<Record<string, boolean>>(USER_PROJECT_ROLES, () => ({})) |
|
|
|
|
|
|
|
const { $api } = useNuxtApp() |
|
|
|
const { $api } = useNuxtApp() |
|
|
|
let _projectId = $ref('') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const project = useState<ProjectType>('project') |
|
|
|
|
|
|
|
const tables = useState<TableType[]>('tables', () => [] as TableType[]) |
|
|
|
|
|
|
|
const route = useRoute() |
|
|
|
const route = useRoute() |
|
|
|
const { includeM2M } = useGlobal() |
|
|
|
const { includeM2M } = useGlobal() |
|
|
|
const { setTheme } = useTheme() |
|
|
|
const { setTheme } = useTheme() |
|
|
|
const projectMetaInfo = useState<ProjectMetaInfo | undefined>('projectMetaInfo') |
|
|
|
|
|
|
|
|
|
|
|
const projectId = computed(() => (_projectId ? unref(_projectId) : (route.params.projectId as string))) |
|
|
|
|
|
|
|
const project = ref<ProjectType>({}) |
|
|
|
|
|
|
|
const tables = ref<TableType[]>([]) |
|
|
|
|
|
|
|
const projectRoles = ref<Record<string, boolean>>({}) |
|
|
|
|
|
|
|
const projectMetaInfo = ref<ProjectMetaInfo | undefined>() |
|
|
|
|
|
|
|
|
|
|
|
// todo: refactor path param name and variable name
|
|
|
|
// todo: refactor path param name and variable name
|
|
|
|
const projectType = $computed(() => route.params.projectType as string) |
|
|
|
const projectType = $computed(() => route.params.projectType as string) |
|
|
|
const isLoaded = ref(false) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const projectBaseType = $computed(() => project.value?.bases?.[0]?.type || '') |
|
|
|
|
|
|
|
const isMysql = computed(() => ['mysql', 'mysql2'].includes(projectBaseType)) |
|
|
|
|
|
|
|
const isMssql = computed(() => projectBaseType === 'mssql') |
|
|
|
|
|
|
|
const isPg = computed(() => projectBaseType === 'pg') |
|
|
|
|
|
|
|
const sqlUi = computed( |
|
|
|
|
|
|
|
() => SqlUiFactory.create({ client: projectBaseType }) as Exclude<ReturnType<typeof SqlUiFactory['create']>, typeof OracleUi>, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
const isSharedBase = computed(() => projectType === 'base') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const projectMeta = computed(() => { |
|
|
|
const projectMeta = computed(() => { |
|
|
|
try { |
|
|
|
try { |
|
|
@ -38,6 +29,17 @@ export function useProject(projectId?: MaybeRef<string>) { |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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') |
|
|
|
|
|
|
|
|
|
|
|
async function loadProjectMetaInfo(force?: boolean) { |
|
|
|
async function loadProjectMetaInfo(force?: boolean) { |
|
|
|
if (!projectMetaInfo.value || force) { |
|
|
|
if (!projectMetaInfo.value || force) { |
|
|
|
const data = await $api.project.metaGet(project.value.id!, {}, {}) |
|
|
|
const data = await $api.project.metaGet(project.value.id!, {}, {}) |
|
|
@ -74,51 +76,54 @@ export function useProject(projectId?: MaybeRef<string>) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function loadProject() { |
|
|
|
async function loadProject() { |
|
|
|
if (unref(projectId)) { |
|
|
|
if (projectType === 'base') { |
|
|
|
_projectId = unref(projectId)! |
|
|
|
|
|
|
|
} else if (projectType === 'base') { |
|
|
|
|
|
|
|
const baseData = await $api.public.sharedBaseGet(route.params.projectId as string) |
|
|
|
const baseData = await $api.public.sharedBaseGet(route.params.projectId as string) |
|
|
|
_projectId = baseData.project_id! |
|
|
|
project.value = await $api.project.read(baseData.project_id!) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
_projectId = route.params.projectId as string |
|
|
|
project.value = await $api.project.read(projectId.value) |
|
|
|
} |
|
|
|
} |
|
|
|
isLoaded.value = true |
|
|
|
|
|
|
|
project.value = await $api.project.read(_projectId!) |
|
|
|
|
|
|
|
await loadProjectRoles() |
|
|
|
await loadProjectRoles() |
|
|
|
await loadTables() |
|
|
|
await loadTables() |
|
|
|
setTheme(projectMeta.value?.theme) |
|
|
|
setTheme(projectMeta.value?.theme) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function updateProject(data: Partial<ProjectType>) { |
|
|
|
async function updateProject(data: Partial<ProjectType>) { |
|
|
|
if (unref(projectId)) { |
|
|
|
if (projectType === 'base') { |
|
|
|
_projectId = unref(projectId)! |
|
|
|
|
|
|
|
} else if (projectType === 'base') { |
|
|
|
|
|
|
|
return |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (data.meta && typeof data.meta === 'string') { |
|
|
|
|
|
|
|
await $api.project.update(projectId.value, data) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
_projectId = route.params.projectId as string |
|
|
|
await $api.project.update(projectId.value, { ...data, meta: JSON.stringify(data.meta) }) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await $api.project.update(_projectId, data) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function saveTheme(theme: Partial<ThemeConfig>) { |
|
|
|
async function saveTheme(theme: Partial<ThemeConfig>) { |
|
|
|
await updateProject({ |
|
|
|
await updateProject({ |
|
|
|
meta: JSON.stringify({ |
|
|
|
color: theme.primaryColor, |
|
|
|
|
|
|
|
meta: { |
|
|
|
...projectMeta.value, |
|
|
|
...projectMeta.value, |
|
|
|
theme, |
|
|
|
theme, |
|
|
|
}), |
|
|
|
}, |
|
|
|
}) |
|
|
|
}) |
|
|
|
setTheme(theme) |
|
|
|
setTheme(theme) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
watch( |
|
|
|
|
|
|
|
() => route.params, |
|
|
|
|
|
|
|
(v) => { |
|
|
|
|
|
|
|
if (!v?.projectId) { |
|
|
|
|
|
|
|
setTheme() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO useProject should only called inside a project for now this doesn't work
|
|
|
|
onScopeDispose(() => { |
|
|
|
onScopeDispose(() => { |
|
|
|
if (isLoaded.value === true) { |
|
|
|
|
|
|
|
project.value = {} |
|
|
|
project.value = {} |
|
|
|
tables.value = [] |
|
|
|
tables.value = [] |
|
|
|
projectMetaInfo.value = undefined |
|
|
|
projectMetaInfo.value = undefined |
|
|
|
projectRoles.value = {} |
|
|
|
projectRoles.value = {} |
|
|
|
setTheme({}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
return { |
|
|
@ -138,4 +143,16 @@ export function useProject(projectId?: MaybeRef<string>) { |
|
|
|
projectMeta, |
|
|
|
projectMeta, |
|
|
|
saveTheme, |
|
|
|
saveTheme, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}, 'useProject') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const provideProject = setup |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function useProject(projectId?: MaybeRef<string>) { |
|
|
|
|
|
|
|
const state = use() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!state) { |
|
|
|
|
|
|
|
return setup(projectId) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return state |
|
|
|
} |
|
|
|
} |
|
|
|