fmproService / frontend /src /components /UserApiKeysManager.tsx
Hkoradiya123
feat: Initialize frontend with Zustand for state management, Tailwind CSS for styling, and TypeScript for type safety
4854e2b
Raw History Blame Contribute Delete
5.49 kB
/**
* User API Keys Manager Component
*/
import React, { useState } from 'react';
import { APIKey } from '../types';
import { useApiKeysStore } from '../store';
interface UserApiKeysManagerProps {
apiKeys: APIKey[];
}
export const UserApiKeysManager: React.FC<UserApiKeysManagerProps> = ({ apiKeys }) => {
const createMyApiKey = useApiKeysStore((state) => state.createMyApiKey);
const revokeMyApiKey = useApiKeysStore((state) => state.revokeMyApiKey);
const [keyName, setKeyName] = useState('');
const [createdKey, setCreatedKey] = useState<string | null>(null);
const [isSubmitting, setIsSubmitting] = useState(false);
const handleCreate = async (event: React.FormEvent) => {
event.preventDefault();
if (!keyName.trim()) {
return;
}
setIsSubmitting(true);
try {
const created = await createMyApiKey(keyName.trim());
setCreatedKey(created.api_key);
setKeyName('');
} finally {
setIsSubmitting(false);
}
};
return (
<div className="space-y-6">
<div className="panel p-5">
<div className="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
<div>
<p className="eyebrow">Your access key</p>
<h3 className="text-lg font-black text-[color:var(--ink)]">Create API key</h3>
<p className="text-sm text-[color:var(--muted)]">
Keys are shown once. Copy it now and store it securely.
</p>
</div>
<form onSubmit={handleCreate} className="flex w-full flex-col gap-2 md:w-auto md:flex-row">
<input
className="field min-w-[220px]"
placeholder="Key name (e.g. My App)"
value={keyName}
onChange={(event) => setKeyName(event.target.value)}
/>
<button
type="submit"
className="btn-primary px-4 py-2"
disabled={isSubmitting}
>
{isSubmitting ? 'Creating...' : 'Create'}
</button>
</form>
</div>
{createdKey && (
<div className="mt-4 rounded-md border border-[color:var(--line)] bg-[#f7f3e6] p-4">
<p className="text-xs font-bold uppercase text-[color:var(--muted)]">New API key</p>
<p className="mt-2 break-all font-mono text-sm text-[color:var(--ink)]">{createdKey}</p>
</div>
)}
</div>
<div className="panel p-5">
<div className="flex items-center justify-between">
<div>
<p className="eyebrow">Your keys</p>
<h3 className="text-lg font-black text-[color:var(--ink)]">API keys</h3>
</div>
</div>
{apiKeys.length === 0 ? (
<div className="mt-4 rounded-md border border-dashed border-[color:var(--line)] bg-white/70 py-10 text-center text-[color:var(--muted)]">
<p className="font-bold text-[color:var(--ink)]">No API keys yet</p>
<p className="mt-1 text-sm">Create a key to use the recognition API.</p>
</div>
) : (
<div className="mt-4 overflow-x-auto">
<table className="data-table">
<thead>
<tr>
<th className="px-4 py-3 text-left text-sm font-semibold text-gray-700">Name</th>
<th className="px-4 py-3 text-left text-sm font-semibold text-gray-700">Status</th>
<th className="px-4 py-3 text-left text-sm font-semibold text-gray-700">Last Used</th>
<th className="px-4 py-3 text-left text-sm font-semibold text-gray-700">Created</th>
<th className="px-4 py-3 text-right text-sm font-semibold text-gray-700">Actions</th>
</tr>
</thead>
<tbody className="divide-y divide-gray-200">
{apiKeys.map((key) => (
<tr key={key.id}>
<td className="px-4 py-3 text-sm font-medium text-gray-900">{key.key_name}</td>
<td className="px-4 py-3 text-sm">
<span
className={`status-pill ${
key.is_active ? 'bg-green-50 text-green-800' : 'bg-gray-100 text-gray-800'
}`}
>
{key.is_active ? 'Active' : 'Revoked'}
</span>
</td>
<td className="px-4 py-3 text-sm text-gray-600">
{key.last_used_at ? new Date(key.last_used_at).toLocaleString() : 'Never'}
</td>
<td className="px-4 py-3 text-sm text-gray-600">
{new Date(key.created_at).toLocaleDateString()}
</td>
<td className="px-4 py-3 text-right">
{key.is_active ? (
<button
onClick={() => revokeMyApiKey(key.id)}
className="font-bold text-[color:var(--danger)] hover:underline"
>
Revoke
</button>
) : (
<span className="text-xs font-semibold text-[color:var(--muted)]">Revoked</span>
)}
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
</div>
);
};