All files / src/components/forms MusicSelect.tsx

0% Statements 0/47
0% Branches 0/29
0% Functions 0/19
0% Lines 0/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                                                                                                                                                                                                                                                                                     
import React, { memo, useCallback, useEffect, useMemo, useState } from "react";
import uniq from "lodash/uniq";
import { musicSelectors } from "store/features/entities/entitiesSelectors";
import {
  Option,
  Select,
  OptionLabelWithPreview,
  SingleValueWithPreview,
  SelectCommonProps,
  OptGroup,
  FormatFolderLabel,
} from "ui/form/Select";
import { PauseIcon, PlayIcon } from "ui/icons/Icons";
import { Button } from "ui/buttons/Button";
import musicActions from "store/features/music/musicActions";
import { useAppDispatch, useAppSelector } from "store/hooks";
import { SingleValue } from "react-select";
 
interface MusicSelectProps extends SelectCommonProps {
  name: string;
  value?: string;
  onChange?: (newId: string) => void;
}
 
interface PlayPauseTrackProps extends SelectCommonProps {
  musicId: string;
}
 
const PlayPauseTrack = ({ musicId }: PlayPauseTrackProps) => {
  const dispatch = useAppDispatch();
  const musicPlaying = useAppSelector((state) => state.music.playing);
 
  const onMouseDown = useCallback((e: React.MouseEvent<HTMLButtonElement>) => {
    e.stopPropagation();
    e.preventDefault();
  }, []);
 
  const onClick = useCallback(
    (e: React.MouseEvent<HTMLButtonElement>) => {
      e.stopPropagation();
      e.preventDefault();
      if (musicPlaying) {
        dispatch(musicActions.pauseMusic());
      } else {
        dispatch(musicActions.playMusic({ musicId }));
      }
    },
    [dispatch, musicId, musicPlaying],
  );
 
  // Cleanup on unmount
  useEffect(() => {
    return () => {
      dispatch(musicActions.pauseMusic());
    };
  }, [dispatch]);
 
  return (
    <Button
      size="small"
      variant="transparent"
      onClick={onClick}
      onMouseDown={onMouseDown}
    >
      {musicPlaying ? <PauseIcon /> : <PlayIcon />}
    </Button>
  );
};
 
const MusicSelectComponent = ({
  value,
  onChange,
  ...selectProps
}: MusicSelectProps) => {
  const tracks = useAppSelector((state) => musicSelectors.selectAll(state));
  const [options, setOptions] = useState<OptGroup[]>([]);
 
  useEffect(() => {
    const plugins = uniq(tracks.map((s) => s.plugin || "")).sort();
    setOptions(
      plugins.map((pluginKey) => ({
        label: pluginKey,
        options: tracks
          .filter((track) => (track.plugin || "") === pluginKey)
          .map((track) => ({
            label: track.filename,
            value: track.id,
          })),
      })),
    );
  }, [tracks]);
 
  const currentValue = useMemo(
    () =>
      options
        .flatMap((group) => group.options)
        .find((option) => option.value === value) || options[0]?.options[0],
    [options, value],
  );
 
  const onSelectChange = useCallback(
    (newValue: SingleValue<Option>) => {
      Iif (newValue) {
        onChange?.(newValue.value);
      }
    },
    [onChange],
  );
 
  return (
    <Select
      value={currentValue}
      options={options}
      onChange={onSelectChange}
      formatOptionLabel={(option: Option) => {
        return (
          <OptionLabelWithPreview
            preview={<PlayPauseTrack musicId={option.value} />}
          >
            <FormatFolderLabel label={option.label} />
          </OptionLabelWithPreview>
        );
      }}
      components={{
        SingleValue: () => (
          <SingleValueWithPreview
            preview={<PlayPauseTrack musicId={currentValue?.value || ""} />}
          >
            <FormatFolderLabel label={currentValue?.label} />
          </SingleValueWithPreview>
        ),
      }}
      {...selectProps}
    />
  );
};
 
export const MusicSelect = memo<MusicSelectProps>(MusicSelectComponent);