Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
"use client";
/**
* useConnectionDeleteConfirm — Issue #7361.
*
* Gates single-connection delete behind a confirmation step, mirroring the
* existing batch-delete confirm pattern already wired in
* useProviderConnections.ts (handleBatchDeleteOpenModal / handleBatchDeleteConfirm).
* Extracted into its own module so the frozen useProviderConnections.ts file-size
* ratchet does not have to absorb the extra state + handlers.
*/
import { useCallback, useState } from "react";
import { useTranslations } from "next-intl";
import { providerText } from "../providerPageHelpers";
export interface ConnectionDeleteConfirmTarget {
id: string;
name: string;
}
export interface ConnectionDeleteConfirmState {
connection: ConnectionDeleteConfirmTarget | null;
deleting: boolean;
request: (connectionId: string, name: string) => void;
confirm: () => Promise<void>;
cancel: () => void;
}
interface NotifyLike {
success: (message: string) => void;
error: (message: string) => void;
}
export function useConnectionDeleteConfirm(
fetchConnections: () => Promise<void>,
notify: NotifyLike
): ConnectionDeleteConfirmState {
const t = useTranslations("providers");
const [connection, setConnection] = useState<ConnectionDeleteConfirmTarget | null>(null);
const [deleting, setDeleting] = useState(false);
const request = useCallback((connectionId: string, name: string) => {
if (!connectionId) return;
setConnection({ id: connectionId, name });
}, []);
const cancel = useCallback(() => {
setConnection(null);
}, []);
const confirm = useCallback(async () => {
const connectionId = connection?.id;
if (!connectionId) {
setConnection(null);
return;
}
setDeleting(true);
try {
const res = await fetch(`/api/providers/${connectionId}`, { method: "DELETE" });
if (res.ok) {
notify.success(providerText(t, "connectionDeleted", "Connection deleted"));
await fetchConnections();
} else {
const data = await res.json().catch(() => ({}));
const message =
(typeof data?.error === "string" && data.error) ||
data?.error?.message ||
providerText(t, "failedDeleteConnection", "Failed to delete connection");
notify.error(message);
}
} catch (error) {
console.error("Error deleting connection:", error);
notify.error(providerText(t, "failedDeleteConnection", "Failed to delete connection"));
} finally {
setDeleting(false);
setConnection(null);
}
}, [connection, fetchConnections, notify, t]);
return { connection, deleting, request, confirm, cancel };
}