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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | 2x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 19x 73x 73x 11x 73x 29x 10x 10x 73x 73x 1x 1x 1x 73x 73x 73x 73x 1x 73x 73x 28x 28x 28x 28x 1x 28x 2x 83x 73x 108x 10x 10x 10x 28x 1x | import { useState, useMemo } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { adminService, type Configuration } from '@/services/admin.service';
import type { Account } from '@/services/accounts.service';
import { DataTable, type Column } from '@/components/common/DataTable';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { Switch } from '@/components/ui/switch';
import { Button } from '@/components/ui/button';
import { Trash2, Edit, Plus, Search, Star, StarOff } from 'lucide-react';
import { formatDistanceToNow } from 'date-fns';
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from '@/components/ui/alert-dialog';
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from '@/components/ui/command';
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '@/components/ui/popover';
import { useToast } from '@/hooks/use-toast';
import { Check, ChevronsUpDown } from 'lucide-react';
import { cn } from '@/lib/utils';
import { AddProviderModal } from '@/components/common/AddProviderModal';
import { ConfigurationForm } from '@/components/common/ConfigurationForm';
import { AppDialog } from '@/components/common/AppDialog';
import { ProviderLogo } from '@/components/common/ProviderLogo';
const AccountProvidersTab = () => {
const [selectedAccountId, setSelectedAccountId] = useState<string>('');
const [categoryFilter, setCategoryFilter] = useState<string>('all');
const [configToDelete, setConfigToDelete] = useState<Configuration | null>(null);
const [configToEdit, setConfigToEdit] = useState<Configuration | null>(null);
const [isAddModalOpen, setIsAddModalOpen] = useState(false);
const [page, setPage] = useState(1);
const [pageSize, setPageSize] = useState(10);
const [open, setOpen] = useState(false);
const [searchQuery, setSearchQuery] = useState('');
const queryClient = useQueryClient();
const { toast } = useToast();
// Fetch accounts for the selector
const { data: accountsData } = useQuery({
queryKey: ['admin', 'accounts', searchQuery],
queryFn: () => adminService.getAccounts(searchQuery || undefined, 1, 100),
});
const accounts = accountsData?.items || [];
// Fetch configurations for selected account
const { data: allConfigs, isLoading } = useQuery({
queryKey: ['admin', 'config', 'account', selectedAccountId, categoryFilter],
queryFn: () => adminService.getAccountConfigs(selectedAccountId, categoryFilter),
enabled: !!selectedAccountId,
});
// Client-side pagination
const paginatedData = useMemo(() => {
if (!allConfigs) return [];
const startIndex = (page - 1) * pageSize;
return allConfigs.slice(startIndex, startIndex + pageSize);
}, [allConfigs, page, pageSize]);
const totalItems = allConfigs?.length || 0;
const toggleMutation = useMutation({
mutationFn: ({ id, enabled }: { id: string; enabled: boolean }) =>
adminService.updateConfig(id, enabled),
onSuccess: (data) => {
queryClient.invalidateQueries({ queryKey: ['admin', 'config'] });
toast({
title: 'Configuration Updated',
description: `Provider has been ${data.enabled ? 'enabled' : 'disabled'}.`,
});
},
onError: () => {
toast({
title: 'Error',
description: 'Failed to update configuration.',
variant: 'destructive',
});
},
});
const deleteMutation = useMutation({
mutationFn: (id: string) => adminService.deleteConfig(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['admin', 'config'] });
toast({
title: 'Configuration Deleted',
description: 'Account configuration has been deleted. System default will be used.',
});
setConfigToDelete(null);
},
onError: () => {
toast({
title: 'Error',
description: 'Failed to delete configuration.',
variant: 'destructive',
});
},
});
const setDefaultMutation = useMutation({
mutationFn: (id: string) => adminService.setDefaultConfig(id),
onSuccess: (data) => {
queryClient.invalidateQueries({ queryKey: ['admin', 'config'] });
toast({
title: 'Default Updated',
description: `${data.display_name} is now the default provider.`,
});
},
onError: (error: Error) => {
toast({
title: 'Error',
description: error.message || 'Failed to set default provider.',
variant: 'destructive',
});
},
});
const unsetDefaultMutation = useMutation({
mutationFn: (id: string) => adminService.unsetDefaultConfig(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['admin', 'config'] });
toast({
title: 'Default Cleared',
description: 'Provider is no longer the default.',
});
},
onError: () => {
toast({
title: 'Error',
description: 'Failed to unset default provider.',
variant: 'destructive',
});
},
});
const handleToggle = (config: Configuration) => {
toggleMutation.mutate({ id: config.id, enabled: !config.enabled });
};
const confirmDelete = () => {
if (configToDelete) {
deleteMutation.mutate(configToDelete.id);
}
};
const columns: Column<Configuration>[] = [
{
header: 'Provider',
render: (config) => (
<div className="flex items-center space-x-3">
<ProviderLogo
logoUrl={config.logo_url}
providerName={config.provider_name}
className="h-6 w-6"
/>
<div className="flex flex-col">
<span className="font-medium">{config.display_name}</span>
<span className="text-xs text-muted-foreground">{config.provider_name}</span>
</div>
{config.is_default && (
<span className="ml-2 rounded-full bg-primary/10 px-2 py-0.5 text-xs text-primary font-medium">
Default
</span>
)}
</div>
)
},
{
header: 'Category',
accessorKey: 'category',
render: (config) => <span className="capitalize">{config.category.replace('_', ' ')}</span>
},
{
header: 'Last Updated',
accessorKey: 'updated_at',
render: (config) => (
<span className="text-muted-foreground">
{formatDistanceToNow(new Date(config.updated_at), { addSuffix: true })}
</span>
)
},
{
header: 'Status',
render: (config) => (
<div className="flex items-center space-x-2">
<Switch
checked={config.enabled}
onCheckedChange={() => handleToggle(config)}
disabled={toggleMutation.isPending}
/>
<span className="text-sm text-muted-foreground">
{config.enabled ? 'Enabled' : 'Disabled'}
</span>
</div>
)
},
{
header: 'Actions',
className: 'text-right',
render: (config) => (
<div className="flex justify-end space-x-2">
{config.is_default ? (
<Button
variant="ghost"
size="icon"
title="Unset as default"
onClick={() => unsetDefaultMutation.mutate(config.id)}
disabled={unsetDefaultMutation.isPending}
className="text-primary"
>
<StarOff className="h-4 w-4" />
</Button>
) : (
<Button
variant="ghost"
size="icon"
title={config.enabled ? 'Set as default' : 'Enable this provider before setting it as default'}
onClick={() => config.enabled && setDefaultMutation.mutate(config.id)}
disabled={!config.enabled || setDefaultMutation.isPending}
>
<Star className="h-4 w-4" />
</Button>
)}
<Button
variant="ghost"
size="icon"
onClick={() => setConfigToEdit(config)}
>
<Edit className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="icon"
onClick={() => setConfigToDelete(config)}
className="text-destructive hover:text-destructive"
>
<Trash2 className="h-4 w-4" />
</Button>
</div>
)
}
];
const selectedAccount = accounts.find((acc: Account) => acc.id === selectedAccountId);
return (
<div className="space-y-4">
<div className="flex items-center justify-between">
<div className="flex items-center space-x-2">
{/* Account Selector */}
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
aria-expanded={open}
className="w-75 justify-between"
>
{selectedAccount ? (
<span className="truncate">
{selectedAccount.name} ({selectedAccount.slug})
</span>
) : (
"Select account..."
)}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-75 p-0">
<Command shouldFilter={false}>
<CommandInput
placeholder="Search accounts..."
value={searchQuery}
onValueChange={setSearchQuery}
/>
<CommandList>
<CommandEmpty>No account found.</CommandEmpty>
<CommandGroup>
{accounts.map((account: Account) => (
<CommandItem
key={account.id}
value={account.id}
onSelect={(currentValue: string) => {
setSelectedAccountId(currentValue === selectedAccountId ? '' : currentValue);
setPage(1);
setOpen(false);
}}
>
<Check
className={cn(
"mr-2 h-4 w-4",
selectedAccountId === account.id ? "opacity-100" : "opacity-0"
)}
/>
<div className="flex flex-col">
<span>{account.name}</span>
<span className="text-xs text-muted-foreground">{account.slug}</span>
</div>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
{/* Category Filter */}
{selectedAccountId && (
<Select value={categoryFilter} onValueChange={(val) => {
setCategoryFilter(val);
setPage(1);
}}>
<SelectTrigger className="w-45">
<SelectValue placeholder="Filter by category" />
</SelectTrigger>
<SelectContent>
<SelectItem value="all">All Categories</SelectItem>
<SelectItem value="auth_providers">Auth Providers</SelectItem>
<SelectItem value="email_providers">Email Providers</SelectItem>
</SelectContent>
</Select>
)}
</div>
{selectedAccountId && (
<Button onClick={() => setIsAddModalOpen(true)}>
<Plus className="mr-2 h-4 w-4" /> Add Provider
</Button>
)}
</div>
{!selectedAccountId ? (
<div className="flex h-100 items-center justify-center rounded-md border border-dashed text-muted-foreground">
<div className="text-center">
<Search className="mx-auto h-12 w-12 text-muted-foreground/50 mb-4" />
<p className="text-lg font-medium">Select an account</p>
<p className="text-sm">Choose an account from the dropdown to view its provider configurations</p>
</div>
</div>
) : (
<DataTable
data={paginatedData}
columns={columns}
keyExtractor={(item) => item.id}
isLoading={isLoading}
totalItems={totalItems}
pagination={{
page,
pageSize,
onPageChange: setPage,
onPageSizeChange: (size) => {
setPageSize(size);
setPage(1);
}
}}
noDataMessage="No account-level configurations found. This account is using system defaults."
/>
)}
<AlertDialog open={!!configToDelete} onOpenChange={() => setConfigToDelete(null)}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Are you sure?</AlertDialogTitle>
<AlertDialogDescription>
This will permanently delete the account-level configuration for{' '}
<span className="font-medium text-foreground">
{configToDelete?.display_name}
</span>
. The account will fall back to using the system default configuration.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction
onClick={confirmDelete}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
>
{deleteMutation.isPending ? 'Deleting...' : 'Delete'}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<AddProviderModal
open={isAddModalOpen}
onOpenChange={setIsAddModalOpen}
category={categoryFilter === 'all' ? undefined : categoryFilter}
accountId={selectedAccountId}
onConfigCreated={() => queryClient.invalidateQueries({ queryKey: ['admin', 'config'] })}
existingConfigs={allConfigs || []}
/>
<AppDialog
open={!!configToEdit}
onOpenChange={() => setConfigToEdit(null)}
title={`Configure ${configToEdit?.display_name}`}
className="sm:max-w-xl"
bodyClassName="p-0 overflow-hidden flex flex-col"
>
<div className="flex-1 overflow-hidden h-full flex flex-col">
{configToEdit && (
<ConfigurationForm
category={configToEdit.category}
providerName={configToEdit.provider_name}
displayName={configToEdit.display_name}
logoUrl={configToEdit.logo_url}
configId={configToEdit.id}
accountId={selectedAccountId}
onSuccess={() => {
setConfigToEdit(null);
queryClient.invalidateQueries({ queryKey: ['admin', 'config'] });
}}
onCancel={() => setConfigToEdit(null)}
/>
)}
</div>
</AppDialog>
</div>
);
};
export default AccountProvidersTab;
|