search_vid
Version:
CLI tool for searching subtitles and playing videos at specific timestamps
68 lines (67 loc) • 2.41 kB
JavaScript
import React, { useState, useEffect } from 'react';
import { Box, Text } from 'ink';
import { searchSubtitles } from './utils/search.js';
import { SearchResults } from './components/SearchResults.js';
import { playVideo } from './utils/player.js';
export default function App({ query, options }) {
const [results, setResults] = useState([]);
const [isSearching, setIsSearching] = useState(true);
const [error, setError] = useState(null);
const [selectedMatchIndex] = useState(0);
const [currentQuery, setCurrentQuery] = useState(query);
const performSearch = async (searchQuery, exactMatch = false) => {
setIsSearching(true);
setError(null);
setCurrentQuery(searchQuery);
try {
const searchResults = await searchSubtitles({
query: searchQuery,
exactMatch,
...options,
});
setResults(searchResults);
}
catch (err) {
setError(err.message);
}
finally {
setIsSearching(false);
}
};
useEffect(() => {
void performSearch(query);
}, [query]);
if (error) {
return (React.createElement(Box, null,
React.createElement(Text, { color: "red" },
"Error: ",
error)));
}
if (isSearching) {
return (React.createElement(Box, null,
React.createElement(Text, null,
"Searching for \"",
currentQuery,
"\"...")));
}
return (React.createElement(SearchResults, { results: results, query: currentQuery, options: {
pagination: true,
pageSize: options.pageSize || 10,
treeView: options.treeView || false,
exactMatch: options.exactMatch || true,
}, onSelect: result => {
if (result.videoPath) {
const selectedMatch = result.matches[selectedMatchIndex];
if (selectedMatch) {
playVideo(result.videoPath, selectedMatch.subtitle.startTimeMs);
}
}
else {
console.error('No video file found for this subtitle');
}
}, onNewSearch: (newQuery, exactMatch) => {
void performSearch(newQuery, exactMatch);
}, onQuit: () => {
process.exit(0);
} }));
}