All files / src/components/forms SceneSelect.tsx

0% Statements 0/43
0% Branches 0/34
0% Functions 0/14
0% Lines 0/42

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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212                                                                                                                                                                                                                                                                                                                                                                                                                                       
import React, { memo, useCallback, useMemo } from "react";
import {
  backgroundSelectors,
  sceneSelectors,
} from "store/features/entities/entitiesSelectors";
import {
  Option,
  OptionLabelWithPreview,
  SingleValueWithPreview,
  Select,
  SelectCommonProps,
  FormatFolderLabel,
} from "ui/form/Select";
import styled from "styled-components";
import editorActions from "store/features/editor/editorActions";
import { sceneName } from "shared/lib/entities/entitiesHelpers";
import { assetURLStyleProp } from "shared/lib/helpers/assets";
import {
  useAppDispatch,
  useAppSelectorPick,
  useAppSelectorPickArray,
} from "store/hooks";
import { SingleValue } from "react-select";
 
interface SceneSelectProps extends SelectCommonProps {
  name: string;
  value?: string;
  optional?: boolean;
  optionalLabel?: string;
  onChange?: (newId: string) => void;
}
 
const Thumbnail = styled.div`
  width: 20px;
  height: 20px;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
`;
 
type SceneSelectScene = {
  id: string;
  name: string;
  backgroundId: string;
};
 
type SceneSelectBackground = {
  id: string;
  filename: string;
  plugin?: string;
  _v?: number;
};
 
interface SceneOption extends Option {
  scene?: SceneSelectScene;
}
 
const collator = new Intl.Collator(undefined, {
  numeric: true,
  sensitivity: "base",
});
 
const sortByLabel = (a: SceneOption, b: SceneOption) => {
  return collator.compare(a.label, b.label);
};
 
const sceneToSceneOption = (
  scene: SceneSelectScene,
  sceneIndex: number,
): SceneOption => ({
  value: scene.id,
  label: sceneName(scene, sceneIndex),
  scene,
});
 
export const SceneSelect = memo(
  ({
    value,
    onChange,
    optional,
    optionalLabel,
    ...selectProps
  }: SceneSelectProps) => {
    const dispatch = useAppDispatch();
 
    const scenes = useAppSelectorPickArray(sceneSelectors.selectAll, [
      "id",
      "name",
      "backgroundId",
    ] as const);
 
    const selectedScene = useAppSelectorPick(
      (state) => sceneSelectors.selectById(state, value || ""),
      ["id", "name", "backgroundId"] as const,
    );
 
    const backgrounds = useAppSelectorPickArray(backgroundSelectors.selectAll, [
      "id",
      "filename",
      "plugin",
      "_v",
    ] as const);
 
    const backgroundsLookup = useMemo<Record<string, SceneSelectBackground>>(
      () =>
        Object.fromEntries(
          backgrounds.map((background) => [background.id, background]),
        ),
      [backgrounds],
    );
 
    const selectedBackground = useAppSelectorPick(
      (state) =>
        selectedScene?.backgroundId
          ? backgroundSelectors.selectById(state, selectedScene.backgroundId)
          : undefined,
      ["filename", "plugin", "_v"] as const,
    );
 
    const options = useMemo(
      () =>
        ([] as SceneOption[]).concat(
          optional
            ? [
                {
                  value: "",
                  label: optionalLabel || "None",
                },
              ]
            : [],
          scenes.map(sceneToSceneOption).sort(sortByLabel),
        ),
      [scenes, optional, optionalLabel],
    );
 
    const currentValue = useMemo(() => {
      const sceneIndex = scenes.findIndex((scene) => scene.id === value);
      const scene = sceneIndex >= 0 ? scenes[sceneIndex] : undefined;
 
      return scene ? sceneToSceneOption(scene, sceneIndex) : undefined;
    }, [scenes, value]);
 
    const onSelectChange = useCallback(
      (newValue: SingleValue<Option>) => {
        Iif (newValue) {
          onChange?.(newValue.value);
        }
      },
      [onChange],
    );
 
    const onJumpToScene = useCallback(
      (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
        Iif (e.altKey && value) {
          dispatch(editorActions.selectScene({ sceneId: value }));
          dispatch(editorActions.setFocusSceneId(value));
        }
      },
      [dispatch, value],
    );
 
    return (
      <div onClick={onJumpToScene}>
        <Select
          value={currentValue}
          options={options}
          onChange={onSelectChange}
          formatOptionLabel={(option: SceneOption) => {
            const background = option.scene
              ? backgroundsLookup[option.scene.backgroundId]
              : undefined;
 
            return (
              <OptionLabelWithPreview
                preview={
                  <Thumbnail
                    style={{
                      backgroundImage:
                        background &&
                        assetURLStyleProp("backgrounds", background),
                    }}
                  />
                }
              >
                <FormatFolderLabel label={option.label} />
              </OptionLabelWithPreview>
            );
          }}
          components={{
            SingleValue: () => (
              <SingleValueWithPreview
                preview={
                  <Thumbnail
                    style={{
                      backgroundImage:
                        selectedBackground &&
                        assetURLStyleProp("backgrounds", selectedBackground),
                    }}
                  />
                }
              >
                <FormatFolderLabel label={currentValue?.label} />
              </SingleValueWithPreview>
            ),
          }}
          {...selectProps}
        />
      </div>
    );
  },
);