Dough Git

clove/pkviewer

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

back to web/src/lib/components/manage

<!-- web/src/lib/components/manage/CompositionEditor.svelte -->
<script lang="ts">
  import { COMPOSITION, type CompositionDef } from "@pkviewer/shared";
  import { saveSystemComposition } from "$lib/manage.remote.ts";
  import { mutate } from "$lib/mutate.ts";
  import { useUnsavedGuard } from "$lib/unsavedGuard.svelte.ts";
  import Icon from "../Icon.svelte";

  // What is on the page and how it is arranged; these never become CSS properties
  let {
    systemId,
    initialValues,
  }: {
    systemId: string;
    initialValues: Record<string, string>;
  } = $props();

  const LABELS: Record<string, string> = {
    "banner.display": "Show banner",
    "avatar.size": "Avatar size",
    "directory.columns": "Member columns",
    "directory.card": "Card style",
    "directory.sort": "Sort members by",
    "show.pronouns": "Show pronouns",
    "show.birthday": "Show birthdays",
  };

  const OPTION_LABELS: Record<string, string> = {
    auto: "Automatic",
    hidden: "Hidden",
    one: "One",
    two: "Two",
    three: "Three",
    compact: "Compact",
    detailed: "Detailed",
    pluralkit: "PluralKit order",
    name: "Name",
    small: "Small",
    medium: "Medium",
    large: "Large",
  };

  // svelte-ignore state_referenced_locally
  let values = $state<Record<string, string>>({ ...initialValues });
  // svelte-ignore state_referenced_locally
  let saved = $state<Record<string, string>>({ ...initialValues });
  let status = $state<"idle" | "saving" | "ok" | "error">("idle");
  let message = $state<string | null>(null);

  const dirty = $derived(JSON.stringify(values) !== JSON.stringify(saved));
  useUnsavedGuard(() => dirty);

  const groups = [
    {
      title: "Member directory",
      hint: "How members are listed on your system page.",
      defs: COMPOSITION.filter((def) => def.key.startsWith("directory.")),
    },
    {
      title: "Header",
      hint: "Banner and avatar on system and member pages.",
      defs: COMPOSITION.filter((def) => def.key === "banner.display" || def.key === "avatar.size"),
    },
    {
      title: "Details to show",
      hint: "Only information PluralKit already makes public can be shown. Turning something off here hides it on pkviewer; it does not change PluralKit.",
      defs: COMPOSITION.filter((def) => def.key.startsWith("show.")),
    },
  ];

  function fieldId(def: CompositionDef): string {
    return `comp-${def.key.replace(/\./g, "-")}`;
  }

  function options(def: CompositionDef): Array<{ value: string; label: string }> {
    if (def.type === "boolean") {
      return [
        { value: "true", label: "Shown" },
        { value: "false", label: "Hidden" },
      ];
    }
    return def.values.map((value) => ({ value, label: OPTION_LABELS[value] ?? value }));
  }

  async function save() {
    status = "saving";
    const outcome = await mutate(saveSystemComposition({ systemId, composition: $state.snapshot(values) }));
    if (outcome.ok) {
      saved = { ...values };
      status = "ok";
      message = "Saved.";
    } else {
      status = "error";
      message = outcome.error ?? "Could not save. Please try again.";
    }
  }
</script>

{#each groups as group (group.title)}
  <section class="mg-panel">
    <h2>{group.title}</h2>
    <p class="hint">{group.hint}</p>
    <div class="mg-grid">
      {#each group.defs as def (def.key)}
        {@const id = fieldId(def)}
        <div class="mg-field">
          <div class="mg-field-head">
            <label for={id}>{LABELS[def.key] ?? def.label}</label>
          </div>
          <select
            {id}
            value={values[def.key] ?? String(def.default)}
            aria-describedby="{id}-desc"
            onchange={(event) => (values[def.key] = event.currentTarget.value)}
          >
            {#each options(def) as option (option.value)}
              <option value={option.value}>{option.label}</option>
            {/each}
          </select>
          <span class="desc" id="{id}-desc">{def.help}</span>
        </div>
      {/each}
    </div>
  </section>
{/each}

<div class="mg-savebar">
  <span
    class="mg-status"
    data-tone={status === "ok" ? "ok" : status === "error" ? "error" : dirty ? "dirty" : undefined}
    role="status"
    aria-live="polite"
  >
    {#if status === "error"}<Icon name="exclamation-triangle" />{/if}
    {#if status === "ok"}<Icon name="check2" />{/if}
    {message ?? (dirty ? "Unsaved changes" : "No changes")}
  </span>
  <span class="spacer"></span>
  <button
    type="button"
    class="ghost"
    onclick={() => {
      values = { ...saved };
      status = "idle";
      message = null;
    }}
    disabled={!dirty}
  >
    Discard changes
  </button>
  <button type="button" class="primary" onclick={save} disabled={!dirty || status === "saving"}>
    {status === "saving" ? "Saving…" : "Save settings"}
  </button>
</div>