users/ryan/modules/simple-bar/simple-bar-source/lib/components/aerospace/spaces.jsx
2bead7efb473f0202dc6ef189fbc0e4a5401d41f
· 2.2 KB · 67 lines
raw
| 1 | import * as Uebersicht from "uebersicht"; |
| 2 | import Space from "./space.jsx"; |
| 3 | import { useAerospaceContext } from "../aerospace-context.jsx"; |
| 4 | import { useSimpleBarContext } from "../simple-bar-context.jsx"; |
| 5 | import * as Utils from "../../utils.js"; |
| 6 | import * as AeroSpace from "../../aerospace.js"; |
| 7 | |
| 8 | export { spacesStyles as styles } from "../../styles/components/spaces/spaces.js"; |
| 9 | |
| 10 | const { React } = Uebersicht; |
| 11 | |
| 12 | /** |
| 13 | * Spaces component to display spaces on the screen. |
| 14 | * @returns {JSX.Element|null} The rendered component. |
| 15 | */ |
| 16 | const Component = React.memo(() => { |
| 17 | // Get spaces from aerospace context |
| 18 | const { spaces } = useAerospaceContext(); |
| 19 | // Get displays, displayIndex, and settings from simple bar context |
| 20 | const { displays, displayIndex, settings } = useSimpleBarContext(); |
| 21 | const { spacesDisplay, process } = settings; |
| 22 | const { displayAllSpacesOnAllScreens, showOnDisplay } = spacesDisplay; |
| 23 | // Determine if the component should be visible on the current display |
| 24 | const visible = Utils.isVisibleOnDisplay(displayIndex, showOnDisplay); |
| 25 | const isProcessVisible = Utils.isVisibleOnDisplay( |
| 26 | displayIndex, |
| 27 | process.showOnDisplay |
| 28 | ); |
| 29 | |
| 30 | // If not visible, return null |
| 31 | if (!visible) return null; |
| 32 | |
| 33 | // If there are no spaces, return an empty div |
| 34 | if (!spaces?.length) { |
| 35 | return <div className="spaces spaces--empty" />; |
| 36 | } |
| 37 | |
| 38 | // Map through displays and render spaces for the current display |
| 39 | return displays.map((display) => { |
| 40 | const displayId = AeroSpace.getDisplayIndex(display); |
| 41 | if (displayId !== displayIndex) return null; |
| 42 | |
| 43 | // Filter spaces based on display settings |
| 44 | const filteredSpaces = displayAllSpacesOnAllScreens |
| 45 | ? spaces |
| 46 | : spaces.filter((space) => space.monitor === displayId); |
| 47 | |
| 48 | return ( |
| 49 | <div key={displayId} className="spaces"> |
| 50 | {filteredSpaces.map((space, i) => { |
| 51 | const { workspace } = space; |
| 52 | const lastOfSpace = |
| 53 | i !== 0 && space.monitor !== spaces[i - 1].monitor; |
| 54 | |
| 55 | return ( |
| 56 | <Space key={workspace} space={space} lastOfSpace={lastOfSpace} /> |
| 57 | ); |
| 58 | })} |
| 59 | {isProcessVisible && <div className="spaces__end-separator" />} |
| 60 | </div> |
| 61 | ); |
| 62 | }); |
| 63 | }); |
| 64 | |
| 65 | Component.displayName = "Spaces"; |
| 66 | |
| 67 | export default Component; |