File size: 4,499 Bytes
7faea5c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
'use client';
import { useActionState, useEffect, useRef, useState, type FC } from 'react';
import { useFormStatus } from 'react-dom';
import { convertTextToSpeech, type TtsState } from '@/app/actions';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Textarea } from '@/components/ui/textarea';
import { useToast } from '@/hooks/use-toast';
import { Volume2, LoaderCircle, Copy, Mic } from 'lucide-react';
const WindowControls = () => (
<div className="flex space-x-1.5">
<div className="w-3 h-3 rounded-full bg-muted-foreground/50"></div>
<div className="w-3 h-3 rounded-full bg-muted-foreground/50"></div>
<div className="w-3 h-3 rounded-full bg-muted-foreground/50"></div>
</div>
);
interface TtsFormProps {
examplePhrases: string[];
}
function SubmitButton() {
const { pending } = useFormStatus();
return (
<Button type="submit" disabled={pending} className="w-full" size="lg">
{pending ? (
<>
<LoaderCircle className="animate-spin" />
Generating...
</>
) : (
<>
<Volume2 />
Generate Speech
</>
)}
</Button>
);
}
export const TtsForm: FC<TtsFormProps> = ({ examplePhrases }) => {
const initialState: TtsState = { success: false };
const [state, formAction] = useActionState(convertTextToSpeech, initialState);
const { toast } = useToast();
const formRef = useRef<HTMLFormElement>(null);
const [text, setText] = useState('');
useEffect(() => {
if (state.error) {
toast({
variant: 'destructive',
title: 'Oops! Something went wrong.',
description: state.error,
});
}
}, [state, toast]);
const handleExampleClick = (phrase: string) => {
setText(phrase);
};
const handleCopyUrl = (url: string) => {
navigator.clipboard.writeText(url).then(() => {
toast({
title: 'Copied to clipboard!',
description: 'Audio URL is ready to be shared.',
});
});
};
return (
<div className="space-y-4 md:space-y-6">
<Card>
<CardHeader>
<CardTitle>Text Input</CardTitle>
<WindowControls />
</CardHeader>
<CardContent>
<form ref={formRef} action={formAction} className="space-y-4 pt-4">
<Textarea
name="text"
placeholder="इहाँ अपन छत्तीसगढ़ी सब्द लिखव..."
className="min-h-[100px] sm:min-h-[140px] resize-none"
value={text}
onChange={(e) => setText(e.target.value)}
required
/>
<SubmitButton />
</form>
</CardContent>
</Card>
{state.success && state.audioUrl && (
<Card>
<CardHeader>
<CardTitle>Audio Output</CardTitle>
<WindowControls />
</CardHeader>
<CardContent className="space-y-4 pt-4">
<audio key={state.key} controls src={state.audioUrl} className="w-full">
Your browser does not support the audio element.
</audio>
<Button
variant="outline"
className="w-full"
onClick={() => handleCopyUrl(state.audioUrl!)}
>
<Copy />
Copy Audio URL
</Button>
</CardContent>
</Card>
)}
{examplePhrases.length > 0 && (
<Card>
<CardHeader>
<CardTitle>Examples</CardTitle>
<WindowControls />
</CardHeader>
<CardContent className="pt-4">
<div className="flex flex-wrap gap-2">
{examplePhrases.map((phrase, index) => (
<Button
key={index}
variant="secondary"
size="sm"
className="h-auto items-start text-left font-body font-normal whitespace-normal text-xs sm:text-sm"
onClick={() => handleExampleClick(phrase)}>
<Mic className="mr-1 mt-0.5 h-4 w-4 shrink-0" />
<span>{phrase}</span>
</Button>
))}
</div>
</CardContent>
</Card>
)}
</div>
);
};
|