mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-03-13 15:40:57 -07:00
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
43 lines
989 B
TypeScript
43 lines
989 B
TypeScript
/**
|
|
* @license
|
|
* Copyright 2026 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
import { useState, useCallback, useRef, useEffect } from 'react';
|
|
|
|
/**
|
|
* A hook to manage a state value that automatically resets to null after a duration.
|
|
* Useful for transient UI messages, hints, or warnings.
|
|
*/
|
|
export function useTimedMessage<T>(durationMs: number) {
|
|
const [message, setMessage] = useState<T | null>(null);
|
|
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
const showMessage = useCallback(
|
|
(msg: T | null) => {
|
|
setMessage(msg);
|
|
if (timeoutRef.current) {
|
|
clearTimeout(timeoutRef.current);
|
|
}
|
|
if (msg !== null) {
|
|
timeoutRef.current = setTimeout(() => {
|
|
setMessage(null);
|
|
}, durationMs);
|
|
}
|
|
},
|
|
[durationMs],
|
|
);
|
|
|
|
useEffect(
|
|
() => () => {
|
|
if (timeoutRef.current) {
|
|
clearTimeout(timeoutRef.current);
|
|
}
|
|
},
|
|
[],
|
|
);
|
|
|
|
return [message, showMessage] as const;
|
|
}
|