Skip to content

Commit

Permalink
Translation Module Usable
Browse files Browse the repository at this point in the history
  • Loading branch information
ENDlezZenith committed Sep 17, 2024
1 parent 814714d commit 164138e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 38 deletions.
46 changes: 25 additions & 21 deletions src/renderer/features/lyrics/lyrics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const Lyrics = () => {
const currentSong = useCurrentSong();
const lyricsSettings = useLyricsSettings();
const [index, setIndex] = useState(0);
const [translatedLyrics, setTranslatedLyrics] = useState<string | null>(null);

const { data, isInitialLoading } = useSongLyricsBySong(
{
Expand All @@ -99,6 +100,19 @@ export const Lyrics = () => {

const [override, setOverride] = useState<LyricsOverride | undefined>(undefined);

const [lyrics, synced] = useMemo(() => {
if (Array.isArray(data)) {
if (data.length > 0) {
const selectedLyric = data[Math.min(index, data.length)];
return [selectedLyric, selectedLyric.synced];
}
} else if (data?.lyrics) {
return [data, Array.isArray(data.lyrics)];
}

return [undefined, false];
}, [data, index]);

const handleOnSearchOverride = useCallback((params: LyricsOverride) => {
setOverride(params);
}, []);
Expand Down Expand Up @@ -130,8 +144,10 @@ export const Lyrics = () => {

const handleOnTranslationLyric = useCallback(async () => {
const { apiKey, targetLanguage } = lyricsSettings;
const originalLyrics = data.lyrics;
console.log('Original Lyrics:', originalLyrics);
if (!lyrics) return;
const originalLyrics = Array.isArray(lyrics.lyrics)
? lyrics.lyrics.map(([, line]) => line).join('\n')
: lyrics.lyrics;
const response = await axios({
baseURL: 'https://api.cognitive.microsofttranslator.com',
data: [
Expand All @@ -154,12 +170,9 @@ export const Lyrics = () => {
url: '/translate',
});
const translatedText = response.data[0].translations[0].text;
setTranslatedLyrics(translatedText);
console.log('Translated Lyrics:', translatedText);
queryClient.setQueryData(
queryKeys.songs.lyrics(currentSong?.serverId, { songId: currentSong?.id }),
translatedText,
);
}, [data, currentSong?.id, currentSong?.serverId, lyricsSettings]);
}, [lyrics, lyricsSettings]);

const { isInitialLoading: isOverrideLoading } = useSongLyricsByRemoteId({
options: {
Expand Down Expand Up @@ -188,19 +201,6 @@ export const Lyrics = () => {
};
}, []);

const [lyrics, synced] = useMemo(() => {
if (Array.isArray(data)) {
if (data.length > 0) {
const selectedLyric = data[Math.min(index, data.length)];
return [selectedLyric, selectedLyric.synced];
}
} else if (data?.lyrics) {
return [data, Array.isArray(data.lyrics)];
}

return [undefined, false];
}, [data, index]);

const languages = useMemo(() => {
if (Array.isArray(data)) {
return data.map((lyric, idx) => ({ label: lyric.lang, value: idx.toString() }));
Expand Down Expand Up @@ -241,10 +241,14 @@ export const Lyrics = () => {
transition={{ duration: 0.5 }}
>
{synced ? (
<SynchronizedLyrics {...(lyrics as SynchronizedLyricsProps)} />
<SynchronizedLyrics
{...(lyrics as SynchronizedLyricsProps)}
translatedLyrics={translatedLyrics}
/>
) : (
<UnsynchronizedLyrics
{...(lyrics as UnsynchronizedLyricsProps)}
translatedLyrics={translatedLyrics}
/>
)}
</ScrollContainer>
Expand Down
29 changes: 20 additions & 9 deletions src/renderer/features/lyrics/synchronized-lyrics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const SynchronizedLyricsContainer = styled.div<{ $gap: number }>`

export interface SynchronizedLyricsProps extends Omit<FullLyricsMetadata, 'lyrics'> {
lyrics: SynchronizedLyricsArray;
translatedLyrics?: string | null;
}

export const SynchronizedLyrics = ({
Expand All @@ -63,6 +64,7 @@ export const SynchronizedLyrics = ({
name,
remote,
source,
translatedLyrics,
}: SynchronizedLyricsProps) => {
const playersRef = PlayersRef;
const status = useCurrentStatus();
Expand Down Expand Up @@ -364,15 +366,24 @@ export const SynchronizedLyrics = ({
/>
)}
{lyrics.map(([time, text], idx) => (
<LyricLine
key={idx}
alignment={settings.alignment}
className="lyric-line synchronized"
fontSize={settings.fontSize}
id={`lyric-${idx}`}
text={text}
onClick={() => handleSeek(time / 1000)}
/>
<div key={idx}>
<LyricLine
alignment={settings.alignment}
className="lyric-line synchronized"
fontSize={settings.fontSize}
id={`lyric-${idx}`}
text={text}
onClick={() => handleSeek(time / 1000)}
/>
{translatedLyrics && (
<LyricLine
alignment={settings.alignment}
className="lyric-line synchronized translation"
fontSize={settings.fontSize}
text={translatedLyrics.split('\n')[idx]}
/>
)}
</div>
))}
</SynchronizedLyricsContainer>
);
Expand Down
31 changes: 23 additions & 8 deletions src/renderer/features/lyrics/unsynchronized-lyrics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useLyricsSettings } from '/@/renderer/store';

export interface UnsynchronizedLyricsProps extends Omit<FullLyricsMetadata, 'lyrics'> {
lyrics: string;
translatedLyrics?: string | null;
}

const UnsynchronizedLyricsContainer = styled.div<{ $gap: number }>`
Expand Down Expand Up @@ -45,12 +46,17 @@ export const UnsynchronizedLyrics = ({
name,
remote,
source,
translatedLyrics,
}: UnsynchronizedLyricsProps) => {
const settings = useLyricsSettings();
const lines = useMemo(() => {
return lyrics.split('\n');
}, [lyrics]);

const translatedLines = useMemo(() => {
return translatedLyrics ? translatedLyrics.split('\n') : [];
}, [translatedLyrics]);

return (
<UnsynchronizedLyricsContainer
$gap={settings.gapUnsync}
Expand All @@ -73,14 +79,23 @@ export const UnsynchronizedLyrics = ({
/>
)}
{lines.map((text, idx) => (
<LyricLine
key={idx}
alignment={settings.alignment}
className="lyric-line unsynchronized"
fontSize={settings.fontSizeUnsync}
id={`lyric-${idx}`}
text={text}
/>
<div key={idx}>
<LyricLine
alignment={settings.alignment}
className="lyric-line unsynchronized"
fontSize={settings.fontSizeUnsync}
id={`lyric-${idx}`}
text={text}
/>
{translatedLines[idx] && (
<LyricLine
alignment={settings.alignment}
className="lyric-line unsynchronized translation"
fontSize={settings.fontSizeUnsync}
text={translatedLines[idx]}
/>
)}
</div>
))}
</UnsynchronizedLyricsContainer>
);
Expand Down

0 comments on commit 164138e

Please sign in to comment.