Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | 1x 1x 1x 57x 57x 27x 57x 57x 27x 57x 27x 57x 57x 270x 87x 58x 56x |
import { useState, useEffect } from 'react';
import { useQuery } from '@tanstack/react-query';
import { adminService } from '@/services/admin.service';
import type { Configuration, ConfigurationStats } from '@/services/admin.service';
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Plus, Activity, Server, Users, Settings, RefreshCw } from 'lucide-react';
import { Skeleton } from '@/components/ui/skeleton';
import { formatDistanceToNow } from 'date-fns';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import SystemProvidersTab from './SystemProvidersTab';
import AccountProvidersTab from './AccountProvidersTab';
import EmailTemplatesTab from './EmailTemplatesTab';
const REFRESH_OPTIONS = [
{ value: '0', label: 'No refresh' },
{ value: '10', label: '10 seconds' },
{ value: '30', label: '30 seconds' },
{ value: '60', label: '60 seconds' },
{ value: '300', label: '5 minutes' },
];
const STORAGE_KEY = 'config-dashboard-refresh-frequency';
const ConfigurationDashboardPage = () => {
const [activeTab, setActiveTab] = useState('dashboard');
const [refreshFrequency, setRefreshFrequency] = useState<string>(() => {
return localStorage.getItem(STORAGE_KEY) || '0';
});
const { data: stats, isLoading: statsLoading, refetch: refetchStats } = useQuery<ConfigurationStats>({
queryKey: ['admin', 'config', 'stats'],
queryFn: adminService.getStats,
refetchInterval: activeTab === 'dashboard' && parseInt(refreshFrequency) > 0
? parseInt(refreshFrequency) * 1000
: false,
});
const { data: recentConfigs, isLoading: recentLoading, refetch: refetchRecent } = useQuery<Configuration[]>({
queryKey: ['admin', 'config', 'recent'],
queryFn: () => adminService.getRecentConfigs(5),
refetchInterval: activeTab === 'dashboard' && parseInt(refreshFrequency) > 0
? parseInt(refreshFrequency) * 1000
: false,
});
// Persist refresh frequency preference
useEffect(() => {
localStorage.setItem(STORAGE_KEY, refreshFrequency);
}, [refreshFrequency]);
const handleManualRefresh = () => {
refetchStats();
refetchRecent();
};
return (
<div className="space-y-6">
<div className="flex items-center justify-between">
<div>
<h1 className="text-3xl font-bold tracking-tight">Configuration</h1>
<p className="text-muted-foreground">
Manage system and account-level provider configurations.
</p>
</div>
{activeTab === 'dashboard' && (
<div className="flex items-center gap-3">
<Select value={refreshFrequency} onValueChange={setRefreshFrequency}>
<SelectTrigger className="w-40">
<SelectValue placeholder="Refresh frequency" />
</SelectTrigger>
<SelectContent>
{REFRESH_OPTIONS.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
<Button
onClick={handleManualRefresh}
size="icon"
variant="outline"
>
<RefreshCw className="h-4 w-4" />
</Button>
</div>
)}
</div>
<Tabs defaultValue="dashboard" className="space-y-4" value={activeTab} onValueChange={setActiveTab}>
<TabsList>
<TabsTrigger value="dashboard">Dashboard</TabsTrigger>
<TabsTrigger value="system">System Providers</TabsTrigger>
<TabsTrigger value="accounts">Account Providers</TabsTrigger>
<TabsTrigger value="email-templates">Email Templates</TabsTrigger>
</TabsList>
<TabsContent value="dashboard" className="space-y-4">
{/* Overview Cards */}
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
<Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">System Configs</CardTitle>
<Server className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
{statsLoading ? (
<Skeleton className="h-8 w-[100px]" />
) : (
<>
<div className="text-2xl font-bold">{stats?.system_configs.total || 0}</div>
<p className="text-xs text-muted-foreground">
Active system-level providers
</p>
</>
)}
</CardContent>
</Card>
<Card>
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">Account Configs</CardTitle>
<Users className=" h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
{statsLoading ? (
<Skeleton className="h-8 w-[100px]" />
) : (
<>
<div className="text-2xl font-bold">{stats?.account_configs.total || 0}</div>
<p className="text-xs text-muted-foreground">
Active account-level overrides
</p>
</>
)}
</CardContent>
</Card>
{/* Categories Breakdown (Aggregated) */}
<Card className="col-span-2">
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
<CardTitle className="text-sm font-medium">Category Breakdown</CardTitle>
<Activity className="h-4 w-4 text-muted-foreground" />
</CardHeader>
<CardContent>
{statsLoading ? (
<div className="flex gap-4">
<Skeleton className="h-8 w-[100px]" />
<Skeleton className="h-8 w-[100px]" />
<Skeleton className="h-8 w-[100px]" />
</div>
) : (
<div className="flex gap-6 mt-2">
{Object.entries(stats?.system_configs.by_category || {}).map(([category, count]) => (
<div key={`sys-${category}`} className="flex flex-col">
<span className="text-xs text-muted-foreground uppercase">{category.replace('_', ' ')}</span>
<span className="font-bold">{count} <span className="text-xs font-normal text-muted-foreground">(Sys)</span></span>
</div>
))}
{Object.entries(stats?.account_configs.by_category || {}).map(([category, count]) => (
<div key={`acc-${category}`} className="flex flex-col">
<span className="text-xs text-muted-foreground uppercase">{category.replace('_', ' ')}</span>
<span className="font-bold">{count} <span className="text-xs font-normal text-muted-foreground">(Acc)</span></span>
</div>
))}
{(!stats?.system_configs.by_category && !stats?.account_configs.by_category) && (
<span className="text-sm text-muted-foreground">No active configurations</span>
)}
</div>
)}
</CardContent>
</Card>
</div>
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-7">
{/* Recent Activity */}
<Card className="col-span-4">
<CardHeader>
<CardTitle>Recent Activity</CardTitle>
<CardDescription>
Recently modified configurations.
</CardDescription>
</CardHeader>
<CardContent>
{recentLoading ? (
<div className="space-y-4">
<Skeleton className="h-12 w-full" />
<Skeleton className="h-12 w-full" />
<Skeleton className="h-12 w-full" />
</div>
) : (
<div className="space-y-4">
{recentConfigs && recentConfigs.length > 0 ? (
recentConfigs.map((config) => (
<div key={config.id} className="flex items-center justify-between border-b pb-2 last:border-0 last:pb-0">
<div className="flex items-center gap-4">
{config.logo_url ? (
<img src={config.logo_url} alt={config.provider_name} className="h-8 w-8 object-contain" />
) : (
<div className="h-8 w-8 bg-secondary rounded-full flex items-center justify-center">
<Settings className="h-4 w-4" />
</div>
)}
<div>
<p className="font-medium">{config.display_name}</p>
<p className="text-xs text-muted-foreground">
{config.is_system ? 'System' : 'Account'} • {config.category}
</p>
</div>
</div>
<div className="text-xs text-muted-foreground">
{formatDistanceToNow(new Date(config.updated_at), { addSuffix: true })}
</div>
</div>
))
) : (
<div className="text-center py-4 text-muted-foreground">
No recent activity found.
</div>
)}
</div>
)}
</CardContent>
</Card>
{/* Quick Actions */}
<Card className="col-span-3">
<CardHeader>
<CardTitle>Quick Actions</CardTitle>
<CardDescription>
Manage your provider configurations.
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<Button className="w-full justify-start" variant="outline">
<Plus className="mr-2 h-4 w-4" />
Add System Config
</Button>
<Button className="w-full justify-start" variant="outline">
<Plus className="mr-2 h-4 w-4" />
Add Account Config
</Button>
<div className="pt-4">
<p className="text-xs text-muted-foreground mb-2">
Use System Configs to set global defaults for all accounts. Use Account Configs to override settings for specific tenants.
</p>
</div>
</CardContent>
</Card>
</div>
</TabsContent>
<TabsContent value="system" className="space-y-4">
<SystemProvidersTab />
</TabsContent>
<TabsContent value="accounts" className="space-y-4">
<AccountProvidersTab />
</TabsContent>
<TabsContent value="email-templates" className="space-y-4">
<EmailTemplatesTab />
</TabsContent>
</Tabs>
</div>
);
};
export default ConfigurationDashboardPage;
|