feishin/src/renderer/features/search/components/library-command-item.tsx

158 lines
5.8 KiB
TypeScript
Raw Normal View History

import { CSSProperties, SyntheticEvent, useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';
import styles from './library-command-item.module.css';
import { ActionIcon } from '/@/shared/components/action-icon/action-icon';
import { Flex } from '/@/shared/components/flex/flex';
import { Group } from '/@/shared/components/group/group';
import { Image } from '/@/shared/components/image/image';
import { Text } from '/@/shared/components/text/text';
2025-05-20 19:23:36 -07:00
import { LibraryItem } from '/@/shared/types/domain-types';
import { Play, PlayQueueAddOptions } from '/@/shared/types/types';
2023-05-19 00:21:36 -07:00
interface LibraryCommandItemProps {
2025-05-07 19:53:23 -07:00
disabled?: boolean;
2023-07-01 19:10:05 -07:00
handlePlayQueueAdd?: (options: PlayQueueAddOptions) => void;
id: string;
imageUrl: null | string;
isHighlighted?: boolean;
2023-07-01 19:10:05 -07:00
itemType: LibraryItem;
subtitle?: string;
title?: string;
2023-05-19 00:21:36 -07:00
}
export const LibraryCommandItem = ({
2025-05-07 19:53:23 -07:00
disabled,
handlePlayQueueAdd,
2023-07-01 19:10:05 -07:00
id,
imageUrl,
isHighlighted,
itemType,
2023-07-01 19:10:05 -07:00
subtitle,
title,
2023-05-19 00:21:36 -07:00
}: LibraryCommandItemProps) => {
const { t } = useTranslation();
2023-05-19 00:21:36 -07:00
2023-07-01 19:10:05 -07:00
const handlePlay = useCallback(
(e: SyntheticEvent, id: string, playType: Play) => {
2023-07-01 19:10:05 -07:00
e.stopPropagation();
handlePlayQueueAdd?.({
byItemType: {
id: [id],
type: itemType,
},
playType,
});
2023-05-19 00:21:36 -07:00
},
2023-07-01 19:10:05 -07:00
[handlePlayQueueAdd, itemType],
);
2023-05-19 00:21:36 -07:00
const [isHovered, setIsHovered] = useState(false);
const showControls = isHighlighted || isHovered;
2023-07-01 19:10:05 -07:00
return (
<Flex
2023-07-01 19:10:05 -07:00
gap="xl"
justify="space-between"
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
2023-07-01 19:10:05 -07:00
style={{ height: '40px', width: '100%' }}
2023-05-19 00:21:36 -07:00
>
2025-07-12 11:17:54 -07:00
<div className={styles.itemGrid} style={{ '--item-height': '40px' } as CSSProperties}>
<div className={styles.imageWrapper}>
<Image
alt="cover"
className={styles.image}
height={40}
src={imageUrl || ''}
width={40}
/>
</div>
<div className={styles.metadataWrapper}>
2023-07-01 19:10:05 -07:00
<Text overflow="hidden">{title}</Text>
2025-07-12 11:17:54 -07:00
<Text isMuted overflow="hidden">
2023-07-01 19:10:05 -07:00
{subtitle}
</Text>
</div>
</div>
{showControls && (
2025-07-12 11:17:54 -07:00
<Group align="center" gap="sm" justify="flex-end" wrap="nowrap">
<ActionIcon
2025-06-09 16:02:03 +07:00
disabled={disabled}
icon="mediaPlay"
onClick={(e) => handlePlay(e, id, Play.NOW)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
handlePlay(e, id, Play.NOW);
}
}}
size="xs"
tabIndex={disabled ? -1 : 0}
2025-06-09 16:02:03 +07:00
tooltip={{
label: t('player.play', { postProcess: 'sentenceCase' }),
2025-06-09 16:02:03 +07:00
openDelay: 500,
}}
variant="subtle"
/>
{itemType !== LibraryItem.SONG && (
<ActionIcon
disabled={disabled}
icon="mediaShuffle"
onClick={(e) => handlePlay(e, id, Play.SHUFFLE)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
handlePlay(e, id, Play.SHUFFLE);
}
}}
size="xs"
tabIndex={disabled ? -1 : 0}
tooltip={{
label: t('player.shuffle', { postProcess: 'sentenceCase' }),
openDelay: 500,
}}
variant="subtle"
/>
)}
<ActionIcon
disabled={disabled}
icon="mediaPlayLast"
onClick={(e) => handlePlay(e, id, Play.LAST)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
handlePlay(e, id, Play.LAST);
}
}}
size="xs"
tabIndex={disabled ? -1 : 0}
tooltip={{
label: t('player.addLast', { postProcess: 'sentenceCase' }),
openDelay: 500,
}}
variant="subtle"
/>
<ActionIcon
disabled={disabled}
icon="mediaPlayNext"
onClick={(e) => handlePlay(e, id, Play.NEXT)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
handlePlay(e, id, Play.NEXT);
}
}}
size="xs"
tabIndex={disabled ? -1 : 0}
tooltip={{
label: t('player.addNext', { postProcess: 'sentenceCase' }),
openDelay: 500,
}}
variant="subtle"
/>
</Group>
)}
</Flex>
2023-07-01 19:10:05 -07:00
);
2023-05-19 00:21:36 -07:00
};