Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

5.5 KB
Raw Blame
import { TriangleAlert } from 'lucide-react';
import { observer } from 'mobx-react-lite';
import { useCallback, useEffect, useState } from 'react';
import { failureText } from '@renderer/lib/errors/describe-failure';
import { rpc } from '@renderer/lib/ipc';
import { type BaseModalProps } from '@renderer/lib/modal/modal-provider';
import { Button } from '@renderer/lib/ui/button';
import { ConfirmButton } from '@renderer/lib/ui/confirm-button';
import {
  DialogContentArea,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from '@renderer/lib/ui/dialog';
import { Field, FieldGroup, FieldLabel } from '@renderer/lib/ui/field';
import { Input } from '@renderer/lib/ui/input';
import type {
  BridgeInstallState,
  DeleteBridgeResult,
} from '@shared/core/switch-servers/switch-servers';
import { disconnectMessagingAppParagraphs } from './disconnect-messaging-app-copy';

type DisconnectMessagingAppModalArgs = {
  workspaceId: string;
  bridgeId: string;
  bridgeDisplayName: string;
  bridgeType: string;
};

type Props = BaseModalProps<void> & DisconnectMessagingAppModalArgs;

/**
 * Confirm disconnecting a messaging app from a workspace (CHOO-2137).
 *
 * "Disconnect" undersells it: the server deletes every Switch room on the
 * bridge before removing the bridge itself, so this is the most destructive
 * thing on the server page after deleting the server. It is guarded the same
 * way — type the name — because an accidental click here costs rooms, not a
 * setting.
 *
 * No room count is shown. The renderer's room list is what the signed-in user
 * is allowed to see, which is not necessarily every room on the bridge, and a
 * number that undercounts on a dialog like this is worse than no number.
 */
export const DisconnectMessagingAppModal = observer(function DisconnectMessagingAppModal({
  bridgeDisplayName,
  bridgeId,
  workspaceId,
  bridgeType,
  onSuccess,
  onClose,
}: Props) {
  // Whether the bridge is backed by an install decides what disconnecting it
  // does to its rooms; asked as the dialog opens, and treated as the more
  // destructive case until the answer is in.
  const [installState, setInstallState] = useState<BridgeInstallState | null>(null);
  useEffect(() => {
    let current = true;
    void rpc.workspaces
      .bridgeInstallState({ workspaceId, bridgeId })
      .then((state) => {
        if (current) setInstallState(state);
      })
      .catch(() => {
        if (current) setInstallState('unknown');
      });
    return () => {
      current = false;
    };
  }, [workspaceId, bridgeId]);
  const paragraphs = disconnectMessagingAppParagraphs({
    bridgeDisplayName,
    bridgeType,
    installState,
  });
  const [confirmText, setConfirmText] = useState('');
  const [isDeleting, setIsDeleting] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const typeConfirmed = confirmText.trim() === bridgeDisplayName.trim();

  const handleDelete = useCallback(async () => {
    if (!typeConfirmed) return;
    setIsDeleting(true);
    setError(null);
    try {
      const result = await rpc.workspaces.deleteBridge({ workspaceId, bridgeId });
      if (result.kind !== 'deleted') {
        setError(messageFor(result));
        setIsDeleting(false);
        return;
      }
      onSuccess();
    } catch (cause) {
      setError(failureText(cause, 'Could not disconnect the messaging app.'));
      setIsDeleting(false);
    }
  }, [typeConfirmed, workspaceId, bridgeId, onSuccess]);

  return (
    <>
      <DialogHeader showCloseButton={false}>
        <div className="flex items-center gap-2">
          <TriangleAlert className="size-4 text-red-500" />
          <DialogTitle>Disconnect “{bridgeDisplayName}”?</DialogTitle>
        </div>
      </DialogHeader>
      <DialogContentArea className="space-y-3 pt-0">
        {paragraphs.map((p) => (
          <p key={p} className="text-sm text-foreground-muted">
            {p}
          </p>
        ))}

        <FieldGroup>
          <Field>
            <FieldLabel>
              Type <span className="font-medium text-foreground">{bridgeDisplayName}</span> to
              confirm
            </FieldLabel>
            <Input
              value={confirmText}
              onChange={(e) => {
                setConfirmText(e.target.value);
                setError(null);
              }}
              onKeyDown={(e) => {
                if (e.key === 'Enter' && typeConfirmed) void handleDelete();
              }}
              autoFocus
            />
          </Field>
        </FieldGroup>

        {error && <p className="text-xs text-destructive">{error}</p>}
      </DialogContentArea>
      <DialogFooter>
        <Button variant="outline" onClick={onClose} disabled={isDeleting}>
          Cancel
        </Button>
        <ConfirmButton
          variant="destructive"
          onClick={() => void handleDelete()}
          disabled={!typeConfirmed || isDeleting}
        >
          {isDeleting ? 'Disconnecting…' : 'Disconnect app'}
        </ConfirmButton>
      </DialogFooter>
    </>
  );
});

/** Turn a refused disconnect into something the user can act on. */
function messageFor(result: Exclude<DeleteBridgeResult, { kind: 'deleted' }>): string {
  switch (result.kind) {
    case 'unauthenticated':
      return 'Your session for this server expired. Sign in again, then retry.';
    case 'forbidden':
      return 'Disconnecting a messaging app requires an admin of this workspace.';
    case 'not-found':
      return 'That messaging app is no longer connected to this server.';
    case 'error':
      return result.message;
  }
}