users/ryan/modules/simple-bar/simple-bar-source/lib/components/aerospace/space.jsx
9cb45d050939b499e8e01ec9e4fe9be998d98dc4
· 2.6 KB · 84 lines
raw
| 1 | import * as Uebersicht from "uebersicht"; |
| 2 | import OpenedApps from "./opened-apps.jsx"; |
| 3 | import { useSimpleBarContext } from "../simple-bar-context.jsx"; |
| 4 | import * as Utils from "../../utils.js"; |
| 5 | import * as Aerospace from "../../aerospace.js"; |
| 6 | |
| 7 | const { React } = Uebersicht; |
| 8 | |
| 9 | /** |
| 10 | * Space component to display a single space. |
| 11 | * @param {Object} props - The component props. |
| 12 | * @param {Object} props.space - The space object. |
| 13 | * @param {boolean} props.lastOfSpace - Indicates if this is the last space of the monitor. |
| 14 | * @returns {JSX.Element|null} The rendered component. |
| 15 | */ |
| 16 | export default function Space({ space, lastOfSpace }) { |
| 17 | const { windows } = space; |
| 18 | const { settings } = useSimpleBarContext(); |
| 19 | const { spacesDisplay } = settings; |
| 20 | const { |
| 21 | displayAllSpacesOnAllScreens, |
| 22 | exclusionsAsRegex, |
| 23 | hideDuplicateAppsInSpaces, |
| 24 | } = spacesDisplay; |
| 25 | const { workspace, focused } = space; |
| 26 | |
| 27 | /** |
| 28 | * Handle click event to switch to the clicked space. |
| 29 | * @param {Event} e - The click event. |
| 30 | */ |
| 31 | const onClick = (e) => { |
| 32 | if (focused) return; |
| 33 | Aerospace.goToSpace(workspace); |
| 34 | Utils.clickEffect(e); |
| 35 | }; |
| 36 | |
| 37 | // Determine if the space should be hidden |
| 38 | const hidden = !focused && !windows.length && spacesDisplay.hideEmptySpaces; |
| 39 | |
| 40 | if (hidden) return null; |
| 41 | |
| 42 | // Get exclusions and title exclusions based on settings |
| 43 | const exclusions = exclusionsAsRegex |
| 44 | ? spacesDisplay.exclusions |
| 45 | : spacesDisplay.exclusions.split(", "); |
| 46 | const titleExclusions = exclusionsAsRegex |
| 47 | ? spacesDisplay.titleExclusions |
| 48 | : spacesDisplay.titleExclusions.split(", "); |
| 49 | |
| 50 | // Filter windows based on exclusions |
| 51 | const filteredWindows = windows.filter((window) => |
| 52 | Utils.filterApps(window, exclusions, titleExclusions, exclusionsAsRegex), |
| 53 | ); |
| 54 | |
| 55 | // Remove duplicate apps if the setting is enabled |
| 56 | const displayedWindows = hideDuplicateAppsInSpaces |
| 57 | ? filteredWindows.reduce((acc, window) => { |
| 58 | const isDuplicate = acc.find( |
| 59 | (w) => w["app-name"] === window["app-name"], |
| 60 | ); |
| 61 | return isDuplicate ? acc : [...acc, window]; |
| 62 | }, []) |
| 63 | : filteredWindows; |
| 64 | |
| 65 | // Determine the CSS classes for the space |
| 66 | const classes = Utils.classNames("space", { |
| 67 | "space--focused": focused, |
| 68 | "space--empty": windows.length, |
| 69 | }); |
| 70 | |
| 71 | return ( |
| 72 | <React.Fragment> |
| 73 | {displayAllSpacesOnAllScreens && lastOfSpace && ( |
| 74 | <div className="spaces__separator" /> |
| 75 | )} |
| 76 | <div className={classes}> |
| 77 | <button className="space__inner" onClick={onClick}> |
| 78 | {workspace} |
| 79 | <OpenedApps apps={displayedWindows} /> |
| 80 | </button> |
| 81 | </div> |
| 82 | </React.Fragment> |
| 83 | ); |
| 84 | } |