Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

3.0 KB
Raw Blame
import { toast } from '@core/component/Toast/Toast';
import { fireAndForget } from '@core/util/fireAndForget';
import { getWebOrigin } from '@core/util/webOrigin';
import CheckIcon from '@icon/system/regular/check.svg';
import LinkIcon from '@icon/system/regular/link.svg';
import SpinnerIcon from '@icon/system/regular/spinner.svg';
import { useGetChannelJoinLinkMutation } from '@queries/channel/join-links';
import { Button } from '@ui';
import { createSignal, onCleanup, Show } from 'solid-js';

export function ChannelJoinLinkButton(props: {
  channelId: string;
  iconOnly?: boolean;
}) {
  const getJoinLinkMutation = useGetChannelJoinLinkMutation();
  const [joinCode, setJoinCode] = createSignal<string>();
  const [copied, setCopied] = createSignal(false);
  let copyResetTimeout: ReturnType<typeof setTimeout> | undefined;
  let joinCodeRequest: Promise<string | undefined> | undefined;

  onCleanup(() => clearTimeout(copyResetTimeout));

  const generateJoinCode = async (): Promise<string | undefined> => {
    try {
      const response = await getJoinLinkMutation.mutateAsync({
        channelId: props.channelId,
      });
      setJoinCode(response.join_code);
      return response.join_code;
    } catch {
      // The mutation displays the standard generation failure toast.
      return undefined;
    }
  };

  const getJoinCode = async (): Promise<string | undefined> => {
    const existingCode = joinCode();
    if (existingCode) return existingCode;
    if (joinCodeRequest) return await joinCodeRequest;

    joinCodeRequest = generateJoinCode();
    try {
      return await joinCodeRequest;
    } finally {
      joinCodeRequest = undefined;
    }
  };

  const buttonLabel = () => {
    if (getJoinLinkMutation.isPending) return 'Generating link';
    if (copied()) return 'Copied';
    return 'Copy invite link';
  };

  const copyJoinLink = async () => {
    if (getJoinLinkMutation.isPending) return;

    const code = await getJoinCode();
    if (!code) return;

    try {
      await navigator.clipboard.writeText(
        `${getWebOrigin()}/app/channel-invite?code=${code}`
      );
    } catch (error) {
      console.error('Failed to copy channel join link', error);
      toast.failure('Failed to copy channel join link');
      return;
    }

    setCopied(true);
    clearTimeout(copyResetTimeout);
    copyResetTimeout = setTimeout(() => setCopied(false), 2000);
  };

  return (
    <Button
      aria-label={buttonLabel()}
      tooltip={buttonLabel()}
      variant="secondary"
      size={props.iconOnly ? 'icon-md' : 'sm'}
      disabled={getJoinLinkMutation.isPending}
      onClick={() => fireAndForget(copyJoinLink(), 'channel-invite')}
    >
      <Show
        when={!getJoinLinkMutation.isPending}
        fallback={<SpinnerIcon class="size-4 animate-spin" />}
      >
        <Show when={copied()} fallback={<LinkIcon class="size-4" />}>
          <CheckIcon class="size-4" />
        </Show>
      </Show>
      <Show when={!props.iconOnly}>{buttonLabel()}</Show>
    </Button>
  );
}