Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

836 B
Raw Blame
import { useEffect, useRef, useState } from 'react';
import { copyText } from '../utils/clipboard';

/**
 * Copy text and show "Copied" for two seconds, the timer restarting on every
 * copy. The timer is cleared on unmount, so a dialog closed right after a copy
 * sets no state on a component that is gone.
 */
export function useCopied(ms = 2000) {
  const [copied, setCopied] = useState(false);
  const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
  useEffect(
    () => () => {
      if (timer.current) clearTimeout(timer.current);
    },
    []
  );
  const copy = async (text: string | null) => {
    if (!text || !(await copyText(text))) return;
    setCopied(true);
    if (timer.current) clearTimeout(timer.current);
    timer.current = setTimeout(() => setCopied(false), ms);
  };
  return { copied, copy };
}