/** * Delete-on-uninstall warning dialog store * * Shown in General Settings when the user enables "delete content on uninstall". */ import { defineStore } from 'pinia' import { ref, watch } from 'vue' import type { Ref } from 'vue' import { useLabels } from '@/composables/useLabels' type ButtonType = 'primary' | 'secondary' | 'success' | 'warning' | 'danger' | 'tertiary' export type DeleteOnUninstallDialogData = { title: string subtitle: string dialogType?: 'info' | 'success' | 'warning' | 'error' | 'default' | 'pro' | 'upcoming' buttons?: { close: { type?: ButtonType; text: string; function?: () => void } confirm: { type: ButtonType text: string function?: () => void } } | null } export const useDeleteOnUninstallWarningStore = defineStore('deleteOnUninstallWarning', () => { const defaultDialogData: DeleteOnUninstallDialogData = { title: '', subtitle: '', dialogType: 'default', } const dialogVisible: Ref = ref(false) const dialogData: Ref = ref({ ...defaultDialogData }) const actionType = ref<'delete_on_uninstall_warning'>('delete_on_uninstall_warning') const pendingEnableCallback: Ref<(() => void | Promise) | null> = ref(null) const pendingCancelCallback: Ref<(() => void) | null> = ref(null) let closeTimeoutId: ReturnType | null = null const { getLabel } = useLabels() const clearCloseTimeout = () => { if (closeTimeoutId !== null) { clearTimeout(closeTimeoutId) closeTimeoutId = null } } const resetDialogState = () => { clearCloseTimeout() pendingEnableCallback.value = null pendingCancelCallback.value = null } watch(dialogVisible, (value) => { if (value) { clearCloseTimeout() return } const capturedCancelCallback = pendingCancelCallback.value clearCloseTimeout() closeTimeoutId = setTimeout(() => { closeTimeoutId = null onDialogClose(capturedCancelCallback) }, 150) }) const showDeleteOnUninstallWarningDialog = ( onEnable: () => void | Promise, onCancel: () => void, ) => { clearCloseTimeout() pendingEnableCallback.value = onEnable pendingCancelCallback.value = onCancel dialogData.value = { title: getLabel('delete_on_uninstall_warning_title'), subtitle: getLabel('delete_on_uninstall_warning_message'), dialogType: 'warning', buttons: { close: { type: 'tertiary', text: getLabel('cancel'), function: () => { pendingCancelCallback.value?.() resetDialogState() }, }, confirm: { type: 'warning', text: getLabel('enable'), }, }, } dialogVisible.value = true } const confirmDialog = async () => { if (pendingEnableCallback.value) { await pendingEnableCallback.value() } resetDialogState() dialogVisible.value = false } const onDialogClose = (cancelCallback: (() => void) | null = pendingCancelCallback.value) => { if (cancelCallback && pendingCancelCallback.value === cancelCallback) { cancelCallback() } dialogData.value = { ...defaultDialogData } resetDialogState() } return { dialogVisible, dialogData, actionType, showDeleteOnUninstallWarningDialog, confirmDialog, } })