Dough Git

clove/pkviewer

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

back to web/src/lib/components/manage

<!-- web/src/lib/components/manage/PublicUrl.svelte -->
<script lang="ts">
  import Icon from "../Icon.svelte";

  // Copying is never the only way: the URL is selectable text and a link too
  let {
    url,
    label = "Public address",
    secondary,
  }: {
    url: string;
    label?: string;
    secondary?: { url: string; note: string };
  } = $props();

  let copied = $state(false);
  let timer: ReturnType<typeof setTimeout> | null = null;

  $effect(() => () => {
    if (timer) clearTimeout(timer);
  });

  async function copy() {
    try {
      await navigator.clipboard.writeText(url);
    } catch {
      // A blocked clipboard is fine: the address is right there to select
      return;
    }
    copied = true;
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => (copied = false), 2200);
  }
</script>

<div class="mg-field">
  <span class="desc">{label}</span>
  <div style="display: flex; gap: 8px; align-items: center; flex-wrap: wrap">
    <code style="font-size: 13.5px; overflow-wrap: anywhere; flex: 1 1 16rem">{url}</code>
    <button type="button" onclick={copy}>
      <Icon name={copied ? "check2" : "clipboard"} />
      {copied ? "Copied" : "Copy"}
    </button>
    <a class="btn" href={url} target="_blank" rel="noopener">
      Open <Icon name="box-arrow-up-right" />
    </a>
  </div>
  <span role="status" aria-live="polite" class="visually-hidden">
    {copied ? "Address copied to clipboard" : ""}
  </span>
  {#if secondary}
    <span class="desc">
      {secondary.note}
      <a href={secondary.url} target="_blank" rel="noopener">
        <code>{secondary.url}</code>
      </a>
    </span>
  {/if}
</div>