Files
shoptime/com.twin.app.shoptime/src/hooks/useWebSpeech.js
optrader d90385ec7d [251016] feat: WebSpeech API
🕐 커밋 시간: 2025. 10. 16. 17:04:05

📊 변경 통계:
  • 총 파일: 13개
  • 추가: +558줄
  • 삭제: -91줄

📁 추가된 파일:
  + com.twin.app.shoptime/src/actions/webSpeechActions.js
  + com.twin.app.shoptime/src/hooks/useWebSpeech.js
  + com.twin.app.shoptime/src/services/webSpeech/WebSpeechService.js
  + com.twin.app.shoptime/src/views/SearchPanel/VoiceInputOverlay/modes/VoiceResponse.jsx
  + com.twin.app.shoptime/src/views/SearchPanel/VoiceInputOverlay/modes/VoiceResponse.module.less

📝 수정된 파일:
  ~ com.twin.app.shoptime/src/actions/actionTypes.js
  ~ com.twin.app.shoptime/src/actions/voiceActions.js
  ~ com.twin.app.shoptime/src/reducers/voiceReducer.js
  ~ com.twin.app.shoptime/src/views/SearchPanel/SearchPanel.new.jsx
  ~ com.twin.app.shoptime/src/views/SearchPanel/VoiceInputOverlay/VoiceInputOverlay.jsx
  ~ com.twin.app.shoptime/src/views/VoicePanel/VoicePanel.jsx
  ~ com.twin.app.shoptime/src/views/VoicePanel/VoicePanel.module.less
  ~ com.twin.app.shoptime/web-speech.md

🔧 함수 변경 내용:
  📄 com.twin.app.shoptime/src/actions/voiceActions.js (javascript):
    🔄 Modified: addLog(), handleScrollIntent()
  📄 com.twin.app.shoptime/src/views/SearchPanel/VoiceInputOverlay/VoiceInputOverlay.jsx (javascript):
     Added: Spottable()
  📄 com.twin.app.shoptime/web-speech.md (md파일):
     Added: Framework()
    🔄 Modified: onresult()
     Deleted: Framework()
  📄 com.twin.app.shoptime/src/services/webSpeech/WebSpeechService.js (javascript):
     Added: WebSpeechService()
  📄 com.twin.app.shoptime/src/views/SearchPanel/VoiceInputOverlay/modes/VoiceResponse.jsx (javascript):
     Added: handleTalkAgainClick()
  📄 com.twin.app.shoptime/src/views/SearchPanel/VoiceInputOverlay/modes/VoiceResponse.module.less (unknown):
     Added: translateY()

🔧 주요 변경 내용:
  • 타입 시스템 안정성 강화
  • 핵심 비즈니스 로직 개선
  • 개발 문서 및 가이드 개선
  • API 서비스 레이어 개선
2025-10-16 17:04:08 +09:00

77 lines
2.1 KiB
JavaScript

// src/hooks/useWebSpeech.js
import { useEffect, useCallback } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import {
initializeWebSpeech,
startWebSpeech,
stopWebSpeech,
cleanupWebSpeech,
} from '../actions/webSpeechActions';
/**
* Web Speech API Hook
* - SearchPanel에서 사용하는 음성 입력 Hook
* - VoiceInputOverlay와 통합
*
* @param {boolean} isActive - Hook 활성화 여부 (예: SearchPanel이 foreground인지)
* @param {function} onSTTText - STT 텍스트 수신 콜백
* @param {Object} config - Web Speech 설정
*/
export const useWebSpeech = (isActive, onSTTText, config = {}) => {
const dispatch = useDispatch();
const { lastSTTText, sttTimestamp, webSpeech } = useSelector((state) => state.voice);
// Web Speech 초기화
useEffect(() => {
if (isActive) {
console.log('[useWebSpeech] Initializing Web Speech API');
dispatch(
initializeWebSpeech({
lang: config.lang || 'ko-KR',
continuous: config.continuous || false,
interimResults: config.interimResults !== false,
})
);
}
// Cleanup on unmount only
return () => {
if (isActive) {
console.log('[useWebSpeech] Cleaning up Web Speech API (unmount)');
dispatch(cleanupWebSpeech());
}
};
}, [isActive, dispatch, config.lang, config.continuous, config.interimResults]);
// STT 텍스트 수신 처리
useEffect(() => {
if (lastSTTText && sttTimestamp) {
console.log('[useWebSpeech] STT text received:', lastSTTText);
if (onSTTText) {
onSTTText(lastSTTText);
}
}
}, [lastSTTText, sttTimestamp, onSTTText]);
// 음성 인식 시작/중지 함수 반환
const startListening = useCallback(() => {
dispatch(startWebSpeech());
}, [dispatch]);
const stopListening = useCallback(() => {
dispatch(stopWebSpeech());
}, [dispatch]);
return {
isInitialized: webSpeech.isInitialized,
isListening: webSpeech.isListening,
interimText: webSpeech.interimText,
error: webSpeech.error,
startListening,
stopListening,
};
};
export default useWebSpeech;