Skip to content

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

fix(nc-gui): pass key to showInfoModal to avoid showing same modal multiple times

Ramesh Mane22a7689f3378
diff --git a/packages/nc-gui/composables/useNcConfirmModal.ts b/packages/nc-gui/composables/useNcConfirmModal.ts
index 8754abafd8..f529833f98 100644
--- a/packages/nc-gui/composables/useNcConfirmModal.ts
+++ b/packages/nc-gui/composables/useNcConfirmModal.ts
@@ -3,9 +3,39 @@ import NcModalConfirm, { type NcConfirmModalProps } from '../components/nc/Modal
export type NcShowConfirmModalProps = Pick<
NcConfirmModalProps,
'type' | 'title' | 'content' | 'okText' | 'okClass' | 'showCancelBtn' | 'cancelText' | 'showIcon' | 'maskClosable' | 'keyboard'
->
+> & {
+ /**
+ * Use key to avoid showing multiple modals
+ */
+ key?: string
+}
const useNcConfirmModal = () => {
+ const showModalCount = ref(0)
+
+ const openedModalKeyMap = ref<Record<string, boolean>>({})
+
+ const activeModals: Record<string, () => void> = {} // store closeDialog callbacks
+
+ if (import.meta.hot) {
+ import.meta.hot.accept(() => {
+ // Close all modals on hot reload
+ Object.values(activeModals).forEach((fn) => fn())
+ Object.assign(activeModals, {})
+ openedModalKeyMap.value = {}
+ })
+ }
+
+ const getModalKey = () => {
+ const modalKey = `confirm-modal-${++showModalCount.value}`
+
+ if (openedModalKeyMap.value[modalKey]) {
+ return getModalKey()
+ }
+
+ return modalKey
+ }
+
const showConfirmModal = ({
type,
title,
@@ -17,7 +47,16 @@ const useNcConfirmModal = () => {
maskClosable = true,
keyboard = true,
showCancelBtn = false,
+ key,
}: NcShowConfirmModalProps) => {
+ key = key || getModalKey()
+
+ if (openedModalKeyMap.value[key]) {
+ return
+ }
+
+ openedModalKeyMap.value[key] = true
+
const isOpen = ref(true)
const { close } = useDialog(NcModalConfirm, {
@@ -27,20 +66,31 @@ const useNcConfirmModal = () => {
'content': content,
'okText': okText,
'cancelText': cancelText,
- 'onCancel': closeDialog,
- 'onOk': closeDialog,
+ 'onCancel': () => closeDialog(key),
+ 'onOk': () => closeDialog(key),
'okClass': okClass,
- 'update:visible': closeDialog,
+ 'update:visible': () => closeDialog(key),
'showIcon': showIcon,
'maskClosable': maskClosable,
'keyboard': keyboard,
'showCancelBtn': showCancelBtn,
})
- function closeDialog() {
+ function closeDialog(modalKey?: string) {
isOpen.value = false
+
+ if (modalKey) {
+ delete openedModalKeyMap.value[modalKey]
+ delete activeModals[modalKey]
+ }
+
close(1000)
}
+
+ // Keep closeDialog for HMR cleanup
+ if (key) {
+ activeModals[key] = () => closeDialog(key)
+ }
}
return {
@@ -56,6 +106,7 @@ const useNcConfirmModal = () => {
showErrorModal: (props: Omit<NcShowConfirmModalProps, 'type'>) => {
showConfirmModal({ ...props, type: 'error' })
},
+ openedModalKeyMap,
}
}