Dough Git

clove/pkviewer

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

back to web/src/lib/components/manage

<!-- web/src/lib/components/manage/SaveBar.svelte -->
<script lang="ts" module>
  export type SaveState = "clean" | "dirty" | "saving" | "saved" | "error";
</script>

<script lang="ts">
  import Icon from "../Icon.svelte";

  // One save state for every editable screen, named in words and announced live
  let {
    state,
    message = null,
    onSave,
    onDiscard,
    saveLabel = "Save changes",
    disabled = false,
  }: {
    state: SaveState;
    message?: string | null;
    onSave: () => void;
    onDiscard: () => void;
    saveLabel?: string;
    disabled?: boolean;
  } = $props();

  const LABELS: Record<SaveState, string> = {
    clean: "All changes saved",
    dirty: "Unsaved changes",
    saving: "Saving…",
    saved: "Changes saved",
    error: "Couldn't save changes. Try again.",
  };

  const TONES: Partial<Record<SaveState, string>> = {
    dirty: "dirty",
    saved: "ok",
    error: "error",
  };

  const busy = $derived(state === "saving");
  const canSave = $derived(!disabled && !busy && (state === "dirty" || state === "error"));
</script>

<div class="mg-savebar" data-state={state}>
  <span class="mg-status" data-tone={TONES[state]} role="status" aria-live="polite">
    {#if state === "saved"}<Icon name="check2" />{/if}
    {#if state === "error"}<Icon name="exclamation-triangle" />{/if}
    {#if state === "dirty"}<Icon name="pencil-fill" />{/if}
    {#if state === "saving"}<Icon name="arrow-clockwise" />{/if}
    {message ?? LABELS[state]}
  </span>
  <span class="spacer"></span>
  <button
    type="button"
    class="quiet"
    onclick={onDiscard}
    disabled={busy || (state !== "dirty" && state !== "error")}
  >
    Discard
  </button>
  <button type="button" class="primary" onclick={onSave} disabled={!canSave}>
    {busy ? "Saving…" : saveLabel}
  </button>
</div>