aboutsummaryrefslogtreecommitdiff
path: root/web/ui/src/routes/dashboard/keys/+page.svelte
blob: b0a066ffa0201e23d99d0ba096ac76d9596533a1 (plain)
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
<script lang="ts">
  import { enhance } from '$app/forms';
  import { DATASETS } from '$lib/datasets';
  import { maskKey, scopeSummary } from '$lib/keys';
  import { pushToast } from '$lib/stores/toasts';
  import type { PageData, ActionData } from './$types';

  let { data, form }: { data: PageData; form: ActionData } = $props();

  let showCreate = $state(false);
  let name = $state('');
  let selected = $state<Set<string>>(new Set());
  let allScopes = $state(true);

  function toggleChip(slug: string) {
    if (allScopes) {
      allScopes = false;
      selected = new Set([slug]);
      return;
    }
    const next = new Set(selected);
    if (next.has(slug)) next.delete(slug);
    else next.add(slug);
    if (next.size === 0) {
      allScopes = true;
      selected = new Set();
    } else {
      selected = next;
    }
  }

  function selectAll() {
    allScopes = true;
    selected = new Set();
  }

  function fmtDate(ts: number): string {
    return new Date(ts).toLocaleDateString('en-US', {
      month: 'short',
      day: 'numeric',
      year: 'numeric'
    });
  }

  async function copy(text: string) {
    try {
      await navigator.clipboard.writeText(text);
      pushToast('Copied to clipboard', 'success');
    } catch {
      pushToast("Couldn't copy — select it manually", 'error');
    }
  }

  // When a new key is returned in `form`, show toast + scroll to top.
  $effect(() => {
    if (form && 'created' in form && form.created) {
      pushToast(`Key "${form.created.name}" created`, 'success');
      showCreate = false;
      name = '';
      selected = new Set();
      allScopes = true;
    } else if (form && 'revokedId' in form && form.revokedId) {
      pushToast('Key revoked', 'success');
    } else if (form && 'error' in form && form.error) {
      pushToast(form.error, 'error');
    }
  });
</script>

<p class="section-marker">§ 01 &nbsp;&middot;&nbsp; api keys</p>
<div class="row-between mb-24">
  <div>
    <h1 class="page-title">Your API keys.</h1>
    <p class="page-subtitle">
      {data.activeCount} active, {data.keys.length - data.activeCount} revoked. Your
      {data.plan.name} plan allows
      {Number.isFinite(data.plan.maxKeys) ? data.plan.maxKeys : 'unlimited'}
      active key{data.plan.maxKeys === 1 ? '' : 's'}.
    </p>
  </div>
  {#if !showCreate}
    <button class="btn btn-primary" onclick={() => (showCreate = true)}>
      + New key
    </button>
  {/if}
</div>

{#if form && 'created' in form && form.created}
  <div class="key-reveal">
    <div class="key-reveal-head">
      <span class="badge">New key</span>
      <strong>Copy this key now — it won't be shown again.</strong>
    </div>
    <div class="key-reveal-value">
      <span style="flex: 1;">{form.created.plaintext}</span>
      <button class="btn btn-sm btn-ghost" onclick={() => copy(form.created!.plaintext)}>
        Copy
      </button>
    </div>
    <p class="key-reveal-warn">
      Store it somewhere safe. Once you leave this page we only keep the hash.
    </p>
  </div>
{/if}

{#if showCreate}
  <form
    method="POST"
    action="?/create"
    class="card card-accent mb-24"
    use:enhance
  >
    <div class="card-head">
      <h2 class="card-title">New key</h2>
      <button type="button" class="btn btn-sm btn-ghost" onclick={() => (showCreate = false)}>
        Cancel
      </button>
    </div>

    <div class="field">
      <label class="field-label" for="name">name</label>
      <input
        id="name"
        name="name"
        class="input"
        placeholder="e.g. production ingest"
        bind:value={name}
        required
      />
      <p class="help">Just for you — how you'll recognize this key in the list.</p>
    </div>

    <div class="field">
      <span class="field-label">dataset scope</span>
      <div class="chip-grid">
        <button
          type="button"
          class="chip chip-all {allScopes ? 'active' : ''}"
          onclick={selectAll}
        >
          all datasets
        </button>
        {#each DATASETS as slug}
          <button
            type="button"
            class="chip {selected.has(slug) ? 'active' : ''}"
            onclick={() => toggleChip(slug)}
          >
            {slug}
          </button>
        {/each}
      </div>
      <p class="help">
        {#if allScopes}
          This key will work against every dataset in the catalog.
        {:else}
          This key will only work against the {selected.size} selected dataset{selected.size === 1 ? '' : 's'}.
        {/if}
      </p>
    </div>

    {#if allScopes}
      <input type="hidden" name="scopes" value="*" />
    {:else}
      {#each [...selected] as slug}
        <input type="hidden" name="scopes" value={slug} />
      {/each}
    {/if}

    <div class="row mt-16">
      <button type="submit" class="btn btn-accent">Create key</button>
      <button type="button" class="btn btn-ghost" onclick={() => (showCreate = false)}>
        Cancel
      </button>
    </div>
  </form>
{/if}

{#if data.keys.length === 0}
  <div class="empty-state">
    <p class="empty-title">No keys yet.</p>
    <p>Click "+ New key" to create your first one.</p>
  </div>
{:else}
  {#each data.keys as key (key.id)}
    <div class="card">
      <div class="key-row">
        <div class="key-row-main">
          <div class="key-name">
            {key.name}
            {#if key.active}
              <span class="badge badge-success">Active</span>
            {:else}
              <span class="badge badge-muted">Revoked</span>
            {/if}
          </div>
          <div class="key-mask">{maskKey(key.key_prefix)}</div>
          <div class="scope-summary">
            scope: <code>{scopeSummary(key.scopes)}</code>
          </div>
          <div class="key-meta">
            <span><span class="meta-label">created</span> {fmtDate(key.created_at)}</span>
            {#if key.last_used_at}
              <span><span class="meta-label">last used</span> {fmtDate(key.last_used_at)}</span>
            {:else}
              <span><span class="meta-label">last used</span> never</span>
            {/if}
          </div>
        </div>

        {#if key.active}
          <form method="POST" action="?/revoke" use:enhance>
            <input type="hidden" name="id" value={key.id} />
            <button class="btn btn-sm btn-danger" type="submit">Revoke</button>
          </form>
        {/if}
      </div>
    </div>
  {/each}
{/if}