import React, { createContext, useContext, useState, useEffect } from 'react'; export interface Account { _id: string; steamId: string; personaName: string; loginName?: string; steamLoginSecure?: string; loginConfig?: any; sessionUpdatedAt?: string; autoCheckCooldown: boolean; avatar: string; localAvatar?: string; profileUrl: string; status: string; vacBanned: boolean; gameBans: number; lastBanCheck: string; lastScrapeTime?: string; cooldownExpiresAt?: string; authError?: boolean; notes?: string; } export interface ServerConfig { url: string; token?: string; serverSteamId?: string; enabled: boolean; isAdmin?: boolean; } interface AccountsContextType { accounts: Account[]; serverConfig: ServerConfig | null; isLoading: boolean; isSyncing: boolean; addAccount: (data: { identifier: string }) => Promise; updateAccount: (id: string, data: Partial) => Promise; deleteAccount: (id: string) => Promise; switchAccount: (loginName: string) => Promise; openSteamAppLogin: () => Promise; openSteamLogin: (steamId: string) => Promise; shareAccountWithUser: (steamId: string, targetSteamId: string) => Promise; revokeAccountAccess: (steamId: string, targetSteamId: string) => Promise; revokeAllAccountAccess: (steamId: string) => Promise; // Server Methods updateServerConfig: (config: Partial) => Promise; loginToServer: () => Promise; syncNow: () => Promise; scrapeAccount: (steamId: string) => Promise; getCommunityAccounts: () => Promise; getServerUsers: () => Promise; refreshAccounts: (showLoading?: boolean) => Promise; // Admin Methods adminGetStats: () => Promise; adminGetUsers: () => Promise; adminDeleteUser: (userId: string) => Promise; adminGetAccounts: () => Promise; adminRemoveAccount: (steamId: string) => Promise; } const AccountsContext = createContext(undefined); export const AccountsProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { const [accounts, setAccounts] = useState([]); const [serverConfig, setServerConfig] = useState(null); const [isLoading, setIsLoading] = useState(true); const [isSyncing, setIsSyncing] = useState(false); const refreshAccounts = async (showLoading = false) => { if (showLoading) setIsLoading(true); try { const api = (window as any).electronAPI; if (!api) { console.warn("[useAccounts] electronAPI not found in window"); return; } console.log("[useAccounts] Fetching data from main process..."); const accData = await api.getAccounts(); const configData = await api.getServerConfig(); setAccounts(Array.isArray(accData) ? accData : []); setServerConfig(configData || null); } catch (error) { console.error("[useAccounts] Error loading accounts:", error); } finally { setIsLoading(false); } }; useEffect(() => { refreshAccounts(true); const api = (window as any).electronAPI; if (api?.onAccountsUpdated) { const cleanup = api.onAccountsUpdated((updatedAccounts: Account[]) => { setAccounts(Array.isArray(updatedAccounts) ? updatedAccounts : []); }); return typeof cleanup === 'function' ? cleanup : undefined; } }, []); const syncNow = async () => { setIsSyncing(true); try { await (window as any).electronAPI.syncNow(); await refreshAccounts(); } catch (e) { console.error("[useAccounts] Sync failed", e); } finally { setIsSyncing(false); } }; const scrapeAccount = async (steamId: string) => { const success = await (window as any).electronAPI.scrapeAccount(steamId); if (success) await syncNow(); return success; }; const addAccount = async (data: { identifier: string }) => { await (window as any).electronAPI.addAccount(data); await refreshAccounts(); await syncNow(); }; const updateAccount = async (id: string, data: Partial) => { await (window as any).electronAPI.updateAccount(id, data); await refreshAccounts(); }; const deleteAccount = async (id: string) => { if (!window.confirm("Are you sure you want to remove this account?")) return; await (window as any).electronAPI.deleteAccount(id); await refreshAccounts(); }; const switchAccount = async (loginName: string) => { if (!loginName) return; await (window as any).electronAPI.switchAccount(loginName); }; const openSteamAppLogin = async () => { await (window as any).electronAPI.openSteamAppLogin(); }; const openSteamLogin = async (steamId: string) => { await (window as any).electronAPI.openSteamLogin(steamId); await syncNow(); }; const shareAccountWithUser = async (steamId: string, targetSteamId: string) => { const res = await (window as any).electronAPI.shareAccountWithUser(steamId, targetSteamId); await syncNow(); return res; }; const revokeAccountAccess = async (steamId: string, targetSteamId: string) => { const res = await (window as any).electronAPI.revokeAccountAccess(steamId, targetSteamId); await syncNow(); return res; }; const revokeAllAccountAccess = async (steamId: string) => { const res = await (window as any).electronAPI.revokeAllAccountAccess(steamId); await syncNow(); return res; }; const updateServerConfig = async (config: Partial) => { const updated = await (window as any).electronAPI.updateServerConfig(config); setServerConfig(updated); }; const loginToServer = async () => { await (window as any).electronAPI.loginToServer(); await refreshAccounts(); await syncNow(); }; const getCommunityAccounts = async () => { return await (window as any).electronAPI.getCommunityAccounts(); }; const getServerUsers = async () => { return await (window as any).electronAPI.getServerUsers(); }; // --- Admin Methods --- const adminGetStats = async () => (window as any).electronAPI.adminGetStats(); const adminGetUsers = async () => (window as any).electronAPI.adminGetUsers(); const adminDeleteUser = async (userId: string) => (window as any).electronAPI.adminDeleteUser(userId); const adminGetAccounts = async () => (window as any).electronAPI.adminGetAccounts(); const adminRemoveAccount = async (steamId: string) => (window as any).electronAPI.adminRemoveAccount(steamId); return ( {children} ); }; export const useAccounts = () => { const context = useContext(AccountsContext); if (context === undefined) { throw new Error('useAccounts must be used within an AccountsProvider'); } return context; };