All files / src/components/pages MusicPageMod.tsx

0% Statements 0/54
0% Branches 0/31
0% Functions 0/12
0% Lines 0/52

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                                                                                                                                                                                                                                                                                         
import React, { useContext, useEffect, useMemo, useRef } from "react";
import { musicSelectors } from "store/features/entities/entitiesState";
import styled, { ThemeContext } from "styled-components";
import useWindowSize from "ui/hooks/use-window-size";
import useResizable from "ui/hooks/use-resizable";
import debounce from "lodash/debounce";
import editorActions from "store/features/editor/editorActions";
import { SplitPaneHorizontalDivider } from "ui/splitpane/SplitPaneDivider";
import {
  modFilter,
  NavigatorModSongs,
} from "components/music/NavigatorModSongs";
import ModViewer from "components/music/ModViewer";
import { useAppDispatch, useAppSelector } from "store/hooks";
import { sortByFilename } from "shared/lib/entities/entitiesHelpers";
 
const Wrapper = styled.div`
  display: flex;
  width: 100%;
`;
 
const MusicPageMod = () => {
  const dispatch = useAppDispatch();
  const themeContext = useContext(ThemeContext);
  const selectedId = useAppSelector((state) => state.navigation.id);
  const navigatorSidebarWidth = useAppSelector(
    (state) => state.editor.navigatorSidebarWidth
  );
  const windowSize = useWindowSize();
  const prevWindowWidthRef = useRef<number>(0);
  const windowWidth = windowSize.width || 0;
  const windowHeight = windowSize.height || 0;
  const minCenterPaneWidth = 0;
 
  const allTracks = useAppSelector(musicSelectors.selectAll);
  const allModTracks = useMemo(
    () => allTracks.filter(modFilter).sort(sortByFilename),
    [allTracks]
  );
 
  const track = useAppSelector((state) =>
    musicSelectors.selectById(state, selectedId)
  );
 
  const lastTrackId = useRef("");
  useEffect(() => {
    Iif (track) {
      lastTrackId.current = track.id;
    }
  }, [track]);
 
  const viewTrackId = useMemo(
    () => track?.id || lastTrackId.current || allModTracks[0]?.id,
    [allModTracks, track]
  );
 
  const [leftPaneWidth, setLeftPaneSize, startLeftPaneResize] = useResizable({
    initialSize: navigatorSidebarWidth,
    direction: "right",
    minSize: 50,
    maxSize: Math.max(101, windowWidth - minCenterPaneWidth - 200),
    onResizeComplete: (v) => {
      Iif (v < 200) {
        setLeftPaneSize(200);
      }
    },
  });
 
  useEffect(() => {
    prevWindowWidthRef.current = windowWidth;
  });
  const prevWidth = prevWindowWidthRef.current;
 
  useEffect(() => {
    Iif (windowWidth !== prevWidth) {
      const panelsTotalWidth = leftPaneWidth + minCenterPaneWidth;
      const widthOverflow = panelsTotalWidth - windowWidth;
      Iif (widthOverflow > 0) {
        setLeftPaneSize(leftPaneWidth - 0.5 * widthOverflow);
      }
    }
  }, [windowWidth, prevWidth, leftPaneWidth, setLeftPaneSize]);
 
  const debouncedStoreWidths = useRef(
    debounce((leftPaneWidth: number) => {
      dispatch(editorActions.resizeNavigatorSidebar(leftPaneWidth));
    }, 100)
  );
 
  useEffect(() => debouncedStoreWidths.current(leftPaneWidth), [leftPaneWidth]);
 
  return (
    <Wrapper>
      {" "}
      <div
        style={{
          transition: "opacity 0.3s ease-in-out",
          width: Math.max(200, leftPaneWidth),
          background: themeContext?.colors.sidebar.background,
          overflow: "hidden",
          position: "relative",
        }}
      >
        <div
          style={{
            minWidth: 200,
            position: "relative",
            width: "100%",
            height: "100%",
          }}
        >
          <NavigatorModSongs
            height={windowHeight - 38}
            selectedId={selectedId}
          />
        </div>
      </div>
      <SplitPaneHorizontalDivider onMouseDown={startLeftPaneResize} />
      <div
        style={{
          flex: "1 1 0",
          minWidth: 0,
          overflow: "hidden",
          background: themeContext?.colors.background,
          color: themeContext?.colors.text,
          height: windowHeight - 38,
          position: "relative",
          display: "flex",
          flexDirection: "column",
        }}
      >
        <div style={{ flexGrow: 1, position: "relative" }}>
          <ModViewer trackId={viewTrackId} />
        </div>
      </div>
    </Wrapper>
  );
};
 
export default MusicPageMod;