All files / src/components/forms MusicSelect.tsx

0% Statements 0/54
0% Branches 0/30
0% Functions 0/19
0% Lines 0/49

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 140 141 142 143                                                                                                                                                                                                                                                                                             
import React, { useCallback, useEffect, useState } from "react";
import uniq from "lodash/uniq";
import { musicSelectors } from "store/features/entities/entitiesState";
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>
  );
};
 
export const MusicSelect = ({
  value,
  onChange,
  ...selectProps
}: MusicSelectProps) => {
  const tracks = useAppSelector((state) => musicSelectors.selectAll(state));
  const [options, setOptions] = useState<OptGroup[]>([]);
  const [currentValue, setCurrentValue] = useState<Option>();
 
  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]);
 
  useEffect(() => {
    let option: Option | null = null;
    options.find((optGroup) => {
      const foundOption = optGroup.options.find((opt) => opt.value === value);
      Iif (foundOption) {
        option = foundOption;
        return true;
      }
      return false;
    });
    setCurrentValue(option || 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}
    />
  );
};