Dough Git

clove/pkviewer

web/src/lib/components/manage/BadgeOffers.svelte

back to web/src/lib/components/manage

<!-- web/src/lib/components/manage/BadgeOffers.svelte -->
<script lang="ts">
  import type { BadgeState, OfferedBadge } from "@pkviewer/shared";
  import { respondToBadge } from "$lib/manage.remote.ts";
  import { mutate } from "$lib/mutate.ts";
  import BadgeSample from "../BadgeSample.svelte";
  import Section from "./Section.svelte";

  // Badges are opt-out, so turning one off is one obvious, reversible click with no confirmation
  let {
    systemId,
    badges,
  }: {
    systemId: string;
    badges: OfferedBadge[];
  } = $props();

  const EXPLAIN: Record<BadgeState, string> = {
    // Nothing is granted as pending any more; kept for rows from before opt-out
    pending: "Given by pkviewer. Not on your page yet.",
    accepted: "Showing on your system page.",
    hidden: "Hidden. Nobody can tell it was ever given.",
    declined: "Turned off. You can put it back at any time.",
    revoked: "",
  };

  let pending = $state(false);
  let error = $state<string | null>(null);

  async function act(badgeId: string, action: "accept" | "decline" | "hide" | "show") {
    pending = true;
    const outcome = await mutate(respondToBadge({ systemId, badgeId, action }));
    error = outcome.ok ? null : (outcome.error ?? "That did not work.");
    pending = false;
  }
</script>

{#if badges.length > 0}
  <Section
    title="Recognition"
    description="Given by pkviewer. Turn any of them off and it disappears from your page — nobody can tell it was ever there."
  >
    {#if error}
      <p class="mg-note" data-tone="error" role="status" aria-live="polite">{error}</p>
    {/if}

    <ul class="mg-list">
      {#each badges as badge (badge.id)}
        <li class="mg-item">
          <BadgeSample {badge} />
          <div class="grow">
            <div class="mg-item-meta muted">
              <span>{EXPLAIN[badge.state]}</span>
              {#if badge.note}<span>“{badge.note}”</span>{/if}
            </div>
          </div>

          {#if badge.state === "pending" || badge.state === "declined"}
            <button type="button" class="primary" disabled={pending} onclick={() => act(badge.id, "accept")}>
              Show it
            </button>
          {/if}
          {#if badge.state === "accepted"}
            <button type="button" class="quiet" disabled={pending} onclick={() => act(badge.id, "hide")}>
              Hide
            </button>
          {/if}
          {#if badge.state === "hidden"}
            <button type="button" class="quiet" disabled={pending} onclick={() => act(badge.id, "show")}>
              Show
            </button>
          {/if}
          {#if badge.state !== "declined"}
            <button type="button" class="quiet" disabled={pending} onclick={() => act(badge.id, "decline")}>
              Turn off
            </button>
          {/if}
        </li>
      {/each}
    </ul>
  </Section>
{/if}