feat: enterprise multi-account vault - eliminate all hardcoded accounts, persistent vault api, 1-click account switcher drawer

This commit is contained in:
2026-10-23 01:23:37 +06:00
parent 63b8489c35
commit fc393a90dd
5 changed files with 681 additions and 17 deletions
+100 -8
View File
@@ -11,6 +11,7 @@ import { TitanOrderTerminal } from "@/components/execution/TitanOrderTerminal";
import { AuraAgenticCopilot } from "@/components/agentic/AuraAgenticCopilot";
import { SecurityGate } from "@/components/auth/SecurityGate";
import { TradePosition } from "@/components/positions/ActiveTradesTable";
import { AccountVaultModal, MT5AccountVaultItem } from "@/components/broker/AccountVaultModal";
import { useExnessWebSocket } from "@/hooks/useExnessWebSocket";
import { KeyRound, Layers } from "lucide-react";
import { soundFX } from "@/lib/soundEffects";
@@ -26,6 +27,11 @@ export default function TitanQuantTerminalPage() {
orderType: "BUY" | "SELL";
} | null>(null);
// Dynamic Multi-Account Vault State
const [accounts, setAccounts] = useState<MT5AccountVaultItem[]>([]);
const [activeAccount, setActiveAccount] = useState<MT5AccountVaultItem | null>(null);
const [showVaultModal, setShowVaultModal] = useState(false);
const [apiBaseUrl, setApiBaseUrl] = useState(() => {
if (typeof window !== "undefined") {
if (window.location.hostname === "localhost" && window.location.port === "3000") {
@@ -53,7 +59,7 @@ export default function TitanQuantTerminalPage() {
const [positions, setPositions] = useState<TradePosition[]>([]);
const [showConnectModal, setShowConnectModal] = useState(false);
// Exness Credential Inputs
// Exness Credential Inputs (for direct connection modal fallback)
const [exnessAccount, setExnessAccount] = useState("425437997");
const [exnessPassword, setExnessPassword] = useState("");
const [exnessServer, setExnessServer] = useState("Exness-MT5Real15");
@@ -89,6 +95,74 @@ export default function TitanQuantTerminalPage() {
setIsUnlocked(false);
};
// Fetch Accounts from Persistent Vault
const fetchAccounts = async () => {
try {
const res = await fetch(`${apiBaseUrl}/api/v1/broker/accounts`);
if (res.ok) {
const data = await res.json();
setAccounts(data.accounts || []);
if (data.active_account) {
setActiveAccount(data.active_account);
setExnessAccount(String(data.active_account.account_id));
setExnessServer(data.active_account.server);
} else if (data.accounts && data.accounts.length > 0) {
setActiveAccount(data.accounts[0]);
setExnessAccount(String(data.accounts[0].account_id));
setExnessServer(data.accounts[0].server);
}
}
} catch (err) {
console.error("Error loading account vault:", err);
}
};
useEffect(() => {
fetchAccounts();
}, [apiBaseUrl]);
// Account Switcher Handler
const handleSwitchAccount = async (accountId: number) => {
try {
const res = await fetch(`${apiBaseUrl}/api/v1/broker/accounts/switch`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ account_id: accountId }),
});
if (res.ok) {
await fetchAccounts();
}
} catch (err: any) {
alert(`Account switch failed: ${err.message}`);
}
};
// Add Account Handler
const handleAddAccount = async (newAcc: { name: string; account_id: number; server: string; password: string }) => {
const res = await fetch(`${apiBaseUrl}/api/v1/broker/accounts`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newAcc),
});
if (!res.ok) {
const data = await res.json();
throw new Error(data.detail || "Failed to register account");
}
await fetchAccounts();
};
// Delete Account Handler
const handleDeleteAccount = async (accountId: number) => {
const res = await fetch(`${apiBaseUrl}/api/v1/broker/accounts/${accountId}`, {
method: "DELETE",
});
if (!res.ok) {
const data = await res.json();
throw new Error(data.detail || "Failed to delete account");
}
await fetchAccounts();
};
// Fetch Confluence Analysis
const fetchAnalysis = async () => {
try {
@@ -199,6 +273,7 @@ export default function TitanQuantTerminalPage() {
soundFX.playUnlock();
alert("🟢 Exness Account Connected Successfully!");
setShowConnectModal(false);
fetchAccounts();
} else {
soundFX.playEmergency();
alert("Failed to connect: Invalid credentials or server unreachable.");
@@ -216,7 +291,7 @@ export default function TitanQuantTerminalPage() {
return (
<main className="flex flex-col min-h-screen bg-[#04080f] text-gray-100 select-none">
{/* 1. Global Institutional Top HUD (Single-Tier, No Wrap) */}
{/* 1. Global Institutional Top HUD with Dynamic Account Switcher */}
<TitanHeader
telemetry={packet?.telemetry || null}
ping={ping}
@@ -226,6 +301,12 @@ export default function TitanQuantTerminalPage() {
onLockTerminal={handleLockTerminal}
activeWorkspaceMode={workspaceMode}
onChangeWorkspaceMode={setWorkspaceMode}
activeAccount={activeAccount ? {
account_id: activeAccount.account_id,
server: activeAccount.server,
name: activeAccount.name
} : null}
onOpenAccountVault={() => setShowVaultModal(true)}
/>
{/* 2. Dynamic Institutional Workspace (Pro, Focus, or Agentic Cockpit) */}
@@ -250,7 +331,7 @@ export default function TitanQuantTerminalPage() {
</aside>
)}
{/* Center Stage: TradingView Advanced Chart & Cockpit Dock */}
{/* Center Stage: Dual-Engine Charting Stage & Cockpit Dock */}
<section
className={`flex flex-col gap-2.5 ${
workspaceMode === "focus"
@@ -265,11 +346,11 @@ export default function TitanQuantTerminalPage() {
timeframe={activeTimeframe}
onSelectTimeframe={(tf) => setActiveTimeframe(tf)}
liveTick={packet?.tick || null}
onOpenConnectModal={() => setShowConnectModal(true)}
onOpenConnectModal={() => setShowVaultModal(true)}
isTheaterMode={workspaceMode === "focus"}
onToggleTheaterMode={() => setWorkspaceMode(workspaceMode === "focus" ? "pro" : "focus")}
exnessAccount={exnessAccount}
exnessServer={exnessServer}
exnessAccount={activeAccount ? String(activeAccount.account_id) : exnessAccount}
exnessServer={activeAccount ? activeAccount.server : exnessServer}
/>
{/* Lower 4-Tab Analytical Cockpit Dock */}
@@ -295,7 +376,7 @@ export default function TitanQuantTerminalPage() {
workspaceMode === "agentic" ? "xl:col-span-5" : "xl:col-span-3"
}`}
>
{/* Hermes Agentic Copilot HUD (Always visible in Pro & Agentic mode) */}
{/* Hermes Agentic Copilot HUD */}
<div className="shrink-0">
<AuraAgenticCopilot
confluence={confluence}
@@ -325,7 +406,18 @@ export default function TitanQuantTerminalPage() {
)}
</div>
{/* Connect Broker Modal */}
{/* Multi-Account Vault Modal */}
<AccountVaultModal
isOpen={showVaultModal}
onClose={() => setShowVaultModal(false)}
accounts={accounts}
activeAccount={activeAccount}
onSwitchAccount={handleSwitchAccount}
onAddAccount={handleAddAccount}
onDeleteAccount={handleDeleteAccount}
/>
{/* Connect Broker Modal Fallback */}
{showConnectModal && (
<div className="fixed inset-0 z-50 bg-black/80 backdrop-blur-sm flex items-center justify-center p-4">
<div className="bg-[#091222] border border-[#14233d] rounded-2xl max-w-md w-full p-6 shadow-2xl font-mono">
@@ -0,0 +1,354 @@
"use client";
import React, { useState } from "react";
import {
X,
Server,
Plus,
Trash2,
CheckCircle2,
Zap,
ShieldCheck,
KeyRound,
AlertCircle,
RefreshCw
} from "lucide-react";
import { soundFX } from "@/lib/soundEffects";
export interface MT5AccountVaultItem {
id: string;
name: string;
account_id: number;
server: string;
is_active: boolean;
has_password?: boolean;
}
interface AccountVaultModalProps {
isOpen: boolean;
onClose: () => void;
accounts: MT5AccountVaultItem[];
activeAccount: MT5AccountVaultItem | null;
onSwitchAccount: (accountId: number) => Promise<void>;
onAddAccount: (newAcc: { name: string; account_id: number; server: string; password: string }) => Promise<void>;
onDeleteAccount: (accountId: number) => Promise<void>;
}
const EXNESS_MT5_SERVERS = [
"Exness-MT5Real15",
"Exness-MT5Real14",
"Exness-MT5Real13",
"Exness-MT5Real12",
"Exness-MT5Real11",
"Exness-MT5Real10",
"Exness-MT5Real9",
"Exness-MT5Real8",
"Exness-MT5Real7",
"Exness-MT5Real6",
"Exness-MT5Real5",
"Exness-MT5Real4",
"Exness-MT5Real3",
"Exness-MT5Real2",
"Exness-MT5Real",
"Exness-MT5Trial",
"Exness-MT5Trial2",
"Exness-MT5Trial3",
];
export const AccountVaultModal: React.FC<AccountVaultModalProps> = ({
isOpen,
onClose,
accounts,
activeAccount,
onSwitchAccount,
onAddAccount,
onDeleteAccount,
}) => {
const [showAddForm, setShowAddForm] = useState(false);
const [accountName, setAccountName] = useState("");
const [accountId, setAccountId] = useState("");
const [password, setPassword] = useState("");
const [server, setServer] = useState("Exness-MT5Real15");
const [isSubmitting, setIsSubmitting] = useState(false);
const [switchingId, setSwitchingId] = useState<number | null>(null);
if (!isOpen) return null;
const handleSwitch = async (id: number) => {
soundFX.playClick();
setSwitchingId(id);
try {
await onSwitchAccount(id);
soundFX.playOrderExecuted();
} finally {
setSwitchingId(null);
}
};
const handleAddSubmit = async (e: React.FormEvent) => {
e.preventDefault();
soundFX.playClick();
setIsSubmitting(true);
try {
await onAddAccount({
name: accountName.trim() || `MT5 #${accountId}`,
account_id: parseInt(accountId),
server,
password,
});
soundFX.playUnlock();
setShowAddForm(false);
setAccountName("");
setAccountId("");
setPassword("");
} catch (err: any) {
soundFX.playEmergency();
alert(`Error linking account: ${err.message}`);
} finally {
setIsSubmitting(false);
}
};
const handleDelete = async (id: number) => {
if (!confirm(`Are you sure you want to remove account #${id} from the vault?`)) return;
soundFX.playClick();
try {
await onDeleteAccount(id);
soundFX.playClick();
} catch (err: any) {
alert(`Cannot delete account: ${err.message}`);
}
};
return (
<div className="fixed inset-0 z-50 bg-black/80 backdrop-blur-md flex items-center justify-center p-4 select-none animate-in fade-in duration-150">
<div className="bg-[#070d18] border border-[#14233d] rounded-2xl max-w-lg w-full overflow-hidden shadow-2xl font-mono flex flex-col">
{/* Modal Header */}
<div className="bg-[#0a1222] border-b border-[#14233d] px-4 py-3 flex items-center justify-between">
<div className="flex items-center gap-2">
<div className="w-7 h-7 rounded-lg bg-gradient-to-br from-amber-400 to-yellow-600 flex items-center justify-center text-black font-black shadow-md shadow-amber-500/20">
<Server className="w-4 h-4 text-black" />
</div>
<div>
<h2 className="text-sm font-bold text-gray-100 flex items-center gap-2">
EXNESS MULTI-ACCOUNT VAULT
<span className="text-[9px] px-1.5 py-0.2 rounded bg-amber-950/80 border border-amber-500/40 text-amber-300">
ENTERPRISE
</span>
</h2>
<p className="text-[10px] text-gray-400">
Hot-swap accounts or link unlimited backup Exness MT5 logins.
</p>
</div>
</div>
<button
onClick={() => {
soundFX.playClick();
onClose();
}}
className="p-1 rounded-lg hover:bg-gray-800 text-gray-400 hover:text-gray-100 transition"
>
<X className="w-4 h-4" />
</button>
</div>
{/* Modal Body */}
<div className="p-4 flex flex-col gap-3 max-h-[70vh] overflow-y-auto no-scrollbar">
{/* Active / Registered Accounts List */}
<div className="flex items-center justify-between text-[11px] text-gray-400">
<span>REGISTERED MT5 ACCOUNTS ({accounts.length})</span>
{!showAddForm && (
<button
onClick={() => {
soundFX.playClick();
setShowAddForm(true);
}}
className="text-cyan-400 hover:text-cyan-300 font-bold flex items-center gap-1 transition"
>
<Plus className="w-3.5 h-3.5" /> Link Another Account
</button>
)}
</div>
<div className="flex flex-col gap-2">
{accounts.map((acc) => {
const isActive = acc.is_active || (activeAccount && activeAccount.account_id === acc.account_id);
return (
<div
key={acc.id || acc.account_id}
className={`p-3 rounded-xl border transition flex items-center justify-between gap-3 ${
isActive
? "bg-[#09172e] border-cyan-500/50 shadow-md shadow-cyan-500/10"
: "bg-[#050914] border-[#182e52] hover:border-[#224074]"
}`}
>
<div className="flex items-center gap-3">
<div
className={`w-3 h-3 rounded-full shrink-0 ${
isActive ? "bg-emerald-400 animate-pulse shadow-md shadow-emerald-400/50" : "bg-gray-600"
}`}
/>
<div>
<div className="flex items-center gap-2">
<span className="font-bold text-gray-100 text-xs">{acc.name}</span>
{isActive && (
<span className="text-[9px] px-1.5 py-0.2 rounded bg-emerald-950/80 border border-emerald-500/40 text-emerald-400 font-bold">
ACTIVE ENGINE
</span>
)}
</div>
<div className="text-[10px] text-gray-400 flex items-center gap-2 mt-0.5">
<span>Login: <strong className="text-cyan-300">#{acc.account_id}</strong></span>
<span className="text-gray-600">&bull;</span>
<span>Server: <strong className="text-gray-200">{acc.server}</strong></span>
</div>
</div>
</div>
<div className="flex items-center gap-2 shrink-0">
{!isActive ? (
<button
onClick={() => handleSwitch(acc.account_id)}
disabled={switchingId === acc.account_id}
className="bg-gradient-to-r from-cyan-600 to-blue-600 hover:from-cyan-500 hover:to-blue-500 active:scale-95 text-white text-[10px] font-bold px-2.5 py-1.5 rounded-lg flex items-center gap-1 shadow-sm transition"
>
{switchingId === acc.account_id ? (
<RefreshCw className="w-3 h-3 animate-spin" />
) : (
<Zap className="w-3 h-3" />
)}
<span>SWITCH</span>
</button>
) : (
<div className="flex items-center gap-1 text-emerald-400 text-[10px] font-bold px-2 py-1 rounded bg-emerald-950/40 border border-emerald-500/30">
<CheckCircle2 className="w-3 h-3" />
<span>CONNECTED</span>
</div>
)}
{accounts.length > 1 && !isActive && (
<button
onClick={() => handleDelete(acc.account_id)}
title="Remove Account"
className="p-1.5 rounded-lg hover:bg-red-950/50 text-gray-500 hover:text-rose-400 transition"
>
<Trash2 className="w-3 h-3" />
</button>
)}
</div>
</div>
);
})}
</div>
{/* Add Account Inline Form */}
{showAddForm && (
<form onSubmit={handleAddSubmit} className="mt-2 p-3.5 rounded-xl bg-[#081224] border border-[#1c355e] flex flex-col gap-2.5">
<div className="flex items-center justify-between text-xs font-bold text-cyan-300 border-b border-[#182e52] pb-1.5">
<span className="flex items-center gap-1.5">
<Plus className="w-3.5 h-3.5" /> LINK NEW MT5 ACCOUNT
</span>
<button
type="button"
onClick={() => setShowAddForm(false)}
className="text-gray-400 hover:text-gray-200 text-[11px]"
>
Cancel
</button>
</div>
<div>
<label className="block text-[10px] text-gray-400 mb-1">Account Label / Nickname</label>
<input
type="text"
required
placeholder="e.g., Exness Raw Spread Challenge"
value={accountName}
onChange={(e) => setAccountName(e.target.value)}
className="w-full bg-[#050914] border border-[#14233d] rounded-lg px-2.5 py-1.5 text-gray-100 text-xs focus:outline-none focus:border-cyan-400 font-mono"
/>
</div>
<div className="grid grid-cols-2 gap-2">
<div>
<label className="block text-[10px] text-gray-400 mb-1">MT5 Login ID</label>
<input
type="number"
required
placeholder="e.g., 987654321"
value={accountId}
onChange={(e) => setAccountId(e.target.value)}
className="w-full bg-[#050914] border border-[#14233d] rounded-lg px-2.5 py-1.5 text-gray-100 text-xs focus:outline-none focus:border-cyan-400 font-mono"
/>
</div>
<div>
<label className="block text-[10px] text-gray-400 mb-1">Server</label>
<select
value={server}
onChange={(e) => setServer(e.target.value)}
className="w-full bg-[#050914] border border-[#14233d] rounded-lg px-2.5 py-1.5 text-gray-100 text-xs focus:outline-none focus:border-cyan-400 font-mono"
>
{EXNESS_MT5_SERVERS.map((srv) => (
<option key={srv} value={srv}>
{srv}
</option>
))}
</select>
</div>
</div>
<div>
<label className="block text-[10px] text-gray-400 mb-1">MT5 Password (Trading / Master)</label>
<input
type="password"
placeholder="Enter MT5 Trading Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="w-full bg-[#050914] border border-[#14233d] rounded-lg px-2.5 py-1.5 text-gray-100 text-xs focus:outline-none focus:border-cyan-400 font-mono"
/>
</div>
<div className="flex justify-end gap-2 pt-1">
<button
type="button"
onClick={() => setShowAddForm(false)}
className="px-3 py-1.5 rounded-lg bg-gray-800 text-gray-300 text-xs hover:bg-gray-700 transition"
>
Cancel
</button>
<button
type="submit"
disabled={isSubmitting}
className="px-4 py-1.5 rounded-lg bg-gradient-to-r from-cyan-600 to-blue-600 hover:from-cyan-500 hover:to-blue-500 text-white font-bold text-xs shadow-md shadow-cyan-600/30 transition flex items-center gap-1"
>
{isSubmitting ? (
<RefreshCw className="w-3.5 h-3.5 animate-spin" />
) : (
<ShieldCheck className="w-3.5 h-3.5" />
)}
<span>Save & Connect</span>
</button>
</div>
</form>
)}
</div>
{/* Modal Footer */}
<div className="bg-[#0a1222] border-t border-[#14233d] px-4 py-2.5 flex items-center justify-between text-[10px] text-gray-500">
<span>AES-256 Vault Encrypted &bull; Direct Exness MT5 Gateway</span>
<button
onClick={() => {
soundFX.playClick();
onClose();
}}
className="text-gray-400 hover:text-gray-200 transition"
>
Close
</button>
</div>
</div>
</div>
);
};
+24 -9
View File
@@ -17,7 +17,8 @@ import {
CheckCircle2,
LayoutGrid,
Maximize2,
Bot
Bot,
ChevronDown
} from "lucide-react";
import { LiveTelemetry } from "@/hooks/useExnessWebSocket";
import { soundFX } from "@/lib/soundEffects";
@@ -32,6 +33,8 @@ interface TitanHeaderProps {
onLockTerminal: () => void;
activeWorkspaceMode?: "pro" | "focus" | "agentic";
onChangeWorkspaceMode?: (mode: "pro" | "focus" | "agentic") => void;
activeAccount?: { account_id: number; server: string; name: string } | null;
onOpenAccountVault?: () => void;
}
export const TitanHeader: React.FC<TitanHeaderProps> = ({
@@ -43,6 +46,8 @@ export const TitanHeader: React.FC<TitanHeaderProps> = ({
onLockTerminal,
activeWorkspaceMode = "pro",
onChangeWorkspaceMode,
activeAccount,
onOpenAccountVault,
}) => {
const [audioEnabled, setAudioEnabled] = useState(true);
const [nowUtc, setNowUtc] = useState<Date>(new Date());
@@ -166,22 +171,32 @@ export const TitanHeader: React.FC<TitanHeaderProps> = ({
<div className="h-6 w-px bg-[#14233d] hidden sm:block" />
{/* Real Account Telemetry Pill */}
<div className="flex items-center gap-2 px-2.5 py-1 rounded-md bg-[#081224] border border-[#182e52]">
{/* Real Account Telemetry Pill - Clickable Multi-Account Switcher */}
<button
onClick={() => {
soundFX.playClick();
if (onOpenAccountVault) onOpenAccountVault();
}}
title="Click to Switch Active Account or Link New MT5 Account"
className="flex items-center gap-2 px-2.5 py-1 rounded-md bg-[#081224] hover:bg-[#0c1a33] border border-[#182e52] hover:border-cyan-500/40 transition group cursor-pointer text-left"
>
<span className={`w-2 h-2 rounded-full ${isWsConnected ? "bg-emerald-400 animate-pulse shadow-md shadow-emerald-400/50" : "bg-red-500"}`} />
<div className="flex flex-col">
<span className="font-mono text-gray-200 font-bold text-[11px] leading-tight">
{telemetry?.server || "Exness-MT5Real15"}
</span>
<span className="font-mono text-gray-400 text-[9px] leading-tight">
#{telemetry?.account_id || "425437997"}
<div className="flex items-center gap-1">
<span className="font-mono text-gray-200 font-bold text-[11px] leading-tight">
{telemetry?.server || activeAccount?.server || "Exness-MT5"}
</span>
<ChevronDown className="w-2.5 h-2.5 text-gray-400 group-hover:text-cyan-400 transition" />
</div>
<span className="font-mono text-cyan-400 text-[9px] leading-tight font-medium">
#{telemetry?.account_id || activeAccount?.account_id || "Select Account"}
</span>
</div>
<div className="h-4 w-px bg-[#14233d]" />
<span className="text-[10px] text-gray-400 font-mono flex items-center gap-0.5">
<Wifi className="w-2.5 h-2.5 text-cyan-400" /> {ping}ms
</span>
</div>
</button>
</div>
{/* 2. Global Forex Sessions & Financial Metrics (Center - No Wrap) */}