Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

refactor(frontend): Phase 3 §6.6 — add ConfirmProvider (app-level confirm dialog)

Juan Denis6a0d57587adb
diff --git a/frontend/src/contexts/ConfirmContext.jsx b/frontend/src/contexts/ConfirmContext.jsx
new file mode 100644
index 00000000..93ae1606
--- /dev/null
+++ b/frontend/src/contexts/ConfirmContext.jsx
@@ -0,0 +1,54 @@
+import { createContext, useContext, useCallback, useRef, useState } from 'react';
+import ConfirmDialog from '../components/ConfirmDialog';
+
+// App-level confirm dialog. One <ConfirmDialog> is rendered here; pages call
+// `confirm({...})` (via useConfirm) and await the boolean result instead of
+// each rendering their own dialog. See useConfirm for the back-compat shim that
+// lets un-migrated pages keep working.
+
+const ConfirmContext = createContext(null);
+
+export function ConfirmProvider({ children }) {
+ const [state, setState] = useState({ isOpen: false });
+ const resolver = useRef(null);
+
+ const confirm = useCallback((options = {}) => {
+ return new Promise((resolve) => {
+ resolver.current = resolve;
+ setState({ ...options, isOpen: true });
+ });
+ }, []);
+
+ const settle = useCallback((result) => {
+ if (resolver.current) {
+ resolver.current(result);
+ resolver.current = null;
+ }
+ setState((prev) => ({ ...prev, isOpen: false }));
+ }, []);
+
+ return (
+ <ConfirmContext.Provider value={confirm}>
+ {children}
+ <ConfirmDialog
+ isOpen={state.isOpen}
+ title={state.title}
+ message={state.message}
+ details={state.details}
+ confirmText={state.confirmText}
+ cancelText={state.cancelText}
+ variant={state.variant}
+ requireConfirmation={state.requireConfirmation}
+ confirmationPlaceholder={state.confirmationPlaceholder}
+ onConfirm={() => settle(true)}
+ onCancel={() => settle(false)}
+ />
+ </ConfirmContext.Provider>
+ );
+}
+
+export function useConfirmContext() {
+ return useContext(ConfirmContext);
+}
+
+export default ConfirmContext;
diff --git a/frontend/src/hooks/useConfirm.js b/frontend/src/hooks/useConfirm.js
index d57dcc65..dc8f2991 100644
--- a/frontend/src/hooks/useConfirm.js
+++ b/frontend/src/hooks/useConfirm.js
@@ -1,6 +1,21 @@
import { useState, useCallback } from 'react';
+import { useConfirmContext } from '../contexts/ConfirmContext';
+
+// Confirm-dialog hook.
+//
+// When a <ConfirmProvider> is in the tree (the dashboard), `confirm()` routes
+// to the single app-level dialog and the legacy `confirmState`/`handleConfirm`/
+// `handleCancel` become inert no-ops — so a page's own `<ConfirmDialog
+// isOpen={confirmState.isOpen} />` simply stays closed (harmless) until it is
+// removed. Outside a provider (e.g. pre-auth screens) it falls back to the
+// original local-state behavior so nothing breaks.
+const NOOP = () => {};
export function useConfirm() {
+ const ctxConfirm = useConfirmContext();
+
+ // Local fallback state (only used when there is no provider). Hooks run
+ // unconditionally to satisfy the Rules of Hooks.
const [confirmState, setConfirmState] = useState({
isOpen: false,
title: '',
@@ -8,15 +23,15 @@ export function useConfirm() {
confirmText: 'Confirm',
cancelText: 'Cancel',
variant: 'danger',
- resolve: null
+ resolve: null,
});
- const confirm = useCallback(({
+ const localConfirm = useCallback(({
title = 'Confirm Action',
message = 'Are you sure you want to proceed?',
confirmText = 'Confirm',
cancelText = 'Cancel',
- variant = 'danger'
+ variant = 'danger',
} = {}) => {
return new Promise((resolve) => {
setConfirmState({
@@ -26,31 +41,35 @@ export function useConfirm() {
confirmText,
cancelText,
variant,
- resolve
+ resolve,
});
});
}, []);
const handleConfirm = useCallback(() => {
- if (confirmState.resolve) {
- confirmState.resolve(true);
- }
- setConfirmState(prev => ({ ...prev, isOpen: false }));
- }, [confirmState.resolve]);
+ setConfirmState((prev) => {
+ prev.resolve?.(true);
+ return { ...prev, isOpen: false };
+ });
+ }, []);
const handleCancel = useCallback(() => {
- if (confirmState.resolve) {
- confirmState.resolve(false);
- }
- setConfirmState(prev => ({ ...prev, isOpen: false }));
- }, [confirmState.resolve]);
-
- return {
- confirm,
- confirmState,
- handleConfirm,
- handleCancel
- };
+ setConfirmState((prev) => {
+ prev.resolve?.(false);
+ return { ...prev, isOpen: false };
+ });
+ }, []);
+
+ if (ctxConfirm) {
+ return {
+ confirm: ctxConfirm,
+ confirmState: { isOpen: false },
+ handleConfirm: NOOP,
+ handleCancel: NOOP,
+ };
+ }
+
+ return { confirm: localConfirm, confirmState, handleConfirm, handleCancel };
}
export default useConfirm;
diff --git a/frontend/src/layouts/DashboardLayout.jsx b/frontend/src/layouts/DashboardLayout.jsx
index a89dce73..da20fe6a 100644
--- a/frontend/src/layouts/DashboardLayout.jsx
+++ b/frontend/src/layouts/DashboardLayout.jsx
@@ -7,6 +7,7 @@ import LogsDrawer from '../components/LogsDrawer';
import { LogsDrawerProvider } from '../contexts/LogsDrawerContext';
import { AIProvider } from '../contexts/AIContext';
import AIAssistant from '../components/ai/AIAssistant';
+import { ConfirmProvider } from '../contexts/ConfirmContext';
import PluginLoader from '../plugins/PluginLoader';
import { refreshContributions, useContributions } from '../plugins/contributions';
import useMediaQuery from '../hooks/useMediaQuery';
@@ -77,6 +78,7 @@ const DashboardLayout = () => {
return (
<LogsDrawerProvider>
<AIProvider>
+ <ConfirmProvider>
<div className="dashboard-layout">
<MobileTopBar navOpen={navOpen} onToggle={() => setNavOpen(prev => !prev)} />
<Sidebar
@@ -100,6 +102,7 @@ const DashboardLayout = () => {
<AIAssistant />
<PluginLoader api={api} />
</div>
+ </ConfirmProvider>
</AIProvider>
</LogsDrawerProvider>
);