mirror of
https://github.com/QwenLM/qwen-code.git
synced 2026-05-05 23:42:03 +00:00
82 lines
2.5 KiB
TypeScript
82 lines
2.5 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2025 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
import { useState, useEffect, useRef, useMemo } from 'react';
|
|
import { t, ta } from '../../i18n/index.js';
|
|
|
|
export const WITTY_LOADING_PHRASES: string[] = ["I'm Feeling Lucky"];
|
|
|
|
export const PHRASE_CHANGE_INTERVAL_MS = 15000;
|
|
|
|
/**
|
|
* Custom hook to manage cycling through loading phrases.
|
|
* @param isActive Whether the phrase cycling should be active.
|
|
* @param isWaiting Whether to show a specific waiting phrase.
|
|
* @returns The current loading phrase.
|
|
*/
|
|
export const usePhraseCycler = (
|
|
isActive: boolean,
|
|
isWaiting: boolean,
|
|
customPhrases?: string[],
|
|
) => {
|
|
// Get phrases from translations if available
|
|
const loadingPhrases = useMemo(() => {
|
|
if (customPhrases && customPhrases.length > 0) {
|
|
return customPhrases;
|
|
}
|
|
const translatedPhrases = ta('WITTY_LOADING_PHRASES');
|
|
return translatedPhrases.length > 0
|
|
? translatedPhrases
|
|
: WITTY_LOADING_PHRASES;
|
|
}, [customPhrases]);
|
|
|
|
const [currentLoadingPhrase, setCurrentLoadingPhrase] = useState(
|
|
loadingPhrases[0],
|
|
);
|
|
const phraseIntervalRef = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (isWaiting) {
|
|
setCurrentLoadingPhrase(t('Waiting for user confirmation...'));
|
|
if (phraseIntervalRef.current) {
|
|
clearInterval(phraseIntervalRef.current);
|
|
phraseIntervalRef.current = null;
|
|
}
|
|
} else if (isActive) {
|
|
if (phraseIntervalRef.current) {
|
|
clearInterval(phraseIntervalRef.current);
|
|
}
|
|
// Select an initial random phrase
|
|
const initialRandomIndex = Math.floor(
|
|
Math.random() * loadingPhrases.length,
|
|
);
|
|
setCurrentLoadingPhrase(loadingPhrases[initialRandomIndex]);
|
|
|
|
phraseIntervalRef.current = setInterval(() => {
|
|
// Select a new random phrase
|
|
const randomIndex = Math.floor(Math.random() * loadingPhrases.length);
|
|
setCurrentLoadingPhrase(loadingPhrases[randomIndex]);
|
|
}, PHRASE_CHANGE_INTERVAL_MS);
|
|
} else {
|
|
// Idle or other states, clear the phrase interval
|
|
// and reset to the first phrase for next active state.
|
|
if (phraseIntervalRef.current) {
|
|
clearInterval(phraseIntervalRef.current);
|
|
phraseIntervalRef.current = null;
|
|
}
|
|
setCurrentLoadingPhrase(loadingPhrases[0]);
|
|
}
|
|
|
|
return () => {
|
|
if (phraseIntervalRef.current) {
|
|
clearInterval(phraseIntervalRef.current);
|
|
phraseIntervalRef.current = null;
|
|
}
|
|
};
|
|
}, [isActive, isWaiting, loadingPhrases]);
|
|
|
|
return currentLoadingPhrase;
|
|
};
|