Browse Source

revert(nc-gui): use route project id

pull/3801/head
braks 2 years ago
parent
commit
f6b0dbb56a
  1. 46
      packages/nc-gui/composables/useProject.ts

46
packages/nc-gui/composables/useProject.ts

@ -1,4 +1,3 @@
import type { MaybeRef } from '@vueuse/core'
import type { OracleUi, ProjectType, TableType } from 'nocodb-sdk' import type { OracleUi, ProjectType, TableType } from 'nocodb-sdk'
import { SqlUiFactory } from 'nocodb-sdk' import { SqlUiFactory } from 'nocodb-sdk'
import { isString } from '@vueuse/core' import { isString } from '@vueuse/core'
@ -6,8 +5,6 @@ import {
computed, computed,
createEventHook, createEventHook,
ref, ref,
tryOnScopeDispose,
unref,
useApi, useApi,
useGlobal, useGlobal,
useInjectionState, useInjectionState,
@ -16,11 +13,10 @@ import {
useRoute, useRoute,
useRouter, useRouter,
useTheme, useTheme,
watch,
} from '#imports' } from '#imports'
import type { ProjectMetaInfo, ThemeConfig } from '~/lib' import type { ProjectMetaInfo, ThemeConfig } from '~/lib'
const [setup, use] = useInjectionState((_projectId?: MaybeRef<string>) => { const [setup, use] = useInjectionState(() => {
const { $e } = useNuxtApp() const { $e } = useNuxtApp()
const { api, isLoading } = useApi() const { api, isLoading } = useApi()
@ -43,7 +39,7 @@ const [setup, use] = useInjectionState((_projectId?: MaybeRef<string>) => {
const projectMetaInfo = ref<ProjectMetaInfo | undefined>() const projectMetaInfo = ref<ProjectMetaInfo | undefined>()
const projectId = computed(() => (_projectId ? unref(_projectId) : (route.params.projectId as string))) const projectId = computed(() => route.params.projectId as string)
// 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)
@ -73,9 +69,9 @@ const [setup, use] = useInjectionState((_projectId?: MaybeRef<string>) => {
} }
} }
async function loadTables(id?: string) { async function loadTables() {
if (id || project.value.id) { if (project.value.id) {
const tablesResponse = await api.dbTable.list((id || project.value.id)!, { const tablesResponse = await api.dbTable.list(project.value.id, {
includeM2M: includeM2M.value, includeM2M: includeM2M.value,
}) })
@ -83,10 +79,8 @@ const [setup, use] = useInjectionState((_projectId?: MaybeRef<string>) => {
} }
} }
async function loadProject(id?: string) { async function loadProject() {
if (id) { if (projectType === 'base') {
project.value = await api.project.read(id)
} else if (projectType === 'base') {
try { try {
const baseData = await api.public.sharedBaseGet(route.params.projectId as string) const baseData = await api.public.sharedBaseGet(route.params.projectId as string)
@ -100,15 +94,13 @@ const [setup, use] = useInjectionState((_projectId?: MaybeRef<string>) => {
} else if (projectId.value) { } else if (projectId.value) {
project.value = await api.project.read(projectId.value) project.value = await api.project.read(projectId.value)
} else { } else {
console.warn('Project id not found')
return return
} }
await loadProjectRoles( await loadProjectRoles(project.value.id || projectId.value, isSharedBase.value, projectId.value)
id || project.value.id || (route.params.projectId as string), isSharedBase.value,
route.params.projectId as string,
)
await loadTables(id) await loadTables()
setTheme(projectMeta.value?.theme) setTheme(projectMeta.value?.theme)
@ -146,20 +138,12 @@ const [setup, use] = useInjectionState((_projectId?: MaybeRef<string>) => {
$e('c:themes:change') $e('c:themes:change')
} }
watch(
() => route.params,
(next) => {
if (!next.projectId) {
setTheme()
}
},
)
const reset = () => { const reset = () => {
project.value = {} project.value = {}
tables.value = [] tables.value = []
projectMetaInfo.value = undefined projectMetaInfo.value = undefined
projectRoles.value = {} projectRoles.value = {}
setTheme()
} }
return { return {
@ -186,15 +170,11 @@ const [setup, use] = useInjectionState((_projectId?: MaybeRef<string>) => {
export const provideProject = setup export const provideProject = setup
export function useProject(projectId?: MaybeRef<string>) { export function useProject() {
const state = use() const state = use()
if (!state) { if (!state) {
const setupState = setup(projectId) return setup()
tryOnScopeDispose(setupState.reset)
return setupState
} }
return state return state

Loading…
Cancel
Save