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.
96 lines
2.7 KiB
96 lines
2.7 KiB
2 years ago
|
import { message } from 'ant-design-vue'
|
||
2 years ago
|
import type { WatchStopHandle } from 'vue'
|
||
2 years ago
|
import type { TableInfoType, TableType } from 'nocodb-sdk'
|
||
2 years ago
|
import { useProject } from './useProject'
|
||
2 years ago
|
import { extractSdkResponseErrorMsg } from '~/utils'
|
||
2 years ago
|
import { useNuxtApp, useState } from '#app'
|
||
3 years ago
|
|
||
2 years ago
|
export function useMetas() {
|
||
2 years ago
|
const { $api } = useNuxtApp()
|
||
|
const { tables } = useProject()
|
||
3 years ago
|
|
||
|
const metas = useState<{ [idOrTitle: string]: TableType | any }>('metas', () => ({}))
|
||
2 years ago
|
const loadingState = useState<Record<string, boolean>>('metas-loading-state', () => ({}))
|
||
3 years ago
|
|
||
2 years ago
|
const setMeta = async (model: any) => {
|
||
|
metas.value = {
|
||
|
...metas.value,
|
||
|
[model.id!]: model,
|
||
|
[model.title]: model,
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
const getMeta = async (tableIdOrTitle: string, force = false): Promise<TableType | TableInfoType | null> => {
|
||
2 years ago
|
if (!tableIdOrTitle) return null
|
||
2 years ago
|
/** wait until loading is finished if requesting same meta */
|
||
2 years ago
|
if (!force && loadingState.value[tableIdOrTitle]) {
|
||
2 years ago
|
await new Promise((resolve) => {
|
||
|
let unwatch: WatchStopHandle
|
||
|
|
||
2 years ago
|
// set maximum 20sec timeout to wait loading meta
|
||
2 years ago
|
const timeout = setTimeout(() => {
|
||
|
unwatch?.()
|
||
|
clearTimeout(timeout)
|
||
|
resolve(null)
|
||
2 years ago
|
}, 10000)
|
||
2 years ago
|
|
||
2 years ago
|
// watch for loading state change
|
||
2 years ago
|
unwatch = watch(
|
||
2 years ago
|
() => !!loadingState.value[tableIdOrTitle],
|
||
2 years ago
|
(isLoading) => {
|
||
|
if (!isLoading) {
|
||
|
clearTimeout(timeout)
|
||
|
unwatch?.()
|
||
2 years ago
|
resolve(null)
|
||
2 years ago
|
}
|
||
|
},
|
||
|
{ immediate: true },
|
||
|
)
|
||
|
})
|
||
2 years ago
|
if (metas.value[tableIdOrTitle]) {
|
||
|
return metas.value[tableIdOrTitle]
|
||
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
loadingState.value[tableIdOrTitle] = true
|
||
|
try {
|
||
|
if (!force && metas.value[tableIdOrTitle]) {
|
||
|
return metas.value[tableIdOrTitle]
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
const modelId = tableIdOrTitle.startsWith('md_') ? tableIdOrTitle : tables.value.find((t) => t.title === tableIdOrTitle)?.id
|
||
3 years ago
|
|
||
2 years ago
|
if (!modelId) {
|
||
|
console.warn(`Table '${tableIdOrTitle}' is not found in the table list`)
|
||
|
return null
|
||
|
}
|
||
3 years ago
|
|
||
2 years ago
|
const model = await $api.dbTable.read(modelId)
|
||
|
metas.value = {
|
||
|
...metas.value,
|
||
|
[model.id!]: model,
|
||
|
[model.title]: model,
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return model
|
||
|
} catch (e: any) {
|
||
|
message.error(await extractSdkResponseErrorMsg(e))
|
||
|
} finally {
|
||
|
delete loadingState.value[tableIdOrTitle]
|
||
|
}
|
||
|
return null
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
const clearAllMeta = () => {
|
||
|
metas.value = {}
|
||
|
}
|
||
|
const removeMeta = (idOrTitle: string) => {
|
||
|
const meta = metas.value[idOrTitle]
|
||
|
if (meta) {
|
||
|
delete metas.value[meta.id]
|
||
|
delete metas.value[meta.title]
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
return { getMeta, clearAllMeta, metas, removeMeta, setMeta }
|
||
3 years ago
|
}
|