File size: 3,402 Bytes
9332ccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import type React from 'react';
import { useCallback, useMemo, useState } from 'react';
import { Box, Text } from 'ink';
import { theme } from '../semantic-colors.js';
import { RadioButtonSelect } from '../components/shared/RadioButtonSelect.js';
import { useKeypress } from '../hooks/useKeypress.js';
import {
  openBrowserSecurely,
  shouldLaunchBrowser,
} from '@google/gemini-cli-core';
import { runExitCleanup } from '../../utils/cleanup.js';
import type { AccountSuspensionInfo } from '../contexts/UIStateContext.js';

interface BannedAccountDialogProps {
  accountSuspensionInfo: AccountSuspensionInfo;
  onExit: () => void;
  onChangeAuth: () => void;
}

export function BannedAccountDialog({
  accountSuspensionInfo,
  onExit,
  onChangeAuth,
}: BannedAccountDialogProps): React.JSX.Element {
  const [errorMessage, setErrorMessage] = useState<string | null>(null);

  const appealUrl = accountSuspensionInfo.appealUrl;
  const appealLinkText =
    accountSuspensionInfo.appealLinkText ?? 'Open the Google Form';

  const items = useMemo(() => {
    const menuItems = [];
    if (appealUrl) {
      menuItems.push({
        label: appealLinkText,
        value: 'open_form' as const,
        key: 'open_form',
      });
    }
    menuItems.push(
      {
        label: 'Change authentication',
        value: 'change_auth' as const,
        key: 'change_auth',
      },
      {
        label: 'Exit',
        value: 'exit' as const,
        key: 'exit',
      },
    );
    return menuItems;
  }, [appealUrl, appealLinkText]);

  useKeypress(
    (key) => {
      if (key.name === 'escape') {
        void handleExit();
        return true;
      }
      return false;
    },
    { isActive: true },
  );

  const handleExit = useCallback(async () => {
    await runExitCleanup();
    onExit();
  }, [onExit]);

  const handleSelect = useCallback(
    async (choice: string) => {
      if (choice === 'open_form' && appealUrl) {
        if (!shouldLaunchBrowser()) {
          setErrorMessage(`Please open this URL in a browser: ${appealUrl}`);
          return;
        }

        try {
          await openBrowserSecurely(appealUrl);
        } catch {
          setErrorMessage(`Failed to open browser. Please visit: ${appealUrl}`);
        }
      } else if (choice === 'change_auth') {
        onChangeAuth();
      } else {
        await handleExit();
      }
    },
    [handleExit, onChangeAuth, appealUrl],
  );

  return (
    <Box flexDirection="column" padding={1}>
      <Text bold color={theme.status.error}>
        Error: Account Suspended
      </Text>

      <Box marginTop={1}>
        <Text>{accountSuspensionInfo.message}</Text>
      </Box>

      {appealUrl && (
        <>
          <Box marginTop={1}>
            <Text>Appeal URL:</Text>
          </Box>
          <Box>
            <Text color={theme.text.link}>[{appealUrl}]</Text>
          </Box>
        </>
      )}

      {errorMessage && (
        <Box marginTop={1}>
          <Text color={theme.status.error}>{errorMessage}</Text>
        </Box>
      )}

      <Box marginTop={1}>
        <RadioButtonSelect
          items={items}
          onSelect={(choice) => void handleSelect(choice)}
        />
      </Box>

      <Box marginTop={1}>
        <Text dimColor>Escape to exit</Text>
      </Box>
    </Box>
  );
}