summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/hooks/useAppEffects.ts
blob: 16f862b0bc27ffc537d0d8af8245b23995727e02 (plain)
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
import { useEffect } from 'react';
import fs from 'fs';
import path from 'path';
import os from 'os';
import type { HistoryItem } from '../types.js';

const warningsFilePath = path.join(os.tmpdir(), 'gemini-code-cli-warnings.txt');

// Effect to handle startup warnings
export function useStartupWarnings(
  setStartupWarnings: React.Dispatch<React.SetStateAction<string[]>>,
) {
  useEffect(() => {
    try {
      if (fs.existsSync(warningsFilePath)) {
        const warningsContent = fs.readFileSync(warningsFilePath, 'utf-8');
        setStartupWarnings(
          warningsContent.split('\n').filter((line) => line.trim() !== ''),
        );
        try {
          fs.unlinkSync(warningsFilePath);
        } catch (unlinkErr: any) {
          setStartupWarnings((prev) => [
            ...prev,
            `Warning: Could not delete temporary warnings file.`,
          ]);
        }
      }
    } catch (err: any) {
      setStartupWarnings((prev) => [
        ...prev,
        `Error checking/reading warnings file: ${err.message}`,
      ]);
    }
  }, [setStartupWarnings]); // Include setStartupWarnings in dependency array
}

// Effect to handle initialization errors
export function useInitializationErrorEffect(
  initError: string | null,
  history: HistoryItem[],
  setHistory: React.Dispatch<React.SetStateAction<HistoryItem[]>>,
) {
  useEffect(() => {
    if (
      initError &&
      !history.some(
        (item) => item.type === 'error' && item.text?.includes(initError),
      )
    ) {
      setHistory((prev) => [
        ...prev,
        {
          id: Date.now(),
          type: 'error',
          text: `Initialization Error: ${initError}. Please check API key and configuration.`,
        } as HistoryItem,
      ]);
    }
  }, [initError, history, setHistory]); // Include setHistory in dependency array
}