feishin/src/renderer/features/lyrics/lyrics-actions.tsx

116 lines
4 KiB
TypeScript
Raw Normal View History

import { Box, Group } from '@mantine/core';
2023-06-09 16:30:31 -07:00
import isElectron from 'is-electron';
import { useTranslation } from 'react-i18next';
2023-06-08 03:40:58 -07:00
import { RiAddFill, RiSubtractFill } from 'react-icons/ri';
import { LyricsOverride } from '/@/renderer/api/types';
2023-06-09 16:39:13 -07:00
import { Button, NumberInput, Tooltip } from '/@/renderer/components';
2023-06-08 03:40:58 -07:00
import { openLyricSearchModal } from '/@/renderer/features/lyrics/components/lyrics-search-form';
import {
2023-07-01 19:10:05 -07:00
useCurrentSong,
useLyricsSettings,
useSettingsStore,
useSettingsStoreActions,
} from '/@/renderer/store';
2023-06-08 03:40:58 -07:00
interface LyricsActionsProps {
2023-07-01 19:10:05 -07:00
onRemoveLyric: () => void;
onResetLyric: () => void;
2023-07-01 19:10:05 -07:00
onSearchOverride: (params: LyricsOverride) => void;
}
export const LyricsActions = ({
onRemoveLyric,
onResetLyric,
onSearchOverride,
}: LyricsActionsProps) => {
const { t } = useTranslation();
2023-07-01 19:10:05 -07:00
const currentSong = useCurrentSong();
const { setSettings } = useSettingsStoreActions();
const { delayMs, sources } = useLyricsSettings();
2023-07-01 19:10:05 -07:00
const handleLyricOffset = (e: number) => {
setSettings({
lyrics: {
...useSettingsStore.getState().lyrics,
delayMs: e,
},
});
};
2023-06-08 03:40:58 -07:00
2023-07-01 19:10:05 -07:00
const isActionsDisabled = !currentSong;
const isDesktop = isElectron();
2023-07-01 19:10:05 -07:00
return (
<Box style={{ position: 'relative', width: '100%' }}>
<Group position="center">
{isDesktop && sources.length ? (
<Button
uppercase
disabled={isActionsDisabled}
variant="subtle"
onClick={() =>
openLyricSearchModal({
artist: currentSong?.artistName,
name: currentSong?.name,
onSearchOverride,
})
}
>
{t('common.search', { postProcess: 'titleCase' })}
</Button>
) : null}
2023-07-01 19:10:05 -07:00
<Button
aria-label="Decrease lyric offset"
2023-07-01 19:10:05 -07:00
variant="subtle"
onClick={() => handleLyricOffset(delayMs - 50)}
2023-07-01 19:10:05 -07:00
>
<RiSubtractFill />
2023-07-01 19:10:05 -07:00
</Button>
<Tooltip
label={t('setting.lyricOffset', { postProcess: 'sentenceCase' })}
openDelay={500}
>
<NumberInput
aria-label="Lyric offset"
styles={{ input: { textAlign: 'center' } }}
value={delayMs || 0}
width={55}
onChange={handleLyricOffset}
/>
</Tooltip>
2023-07-01 19:10:05 -07:00
<Button
aria-label="Increase lyric offset"
2023-07-01 19:10:05 -07:00
variant="subtle"
onClick={() => handleLyricOffset(delayMs + 50)}
2023-07-01 19:10:05 -07:00
>
<RiAddFill />
2023-07-01 19:10:05 -07:00
</Button>
{isDesktop && sources.length ? (
<Button
uppercase
disabled={isActionsDisabled}
variant="subtle"
onClick={onResetLyric}
>
{t('common.reset', { postProcess: 'sentenceCase' })}
</Button>
) : null}
</Group>
<Box style={{ position: 'absolute', right: 0, top: 0 }}>
{isDesktop && sources.length ? (
<Button
uppercase
color="red"
disabled={isActionsDisabled}
variant="subtle"
onClick={onRemoveLyric}
>
{t('common.clear', { postProcess: 'sentenceCase' })}
</Button>
) : null}
</Box>
</Box>
2023-07-01 19:10:05 -07:00
);
2023-06-08 03:40:58 -07:00
};