File size: 5,493 Bytes
4854e2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * 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>
  );
};