Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

fix(nc-gui): preserve ai base create query params

Ramesh Mane775a82a5300e
diff --git a/packages/nc-gui/components/workspace/CreateProjectDlg.vue b/packages/nc-gui/components/workspace/CreateProjectDlg.vue
index 8c0b056e61..3fc08a0bd9 100644
--- a/packages/nc-gui/components/workspace/CreateProjectDlg.vue
+++ b/packages/nc-gui/components/workspace/CreateProjectDlg.vue
@@ -3,21 +3,32 @@ import type { RuleObject } from 'ant-design-vue/es/form'
import type { Form, Input } from 'ant-design-vue'
import type { VNodeRef } from '@vue/runtime-core'
import { computed } from '@vue/reactivity'
+import { trimMatchingQuotes } from 'nocodb-sdk'
const props = defineProps<{
modelValue: boolean
+ isCreateNewActionMenu?: boolean
}>()
const emit = defineEmits(['update:modelValue'])
+const router = useRouter()
+const route = router.currentRoute
+
const { t } = useI18n()
+const { isUIAllowed } = useRoles()
+
+const { orgRoles, workspaceRoles, isBaseRolesLoaded } = useRoles()
+
const dialogShow = useVModel(props, 'modelValue', emit)
const basesStore = useBases()
const { createProject: _createProject } = basesStore
+const { isSharedBase } = storeToRefs(useBase())
+
const { navigateToProject } = useGlobal()
const { refreshCommandPalette } = useCommandPalette()
@@ -41,6 +52,24 @@ const formState = ref({
const creating = ref(false)
+const aiMode = ref<boolean | null>(null)
+
+const modalSize = computed(() => (aiMode.value !== true ? 'small' : 'lg'))
+
+const input: VNodeRef = ref<typeof Input>()
+
+const aiModeInitialValue = ref({
+ basePrompt: '',
+ baseName: '',
+})
+
+const hasBaseCreatePermission = computed(
+ () =>
+ !!isBaseRolesLoaded.value &&
+ isUIAllowed('baseCreate', { roles: workspaceRoles.value ?? orgRoles.value }) &&
+ !isSharedBase.value,
+)
+
const createProject = async () => {
if (formState.value.title) {
formState.value.title = formState.value.title.trim()
@@ -68,12 +97,6 @@ const createProject = async () => {
}
}
-const aiMode = ref<boolean | null>(null)
-
-const modalSize = computed(() => (aiMode.value !== true ? 'small' : 'lg'))
-
-const input: VNodeRef = ref<typeof Input>()
-
const onInit = () => {
// Clear errors
setTimeout(async () => {
@@ -93,9 +116,31 @@ const onInit = () => {
}, 5)
}
+const handleResetInitialValue = () => {
+ // Avoid unnecessary reset of initial value
+ if (!aiModeInitialValue.value.basePrompt && !route.value?.query?.basePrompt) return
+
+ aiModeInitialValue.value = {
+ basePrompt: '',
+ baseName: '',
+ }
+
+ /**
+ * We don't want to trigger route change here, so we are using `removeQueryParamsFromURL`
+ */
+ removeQueryParamsFromURL(['basePrompt', 'baseName'])
+}
+
watch(dialogShow, async (n, o) => {
if (n === o && !n) return
+ // If ai prompt is set, don't reset the aiMode value
+ if (n && aiModeInitialValue.value.basePrompt) return
+
+ if (!n) {
+ handleResetInitialValue()
+ }
+
aiMode.value = null
})
@@ -104,6 +149,34 @@ watch(aiMode, () => {
onInit()
})
+
+/**
+ * this `CreateProjectDlg` is used in multiple places and we are trying to show modal dialog based on the query params
+ * So to avoid multiple dialogs being shown, we are using `isCreateNewActionMenu` props
+ */
+if (props.isCreateNewActionMenu) {
+ watch(
+ [() => route.value.query?.basePrompt, () => route.value.query?.baseName, () => hasBaseCreatePermission.value],
+ ([basePrompt, baseName, hasPermission]) => {
+ /**
+ * Avoid showing prefilled ai base create dialog if basePrompt is not available or if dialog is already shown or if rowId is present in the query params
+ */
+ if (!(basePrompt as string)?.trim() || dialogShow.value || route.value.query?.rowId || !hasPermission) return
+
+ aiModeInitialValue.value = {
+ basePrompt: trimMatchingQuotes(basePrompt as string),
+ baseName: trimMatchingQuotes(baseName as string),
+ }
+
+ aiMode.value = true
+
+ dialogShow.value = true
+ },
+ {
+ immediate: true,
+ },
+ )
+}
</script>
<template>
@@ -186,7 +259,12 @@ watch(aiMode, () => {
</div>
</template>
<template v-if="aiMode === true">
- <WorkspaceProjectAiCreateProject v-model:ai-mode="aiMode" v-model:dialog-show="dialogShow" />
+ <WorkspaceProjectAiCreateProject
+ v-model:ai-mode="aiMode"
+ v-model:dialog-show="dialogShow"
+ :is-create-new-action-menu="isCreateNewActionMenu"
+ :initial-value="aiModeInitialValue"
+ />
</template>
</NcModal>
</template>
diff --git a/packages/nc-gui/pages/index.vue b/packages/nc-gui/pages/index.vue
index b4f4365291..5376cd7ded 100644
--- a/packages/nc-gui/pages/index.vue
+++ b/packages/nc-gui/pages/index.vue
@@ -34,7 +34,7 @@ const autoNavigateToProject = async () => {
if (!firstBase?.id) return
- await basesStore.navigateToProject({ baseId: firstBase.id! })
+ await basesStore.navigateToProject({ baseId: firstBase.id!, query: extractAiBaseCreateQueryParams(route.value.query) })
}
const isSharedView = computed(() => {
diff --git a/packages/nc-gui/store/bases.ts b/packages/nc-gui/store/bases.ts
index 22675b73b6..a0756b7391 100644
--- a/packages/nc-gui/store/bases.ts
+++ b/packages/nc-gui/store/bases.ts
@@ -325,17 +325,19 @@ export const useBases = defineStore('basesStore', () => {
bases.value.clear()
}
- const navigateToProject = async ({ baseId, page }: { baseId: string; page?: 'collaborators' }) => {
+ const navigateToProject = async ({ baseId, page, query }: { baseId: string; page?: 'collaborators'; query?: any }) => {
if (!baseId) return
const base = bases.value.get(baseId)
if (!base) return
- if (page) {
- return await navigateTo(`/nc/${baseId}?page=${page}`)
- }
-
- await navigateTo(`/nc/${baseId}`)
+ return await navigateTo({
+ path: `/nc/${baseId}`,
+ query: {
+ ...(page ? { page } : {}),
+ ...(query || {}),
+ },
+ })
}
async function updateIfBaseOrderIsNullOrDuplicate() {
diff --git a/packages/nc-gui/utils/baseUtils.ts b/packages/nc-gui/utils/baseUtils.ts
index 804d7d5689..51a066d402 100644
--- a/packages/nc-gui/utils/baseUtils.ts
+++ b/packages/nc-gui/utils/baseUtils.ts
@@ -90,3 +90,17 @@ export const aiBaseSchemaPromptsReverseMap = Object.fromEntries(
)
export { isDefaultBase }
+
+export const extractAiBaseCreateQueryParams = (query: any) => {
+ const searchQuery = {} as Record<string, string>
+
+ if (query?.basePrompt) {
+ searchQuery['basePrompt'] = query?.basePrompt as string
+ }
+
+ if (query?.baseName) {
+ searchQuery['baseName'] = query?.baseName as string
+ }
+
+ return searchQuery
+}