Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 51 additions & 6 deletions packages/cli/src/ui/components/Notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,86 @@
*/

import { Box, Text, useIsScreenReaderEnabled } from 'ink';
import { useEffect, useState } from 'react';
import { useAppContext } from '../contexts/AppContext.js';
import { useUIState } from '../contexts/UIStateContext.js';
import { theme } from '../semantic-colors.js';
import { StreamingState } from '../types.js';
import { UpdateNotification } from './UpdateNotification.js';

import { GEMINI_DIR } from '@google/gemini-cli-core';
import { homedir } from 'node:os';
import { GEMINI_DIR, Storage } from '@google/gemini-cli-core';

import * as fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';

const settingsPath = path.join(homedir(), GEMINI_DIR, 'settings.json');
const settingsPath = path.join(os.homedir(), GEMINI_DIR, 'settings.json');

const screenReaderNudgeFilePath = path.join(
Storage.getGlobalTempDir(),
'seen_screen_reader_nudge.json',
);

export const Notifications = () => {
const { startupWarnings } = useAppContext();
const { initError, streamingState, updateInfo } = useUIState();

const isScreenReaderEnabled = useIsScreenReaderEnabled();
const showStartupWarnings = startupWarnings.length > 0;
const showInitError =
initError && streamingState !== StreamingState.Responding;

const [hasSeenScreenReaderNudge, setHasSeenScreenReaderNudge] = useState<
boolean | undefined
>(undefined);

useEffect(() => {
const checkScreenReaderNudge = async () => {
try {
await fs.access(screenReaderNudgeFilePath);
setHasSeenScreenReaderNudge(true);
} catch {
setHasSeenScreenReaderNudge(false);
}
};
checkScreenReaderNudge();
}, []);

const showScreenReaderNudge =
isScreenReaderEnabled && !hasSeenScreenReaderNudge;
Comment on lines +53 to +54

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current logic for showScreenReaderNudge can cause a UI flicker. On the initial render, hasSeenScreenReaderNudge is undefined, making !hasSeenScreenReaderNudge evaluate to true. If the screen reader is enabled, the nudge will be displayed momentarily. Then, the useEffect hook runs, checks for the file, and if it exists, sets hasSeenScreenReaderNudge to true. This triggers a re-render where showScreenReaderNudge becomes false, causing the nudge to disappear.

To prevent this flicker, you should only show the nudge when you are certain it should be displayed, i.e., after the check has completed and hasSeenScreenReaderNudge is explicitly false.

Suggested change
const showScreenReaderNudge =
isScreenReaderEnabled && !hasSeenScreenReaderNudge;
const showScreenReaderNudge =
isScreenReaderEnabled && hasSeenScreenReaderNudge === false;


useEffect(() => {
const writeScreenReaderNudgeFile = async () => {
if (showScreenReaderNudge) {
try {
await fs.mkdir(path.dirname(screenReaderNudgeFilePath), {
recursive: true,
});
await fs.writeFile(screenReaderNudgeFilePath, 'true');
} catch (error) {
console.error('Error storing screen reader nudge', error);
}
}
};
writeScreenReaderNudgeFile();
}, [showScreenReaderNudge]);

if (
!showStartupWarnings &&
!showInitError &&
!updateInfo &&
!isScreenReaderEnabled
!showScreenReaderNudge
) {
return null;
}

return (
<>
{isScreenReaderEnabled && (
{showScreenReaderNudge && (
<Text>
You are currently in screen reader-friendly view. To switch out, open{' '}
{settingsPath} and remove the entry for {'"screenReader"'}.
{settingsPath} and remove the entry for {'"screenReader"'}. This will
disappear on next run.
</Text>
)}
{updateInfo && <UpdateNotification message={updateInfo.message} />}
Expand Down
Loading