aboutsummaryrefslogtreecommitdiff
path: root/users/ryan/modules/simple-bar/simple-bar-source/lib/components/aerospace/spaces.jsx
blob: d576389e870907dea5221f92063573e77e7d4202 (plain)
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
import * as Uebersicht from "uebersicht";
import Space from "./space.jsx";
import { useAerospaceContext } from "../aerospace-context.jsx";
import { useSimpleBarContext } from "../simple-bar-context.jsx";
import * as Utils from "../../utils.js";
import * as AeroSpace from "../../aerospace.js";

export { spacesStyles as styles } from "../../styles/components/spaces/spaces.js";

const { React } = Uebersicht;

/**
 * Spaces component to display spaces on the screen.
 * @returns {JSX.Element|null} The rendered component.
 */
const Component = React.memo(() => {
  // Get spaces from aerospace context
  const { spaces } = useAerospaceContext();
  // Get displays, displayIndex, and settings from simple bar context
  const { displays, displayIndex, settings } = useSimpleBarContext();
  const { spacesDisplay, process } = settings;
  const { displayAllSpacesOnAllScreens, showOnDisplay } = spacesDisplay;
  // Determine if the component should be visible on the current display
  const visible = Utils.isVisibleOnDisplay(displayIndex, showOnDisplay);
  const isProcessVisible = Utils.isVisibleOnDisplay(
    displayIndex,
    process.showOnDisplay
  );

  // If not visible, return null
  if (!visible) return null;

  // If there are no spaces, return an empty div
  if (!spaces?.length) {
    return <div className="spaces spaces--empty" />;
  }

  // Map through displays and render spaces for the current display
  return displays.map((display) => {
    const displayId = AeroSpace.getDisplayIndex(display);
    if (displayId !== displayIndex) return null;

    // Filter spaces based on display settings
    const filteredSpaces = displayAllSpacesOnAllScreens
      ? spaces
      : spaces.filter((space) => space.monitor === displayId);

    return (
      <div key={displayId} className="spaces">
        {filteredSpaces.map((space, i) => {
          const { workspace } = space;
          const lastOfSpace =
            i !== 0 && space.monitor !== spaces[i - 1].monitor;

          return (
            <Space key={workspace} space={space} lastOfSpace={lastOfSpace} />
          );
        })}
        {isProcessVisible && <div className="spaces__end-separator" />}
      </div>
    );
  });
});

Component.displayName = "Spaces";

export default Component;