Spaces:
Sleeping
Sleeping
File size: 1,192 Bytes
2857363 |
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 |
import { useState, useCallback } from 'react';
import { analyzeSentiment } from '../services/api';
import type { SentimentResult } from '../types';
interface UseSentimentAnalysisReturn {
result: SentimentResult | null;
isLoading: boolean;
error: string | null;
analyze: (text: string) => Promise<void>;
reset: () => void;
}
export const useSentimentAnalysis = (): UseSentimentAnalysisReturn => {
const [result, setResult] = useState<SentimentResult | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const analyze = useCallback(async (text: string) => {
setIsLoading(true);
setError(null);
try {
const response = await analyzeSentiment(text);
setResult(response);
} catch (err) {
const errorMessage = err instanceof Error ? err.message : 'Analysis failed';
setError(errorMessage);
setResult(null);
} finally {
setIsLoading(false);
}
}, []);
const reset = useCallback(() => {
setResult(null);
setError(null);
setIsLoading(false);
}, []);
return {
result,
isLoading,
error,
analyze,
reset,
};
};
|