All files / src/components/pages SoundsPage.tsx

0% Statements 0/54
0% Branches 0/33
0% Functions 0/13
0% Lines 0/51

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                                                                                                                                                                                                                                                                         
import React, { useContext, useEffect, useMemo, useRef } from "react";
import styled, { ThemeContext } from "styled-components";
import debounce from "lodash/debounce";
import useResizable from "ui/hooks/use-resizable";
import useWindowSize from "ui/hooks/use-window-size";
import { SplitPaneHorizontalDivider } from "ui/splitpane/SplitPaneDivider";
import editorActions from "store/features/editor/editorActions";
import { soundSelectors } from "store/features/entities/entitiesState";
import { NavigatorSounds } from "components/sounds/NavigatorSounds";
import { SoundViewer } from "components/sounds/SoundViewer";
import { useAppDispatch, useAppSelector } from "store/hooks";
 
const Wrapper = styled.div`
  display: flex;
  width: 100%;
`;
 
const SoundsPage = () => {
  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 allSounds = useAppSelector((state) => soundSelectors.selectAll(state));
 
  const sound = useAppSelector((state) =>
    soundSelectors.selectById(state, selectedId)
  );
 
  const lastSoundId = useRef("");
  useEffect(() => {
    Iif (sound) {
      lastSoundId.current = sound.id;
    }
  }, [sound]);
 
  const viewSoundId = useMemo(
    () => sound?.id || lastSoundId.current || allSounds[0]?.id,
    [allSounds, sound]
  );
 
  const viewSound = useAppSelector((state) =>
    soundSelectors.selectById(state, viewSoundId)
  );
 
  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%",
          }}
        >
          <NavigatorSounds 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" }}>
          {viewSound && <SoundViewer file={viewSound} />}
        </div>
      </div>
    </Wrapper>
  );
};
 
export default SoundsPage;