Dough Git

clove/pkviewer

web/src/lib/components/admin/CreditAdmin.svelte

back to web/src/lib/components/admin

<!-- web/src/lib/components/admin/CreditAdmin.svelte -->
<script lang="ts">
  import { deleteCredit, saveCredit } from "$lib/admin.remote.ts";
  import { mutate } from "$lib/mutate.ts";
  import type { ActionResult, AdminCredit, AdminSection } from "$lib/types.ts";
  import Icon from "../Icon.svelte";
  import PageHeader from "../manage/PageHeader.svelte";
  import Section from "../manage/Section.svelte";
  import SectionAdmin from "./SectionAdmin.svelte";

  // Sections are rows, so "Translators" is added here rather than deployed; order is explicit
  let {
    sections,
    credits,
  }: {
    sections: AdminSection[];
    credits: AdminCredit[];
  } = $props();

  type Draft = {
    id?: string;
    sectionId: string;
    name: string;
    detail: string;
    url: string;
    sortOrder: number;
    visible: boolean;
  };

  function empty(): Draft {
    return {
      sectionId: sections[0]?.id ?? "",
      name: "",
      detail: "",
      url: "",
      sortOrder: 0,
      visible: true,
    };
  }

  let pending = $state(false);
  let message = $state<{ tone: "ok" | "error"; text: string } | null>(null);
  let draft = $state<Draft>(empty());

  const editing = $derived(draft.id !== undefined);

  async function run(action: () => Promise<ActionResult>, okText: string) {
    pending = true;
    const outcome = await mutate(action());
    message = outcome.ok
      ? { tone: "ok", text: okText }
      : { tone: "error", text: outcome.error ?? "That did not work." };
    pending = false;
  }

  function submit() {
    const wasEditing = editing;
    return run(async () => {
      const outcome = await saveCredit({
        ...(draft.id ? { id: draft.id } : {}),
        sectionId: draft.sectionId,
        name: draft.name.trim(),
        detail: draft.detail.trim(),
        url: draft.url.trim(),
        sortOrder: draft.sortOrder,
        visible: draft.visible,
      });
      if (outcome.ok) draft = empty();
      return outcome;
    }, wasEditing ? "Entry updated." : "Entry added.");
  }

  function entriesFor(sectionId: string): AdminCredit[] {
    return credits
      .filter((credit) => credit.sectionId === sectionId)
      .sort((a, b) => a.sortOrder - b.sortOrder || a.name.localeCompare(b.name));
  }
</script>

<div class="mg-shell">
  <PageHeader
    title="Credits"
    description="Anyone can be credited. No pkviewer account and no PluralKit system required."
  />

  {#if message}
    <p class="mg-note" data-tone={message.tone} role="status" aria-live="polite">{message.text}</p>
  {/if}

  <Section title={editing ? "Edit entry" : "Add someone"}>
    <div class="mg-row">
      <div class="mg-field grow">
        <label for="credit-name">Name</label>
        <input
          id="credit-name"
          bind:value={draft.name}
          maxlength="80"
          placeholder="How they want to be known"
        />
      </div>

      <div class="mg-field">
        <label for="credit-section">Section</label>
        <select id="credit-section" bind:value={draft.sectionId}>
          {#each sections as section (section.id)}
            <option value={section.id}>{section.label}</option>
          {/each}
        </select>
      </div>
    </div>

    <div class="mg-field">
      <label for="credit-detail">What they did</label>
      <input
        id="credit-detail"
        bind:value={draft.detail}
        maxlength="200"
        placeholder="Reported the CSP bypass"
      />
    </div>

    <div class="mg-row">
      <div class="mg-field grow">
        <label for="credit-url">Link (optional)</label>
        <input id="credit-url" bind:value={draft.url} maxlength="500" placeholder="https://…" />
        <p class="mg-hint">Rendered as a link. pkviewer never visits it.</p>
      </div>

      <div class="mg-field">
        <label for="credit-order">Order</label>
        <input id="credit-order" type="number" bind:value={draft.sortOrder} />
      </div>
    </div>

    <label class="mg-check">
      <input type="checkbox" bind:checked={draft.visible} />
      Show on the public credits page
    </label>

    <div class="mg-actions">
      <button
        type="button"
        class="primary"
        disabled={pending || !draft.name.trim() || !draft.sectionId}
        onclick={submit}
      >
        {editing ? "Save entry" : "Add entry"}
      </button>
      {#if editing}
        <button type="button" class="quiet" onclick={() => (draft = empty())}>Cancel</button>
      {/if}
    </div>
  </Section>

  {#each sections as section (section.id)}
    {@const entries = entriesFor(section.id)}
    <Section title={section.label} description={section.description ?? undefined}>
      {#if entries.length === 0}
        <p class="muted">Nobody in this section yet.</p>
      {:else}
        <ul class="mg-list">
          {#each entries as credit (credit.id)}
            <li class="mg-item">
              <div class="grow">
                <strong>{credit.name}</strong>
                {#if !credit.visible}
                  <span class="muted"> <Icon name="eye-slash" /> hidden</span>
                {/if}
                {#if credit.detail}
                  <div class="mg-item-meta muted">{credit.detail}</div>
                {/if}
              </div>
              <button
                type="button"
                class="quiet"
                onclick={() =>
                  (draft = {
                    id: credit.id,
                    sectionId: credit.sectionId,
                    name: credit.name,
                    detail: credit.detail ?? "",
                    url: credit.url ?? "",
                    sortOrder: credit.sortOrder,
                    visible: credit.visible,
                  })}
              >
                Edit
              </button>
              <button
                type="button"
                class="quiet"
                disabled={pending}
                onclick={() => run(() => deleteCredit(credit.id), "Entry removed.")}
              >
                Remove
              </button>
            </li>
          {/each}
        </ul>
      {/if}
    </Section>
  {/each}

  <SectionAdmin {sections} {pending} {run} />
</div>