aboutsummaryrefslogtreecommitdiff
path: root/users/ryan/modules/simple-bar/simple-bar-source/lib/components/aerospace-context.jsx
diff options
context:
space:
mode:
Diffstat (limited to 'users/ryan/modules/simple-bar/simple-bar-source/lib/components/aerospace-context.jsx')
-rwxr-xr-xusers/ryan/modules/simple-bar/simple-bar-source/lib/components/aerospace-context.jsx117
1 files changed, 117 insertions, 0 deletions
diff --git a/users/ryan/modules/simple-bar/simple-bar-source/lib/components/aerospace-context.jsx b/users/ryan/modules/simple-bar/simple-bar-source/lib/components/aerospace-context.jsx
new file mode 100755
index 0000000..f780a71
--- /dev/null
+++ b/users/ryan/modules/simple-bar/simple-bar-source/lib/components/aerospace-context.jsx
@@ -0,0 +1,117 @@
1import * as Uebersicht from "uebersicht";
2import { useSimpleBarContext } from "./simple-bar-context.jsx";
3import useServerSocket from "../hooks/use-server-socket.js";
4import * as Aerospace from "../aerospace.js";
5
6const { React } = Uebersicht;
7
8// Create a context with default values
9const AerospaceContext = React.createContext({
10 spaces: [],
11 windows: [],
12 mode: "",
13});
14
15/**
16 * Custom hook to use the AerospaceContext
17 * @returns {Object} The context value
18 */
19export function useAerospaceContext() {
20 return React.useContext(AerospaceContext);
21}
22
23// Memoized AerospaceContextProvider component
24export default React.memo(AerospaceContextProvider);
25
26/**
27 * AerospaceContextProvider component
28 * @param {Object} props - The component props
29 * @param {React.ReactNode} props.children - The child components
30 * @returns {JSX.Element} The provider component
31 */
32function AerospaceContextProvider({ children }) {
33 // Get settings, displayIndex, and displays from SimpleBarContext
34 const { settings, displayIndex, displays } = useSimpleBarContext();
35 const { enableServer, aerospaceServerRefresh } = settings.global;
36 const serverEnabled = enableServer && aerospaceServerRefresh;
37
38 // State to store aerospace spaces
39 const [aerospaceSpaces, setAerospaceSpaces] = React.useState([]);
40 const latestRequestRef = React.useRef(0);
41
42 // Fetches and sets the aerospace spaces
43 const getSpaces = React.useCallback(async () => {
44 const requestId = latestRequestRef.current + 1;
45 latestRequestRef.current = requestId;
46
47 let focusedWindow = {};
48 const [focusedSpace] = await Aerospace.getFocusedSpace();
49 try {
50 [focusedWindow] = await Aerospace.getFocusedWindow();
51 // eslint-disable-next-line no-empty
52 } catch {}
53 const spaces = await Promise.all(
54 displays.map(async (display) => {
55 const id = display["monitor-id"];
56 const result = await Aerospace.getSpaces(id);
57 return Promise.all(
58 result.map(async (space) => {
59 const focused = space.workspace === focusedSpace.workspace;
60 const monitor = Aerospace.getDisplayIndex(space);
61 const windows = await Aerospace.getWindows(space.workspace);
62 const formatted = windows.map((window) => {
63 const focused =
64 window["window-id"] === focusedWindow["window-id"];
65 return {
66 ...window,
67 focused,
68 };
69 });
70 return { ...space, windows: formatted, focused, monitor };
71 })
72 );
73 })
74 );
75 if (requestId !== latestRequestRef.current) {
76 return;
77 }
78 setAerospaceSpaces(spaces.flat());
79 }, [displays]);
80
81 // Refreshes spaces with the data sent by simple-bar-server if it exists
82 // in order to speed up the process then refreshes everything in background
83 // else, simply refreshes everything
84 const refreshSpaces = React.useCallback(
85 async (data) => {
86 if (data) {
87 const { space } = data;
88 setAerospaceSpaces((current) => {
89 return current.map((s) => ({ ...s, focused: s.workspace === space }));
90 });
91 getSpaces();
92 } else {
93 await getSpaces();
94 }
95 },
96 [getSpaces]
97 );
98
99 // Resets the aerospace spaces state
100 const resetSpaces = () => {
101 setAerospaceSpaces([]);
102 };
103
104 // Use server socket to fetch and reset spaces
105 useServerSocket("spaces", serverEnabled, refreshSpaces, resetSpaces);
106
107 // Fetch spaces on component mount and when displayIndex changes
108 React.useEffect(() => {
109 refreshSpaces();
110 }, [refreshSpaces, displayIndex]);
111
112 return (
113 <AerospaceContext.Provider value={{ spaces: aerospaceSpaces }}>
114 {children}
115 </AerospaceContext.Provider>
116 );
117}