import { useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { PageHeader } from "@/components/ui/page-header";
import { useToast } from "@/hooks/use-toast";
import Sidebar from "@/components/layout/Sidebar";
import { Mic, MicOff, Volume2, Settings, PlayCircle } from "lucide-react";
import { useVoice } from "@/hooks/useVoice";
export default function VoicePage() {
const { toast } = useToast();
const [isListening, setIsListening] = useState(false);
const { isSupported, startListening, stopListening } = useVoice();
const { data: commands = [], isLoading } = useQuery({
queryKey: ["/api/voice/commands"],
select: (data: any) => Array.isArray(data) ? data : []
});
useQuery({
queryKey: ["/api/voice/status"],
select: (data: any) => data || { enabled: true }
});
const handleToggleListening = () => {
if (isListening) {
stopListening();
setIsListening(false);
toast({
title: "Voice recognition stopped",
description: "Voice commands are no longer being listened for.",
});
} else {
startListening();
setIsListening(true);
toast({
title: "Voice recognition started",
description: "Say a command to interact with the application.",
});
}
};
const testTTS = () => {
if ('speechSynthesis' in window) {
const utterance = new SpeechSynthesisUtterance("Voice assistant is working correctly!");
speechSynthesis.speak(utterance);
} else {
toast({
title: "Text-to-speech not supported",
description: "Your browser does not support text-to-speech.",
variant: "destructive",
});
}
};
return (
{/* Voice Control */}
Voice Control
Start or stop voice recognition
Voice Recognition
{isSupported ? "Supported" : "Not Supported"}
Status
{isListening ? "Listening" : "Stopped"}
{/* Text-to-Speech */}
Text-to-Speech
Test and configure speech output
TTS Support
{'speechSynthesis' in window ? "Available" : "Not Available"}
{/* Available Commands */}
Available Voice Commands
These are the voice commands you can use
Task Management
- "Create task [task name]"
- "Mark task [task name] complete"
- "Show my tasks"
- "What are my pending tasks?"
Financial Commands
- "Add expense [amount] for [description]"
- "Add income [amount] from [source]"
- "Show my balance"
- "What did I spend this month?"
Navigation
- "Go to dashboard"
- "Open tasks"
- "Show finances"
- "Open settings"
AI Assistant
- "Tell me a joke"
- "Give me productivity tips"
- "Analyze my spending"
- "What should I focus on today?"
{/* Recent Voice Commands */}
{!isLoading && Array.isArray(commands) && commands.length > 0 && (
Recent Commands
Your recent voice command history
{(commands as any[]).slice(0, 10).map((command: any) => (
{command.transcription}
{new Date(command.createdAt).toLocaleString()}
{command.processed ? "Processed" : "Pending"}
))}
)}
{/* Setup Instructions */}
Setup Instructions
Getting Started
- Make sure your browser supports voice recognition (Chrome, Edge recommended)
- Allow microphone access when prompted
- Click "Start Listening" to begin voice recognition
- Speak clearly and wait for the system to process your command
- Commands are processed automatically and actions are executed
Tips for Better Recognition
- Speak clearly and at a normal pace
- Use the exact command phrases listed above
- Ensure you're in a quiet environment
- Wait for the previous command to finish processing
);
}