1*cf059199SKeith Kurakimport { mergeClasses } from '@expo/styleguide';
2*cf059199SKeith Kurakimport { Command } from 'cmdk';
3*cf059199SKeith Kurakimport { PropsWithChildren, useState } from 'react';
4*cf059199SKeith Kurak
5*cf059199SKeith Kurakimport { openLink } from '../utils';
6*cf059199SKeith Kurak
7*cf059199SKeith Kurakimport { Tag } from '~/ui/components/Tag';
8*cf059199SKeith Kurak
9*cf059199SKeith Kuraktype Props = PropsWithChildren<{
10*cf059199SKeith Kurak  url: string;
11*cf059199SKeith Kurak  onSelect?: () => void;
12*cf059199SKeith Kurak  isExternalLink?: boolean;
13*cf059199SKeith Kurak  isNested?: boolean;
14*cf059199SKeith Kurak  // Props forwarded to Command.Item
15*cf059199SKeith Kurak  className?: string;
16*cf059199SKeith Kurak  value?: string;
17*cf059199SKeith Kurak}>;
18*cf059199SKeith Kurak
19*cf059199SKeith Kurak/**
20*cf059199SKeith Kurak * Wrapper for Command.Item that adds copy link on right/ middle click + visual copy indicator.
21*cf059199SKeith Kurak */
22*cf059199SKeith Kurakexport const CommandItemBase = ({
23*cf059199SKeith Kurak  children,
24*cf059199SKeith Kurak  url,
25*cf059199SKeith Kurak  isExternalLink,
26*cf059199SKeith Kurak  isNested,
27*cf059199SKeith Kurak  onSelect,
28*cf059199SKeith Kurak  className,
29*cf059199SKeith Kurak  value,
30*cf059199SKeith Kurak}: Props) => {
31*cf059199SKeith Kurak  const [copyDone, setCopyDone] = useState(false);
32*cf059199SKeith Kurak
33*cf059199SKeith Kurak  const copyUrl = () => {
34*cf059199SKeith Kurak    navigator.clipboard?.writeText(url);
35*cf059199SKeith Kurak    setCopyDone(true);
36*cf059199SKeith Kurak    setTimeout(() => setCopyDone(false), 1500);
37*cf059199SKeith Kurak  };
38*cf059199SKeith Kurak
39*cf059199SKeith Kurak  return (
40*cf059199SKeith Kurak    <Command.Item
41*cf059199SKeith Kurak      className={mergeClasses('relative', className)}
42*cf059199SKeith Kurak      value={value}
43*cf059199SKeith Kurak      data-nested={isNested ? true : undefined}
44*cf059199SKeith Kurak      onMouseUp={event => {
45*cf059199SKeith Kurak        // note(Keith): middle click (typical *nix copy shortcut)
46*cf059199SKeith Kurak        // right click (works with Mac trackpads)
47*cf059199SKeith Kurak        // onAuxClick is not supported in Safari
48*cf059199SKeith Kurak        if (event.button === 1 || event.button === 2) {
49*cf059199SKeith Kurak          copyUrl();
50*cf059199SKeith Kurak        }
51*cf059199SKeith Kurak      }}
52*cf059199SKeith Kurak      onSelect={() => {
53*cf059199SKeith Kurak        openLink(url, isExternalLink);
54*cf059199SKeith Kurak        onSelect && onSelect();
55*cf059199SKeith Kurak      }}
56*cf059199SKeith Kurak      onContextMenu={event => {
57*cf059199SKeith Kurak        event.preventDefault();
58*cf059199SKeith Kurak      }}>
59*cf059199SKeith Kurak      {children}
60*cf059199SKeith Kurak      {copyDone && (
61*cf059199SKeith Kurak        <Tag
62*cf059199SKeith Kurak          name="Copied!"
63*cf059199SKeith Kurak          className="absolute right-2.5 top-[calc(50%-13px)] !m-0 !border-secondary !bg-default"
64*cf059199SKeith Kurak        />
65*cf059199SKeith Kurak      )}
66*cf059199SKeith Kurak    </Command.Item>
67*cf059199SKeith Kurak  );
68*cf059199SKeith Kurak};
69