mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-12-22 17:14:22 +00:00
Refactor CreateApiKeyDialog to use instance IDs
This commit is contained in:
@@ -8,9 +8,9 @@ import { Checkbox } from "@/components/ui/checkbox";
|
|||||||
import { Alert, AlertDescription } from "@/components/ui/alert";
|
import { Alert, AlertDescription } from "@/components/ui/alert";
|
||||||
import { Loader2 } from "lucide-react";
|
import { Loader2 } from "lucide-react";
|
||||||
import { apiKeysApi } from "@/lib/api";
|
import { apiKeysApi } from "@/lib/api";
|
||||||
import { CreateKeyRequest, PermissionMode, InstancePermission } from "@/types/apiKey";
|
import { PermissionMode, type CreateKeyRequest } from "@/types/apiKey";
|
||||||
import { useInstances } from "@/contexts/InstancesContext";
|
import { useInstances } from "@/contexts/InstancesContext";
|
||||||
import { format, addDays } from "date-fns";
|
import { format } from "date-fns";
|
||||||
|
|
||||||
interface CreateApiKeyDialogProps {
|
interface CreateApiKeyDialogProps {
|
||||||
open: boolean;
|
open: boolean;
|
||||||
@@ -61,22 +61,19 @@ function CreateApiKeyDialog({ open, onOpenChange, onKeyCreated }: CreateApiKeyDi
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Build request
|
// Build request
|
||||||
const permissions: InstancePermission[] = [];
|
const instanceIds: number[] = [];
|
||||||
if (permissionMode === PermissionMode.PerInstance) {
|
if (permissionMode === PermissionMode.PerInstance) {
|
||||||
Object.entries(instancePermissions).forEach(([instanceId, canInfer]) => {
|
Object.entries(instancePermissions).forEach(([instanceId, hasPermission]) => {
|
||||||
if (canInfer) {
|
if (hasPermission) {
|
||||||
permissions.push({
|
instanceIds.push(parseInt(instanceId));
|
||||||
InstanceID: parseInt(instanceId),
|
|
||||||
CanInfer: true,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const request: CreateKeyRequest = {
|
const request: CreateKeyRequest = {
|
||||||
Name: name.trim(),
|
name: name.trim(),
|
||||||
PermissionMode: permissionMode,
|
permission_mode: permissionMode,
|
||||||
InstancePermissions: permissions,
|
instance_ids: instanceIds,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add expiration if provided
|
// Add expiration if provided
|
||||||
@@ -87,7 +84,7 @@ function CreateApiKeyDialog({ open, onOpenChange, onKeyCreated }: CreateApiKeyDi
|
|||||||
setError("Expiration date must be in the future");
|
setError("Expiration date must be in the future");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
request.ExpiresAt = Math.floor(expirationDate.getTime() / 1000);
|
request.expires_at = Math.floor(expirationDate.getTime() / 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
@@ -107,10 +104,10 @@ function CreateApiKeyDialog({ open, onOpenChange, onKeyCreated }: CreateApiKeyDi
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleInstancePermissionChange = (instanceId: number, checked: boolean) => {
|
const handleInstancePermissionChange = (instanceId: number, checked: boolean) => {
|
||||||
setInstancePermissions({
|
setInstancePermissions(prev => ({
|
||||||
...instancePermissions,
|
...prev,
|
||||||
[instanceId]: checked,
|
[instanceId]: checked,
|
||||||
});
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -172,25 +169,30 @@ function CreateApiKeyDialog({ open, onOpenChange, onKeyCreated }: CreateApiKeyDi
|
|||||||
<p className="text-sm text-muted-foreground">No instances available</p>
|
<p className="text-sm text-muted-foreground">No instances available</p>
|
||||||
) : (
|
) : (
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
{instances.map((instance) => (
|
{instances.map((instance, index) => {
|
||||||
<div key={instance.id} className="flex items-center space-x-2">
|
const isChecked = !!instancePermissions[instance.id];
|
||||||
<Checkbox
|
return (
|
||||||
id={`instance-${instance.id}`}
|
<div
|
||||||
checked={instancePermissions[instance.id] || false}
|
key={`${instance.name}-${index}`}
|
||||||
onCheckedChange={(checked) =>
|
className="flex items-center space-x-2"
|
||||||
handleInstancePermissionChange(instance.id, checked as boolean)
|
|
||||||
}
|
|
||||||
disabled={loading}
|
|
||||||
/>
|
|
||||||
<Label
|
|
||||||
htmlFor={`instance-${instance.id}`}
|
|
||||||
className="font-normal cursor-pointer flex-1"
|
|
||||||
>
|
>
|
||||||
{instance.name}
|
<Checkbox
|
||||||
</Label>
|
id={`instance-${instance.id}`}
|
||||||
<span className="text-sm text-muted-foreground">Can Infer</span>
|
checked={isChecked}
|
||||||
</div>
|
onCheckedChange={(checked) => {
|
||||||
))}
|
handleInstancePermissionChange(instance.id, checked as boolean);
|
||||||
|
}}
|
||||||
|
disabled={loading}
|
||||||
|
/>
|
||||||
|
<Label
|
||||||
|
htmlFor={`instance-${instance.id}`}
|
||||||
|
className="font-normal cursor-pointer flex-1"
|
||||||
|
>
|
||||||
|
{instance.name}
|
||||||
|
</Label>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState, Fragment } from "react";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
import { Alert, AlertDescription } from "@/components/ui/alert";
|
import { Alert, AlertDescription } from "@/components/ui/alert";
|
||||||
@@ -175,9 +175,8 @@ function ApiKeysSection() {
|
|||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{keys.map((key) => (
|
{keys.map((key) => (
|
||||||
<>
|
<Fragment key={key.id}>
|
||||||
<tr
|
<tr
|
||||||
key={key.id}
|
|
||||||
className="border-t hover:bg-muted/50 cursor-pointer"
|
className="border-t hover:bg-muted/50 cursor-pointer"
|
||||||
onClick={() => handleRowClick(key)}
|
onClick={() => handleRowClick(key)}
|
||||||
>
|
>
|
||||||
@@ -236,25 +235,15 @@ function ApiKeysSection() {
|
|||||||
<p className="text-sm text-muted-foreground">Loading permissions...</p>
|
<p className="text-sm text-muted-foreground">Loading permissions...</p>
|
||||||
) : permissions[key.id] ? (
|
) : permissions[key.id] ? (
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<p className="text-sm font-semibold">Instance Permissions:</p>
|
<p className="text-sm font-semibold">Allowed Instances:</p>
|
||||||
<table className="w-full text-sm">
|
<ul className="text-sm space-y-1">
|
||||||
<thead>
|
{permissions[key.id].map((perm) => (
|
||||||
<tr className="border-b">
|
<li key={perm.instance_id} className="flex items-center gap-2">
|
||||||
<th className="text-left py-2">Instance Name</th>
|
<Check className="h-3 w-3 text-green-600" />
|
||||||
<th className="text-left py-2">Can Infer</th>
|
{perm.instance_name}
|
||||||
</tr>
|
</li>
|
||||||
</thead>
|
))}
|
||||||
<tbody>
|
</ul>
|
||||||
{permissions[key.id].map((perm) => (
|
|
||||||
<tr key={perm.instance_id} className="border-b">
|
|
||||||
<td className="py-2">{perm.instance_name}</td>
|
|
||||||
<td className="py-2">
|
|
||||||
<Check className="h-4 w-4 text-green-600" />
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
))}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<p className="text-sm text-muted-foreground">No permissions data</p>
|
<p className="text-sm text-muted-foreground">No permissions data</p>
|
||||||
@@ -262,7 +251,7 @@ function ApiKeysSection() {
|
|||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
)}
|
)}
|
||||||
</>
|
</Fragment>
|
||||||
))}
|
))}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
Reference in New Issue
Block a user