File size: 2,147 Bytes
52efc7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { useState, useEffect } from 'react';
import { persistentState } from '../../utils/persistentState.js';
import crypto from 'node:crypto';
import chalk from 'chalk';
import { getAntigravityInstallInfo } from '../utils/antigravityUtils.js';

const DEFAULT_MAX_BANNER_SHOWN_COUNT = 5;

// Track banners incremented during this session to prevent multiple increments
// on React unmounts/remounts
const sessionIncrementedBanners = new Set<string>();

// For testing purposes
export function _clearSessionBannersForTest() {
  sessionIncrementedBanners.clear();
}

interface BannerData {
  defaultText: string;
  warningText: string;
}

export function useBanner(bannerData: BannerData) {
  const { defaultText, warningText } = bannerData;

  const [bannerCounts] = useState(
    () => persistentState.get('defaultBannerShownCount') || {},
  );

  const activeText = warningText ? warningText : defaultText;

  const hashedText = crypto
    .createHash('sha256')
    .update(activeText)
    .digest('hex');

  const currentBannerCount = bannerCounts[hashedText] || 0;

  const showBanner =
    activeText !== '' &&
    (currentBannerCount < DEFAULT_MAX_BANNER_SHOWN_COUNT ||
      activeText.includes('Antigravity'));

  const rawBannerText = showBanner ? activeText : '';
  let bannerText = rawBannerText.replace(/\\n/g, '\n');

  if (showBanner && activeText.includes('Antigravity')) {
    const info = getAntigravityInstallInfo();
    if (info) {
      bannerText += `\n \nTo install run "${chalk.bold(info.installCmd)}"`;
    }
  }

  useEffect(() => {
    if (showBanner && activeText) {
      if (!sessionIncrementedBanners.has(activeText)) {
        sessionIncrementedBanners.add(activeText);

        const allCounts = persistentState.get('defaultBannerShownCount') || {};
        const current = allCounts[hashedText] || 0;

        persistentState.set('defaultBannerShownCount', {
          ...allCounts,
          [hashedText]: current + 1,
        });
      }
    }
  }, [showBanner, activeText, hashedText]);

  return {
    bannerText,
  };
}