Skip to content

Commit be7d96a

Browse files
committed
refactor: improve character count query handling and add type annotations in SubtitleTranslator and VideoExtractor
1 parent 31cae28 commit be7d96a

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/components/ApiUsage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function ApiUsage() {
77
queryKey: ['character-count'],
88
queryFn: api.getCharacterCount,
99
refetchInterval: false, // Disable auto-refetch
10-
staleTime: Infinity, // Prevent automatic background updates
10+
staleTime: 0, // Prevent automatic background updates
1111
});
1212

1313
if (!data) return null;

src/components/SubtitleTranslator.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FileSelect } from './FileSelect';
22
import { LanguageSelect } from './LanguageSelect';
3-
import { useQuery, useMutation } from '@tanstack/react-query';
3+
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
44
import * as api from '../lib/api';
55
import { detectLanguageFromFilename } from '../lib/utils/subtitleLanguage';
66
import { useState } from 'react';
@@ -36,6 +36,7 @@ export function SubtitleTranslator({
3636
onClearStatus,
3737
}: Props) {
3838
const [updatePlex, setUpdatePlex] = useState(true);
39+
const queryClient = useQueryClient();
3940

4041
const { data: subtitles } = useQuery({
4142
queryKey: ['subtitles', type, show, season],
@@ -79,8 +80,12 @@ export function SubtitleTranslator({
7980
throw error;
8081
}
8182
},
82-
onSuccess: (path) => {
83+
onSuccess: (path: string) => {
8384
onTranslationComplete(path);
85+
// Wait a bit for DeepL API to update usage stats
86+
setTimeout(() => {
87+
queryClient.invalidateQueries({ queryKey: ['character-count'] });
88+
}, 2000); // 2-second delay
8489
},
8590
});
8691

src/components/VideoExtractor.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FileSelect } from './FileSelect';
22
import { LanguageSelect } from './LanguageSelect';
3-
import { useQuery, useMutation } from '@tanstack/react-query';
3+
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
44
import * as api from '../lib/api';
55
import { normalizeLanguageCode } from '../lib/utils/languages';
66
import { useState } from 'react';
@@ -36,6 +36,7 @@ export function VideoExtractor({
3636
onClearStatus,
3737
}: Props) {
3838
const [updatePlex, setUpdatePlex] = useState(true);
39+
const queryClient = useQueryClient();
3940

4041
const { data: videos } = useQuery({
4142
queryKey: ['videos', type, show, season],
@@ -92,7 +93,7 @@ export function VideoExtractor({
9293
}
9394

9495
return translatedPath;
95-
} catch (error) {
96+
} catch (error: any) { // Added type annotation to error
9697
console.log('Extraction error:', error);
9798
if (error.response) {
9899
const originalMessage = error.response.data.message;
@@ -105,8 +106,12 @@ export function VideoExtractor({
105106
throw error;
106107
}
107108
},
108-
onSuccess: (path) => {
109+
onSuccess: (path: string) => { // Added type annotation to path
109110
onTranslationComplete(path);
111+
// Wait a bit for DeepL API to update usage stats
112+
setTimeout(() => {
113+
queryClient.invalidateQueries({ queryKey: ['character-count'] });
114+
}, 2000); // 2-second delay
110115
},
111116
});
112117

0 commit comments

Comments
 (0)