users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/zoom.jsx
c589cef163fddefcca977f130319f6b5e3657621
· 3.2 KB · 107 lines
raw
| 1 | import * as Uebersicht from "uebersicht"; |
| 2 | import * as DataWidget from "./data-widget.jsx"; |
| 3 | import * as DataWidgetLoader from "./data-widget-loader.jsx"; |
| 4 | import * as Icons from "../icons/icons.jsx"; |
| 5 | import { SuspenseIcon } from "../icons/icon.jsx"; |
| 6 | import useWidgetRefresh from "../../hooks/use-widget-refresh"; |
| 7 | import useServerSocket from "../../hooks/use-server-socket"; |
| 8 | import { useSimpleBarContext } from "../simple-bar-context.jsx"; |
| 9 | import * as Utils from "../../utils"; |
| 10 | |
| 11 | export { zoomStyles as styles } from "../../styles/components/data/zoom"; |
| 12 | |
| 13 | const { React } = Uebersicht; |
| 14 | |
| 15 | const DEFAULT_REFRESH_FREQUENCY = 5000; |
| 16 | |
| 17 | /** |
| 18 | * Zoom widget component. |
| 19 | * @returns {JSX.Element|null} The Zoom widget. |
| 20 | */ |
| 21 | export const Widget = React.memo(() => { |
| 22 | const { displayIndex, settings } = useSimpleBarContext(); |
| 23 | const { widgets, zoomWidgetOptions } = settings; |
| 24 | const { zoomWidget } = widgets; |
| 25 | const { refreshFrequency, showVideo, showMic, showOnDisplay } = |
| 26 | zoomWidgetOptions; |
| 27 | |
| 28 | // Determine the refresh frequency for the widget |
| 29 | const refresh = React.useMemo( |
| 30 | () => |
| 31 | Utils.getRefreshFrequency(refreshFrequency, DEFAULT_REFRESH_FREQUENCY), |
| 32 | [refreshFrequency], |
| 33 | ); |
| 34 | |
| 35 | // Determine if the widget should be visible |
| 36 | const visible = |
| 37 | Utils.isVisibleOnDisplay(displayIndex, showOnDisplay) && zoomWidget; |
| 38 | |
| 39 | const [state, setState] = React.useState(); |
| 40 | const [loading, setLoading] = React.useState(visible); |
| 41 | |
| 42 | /** |
| 43 | * Reset the widget state. |
| 44 | */ |
| 45 | const resetWidget = React.useCallback(() => { |
| 46 | setState(undefined); |
| 47 | setLoading(false); |
| 48 | }, []); |
| 49 | |
| 50 | /** |
| 51 | * Fetch the Zoom status for mic and video. |
| 52 | */ |
| 53 | const getZoom = React.useCallback(async () => { |
| 54 | if (!visible) return; |
| 55 | try { |
| 56 | const [mic, video] = await Promise.all([ |
| 57 | Utils.cachedRun( |
| 58 | `osascript ./simple-bar/lib/scripts/zoom-mute-status.applescript`, |
| 59 | refresh, |
| 60 | ), |
| 61 | Utils.cachedRun( |
| 62 | `osascript ./simple-bar/lib/scripts/zoom-video-status.applescript`, |
| 63 | refresh, |
| 64 | ), |
| 65 | ]); |
| 66 | setState({ |
| 67 | mic: Utils.cleanupOutput(mic), |
| 68 | video: Utils.cleanupOutput(video), |
| 69 | }); |
| 70 | setLoading(false); |
| 71 | } catch (error) { |
| 72 | // eslint-disable-next-line no-console |
| 73 | console.error("Error fetching Zoom status:", error); |
| 74 | setLoading(false); |
| 75 | } |
| 76 | }, [visible, refresh]); |
| 77 | |
| 78 | // Use server socket to listen for Zoom events |
| 79 | useServerSocket("zoom", visible, getZoom, resetWidget, setLoading); |
| 80 | // Refresh the widget at the specified interval |
| 81 | useWidgetRefresh(visible, getZoom, refresh); |
| 82 | |
| 83 | if (loading) return <DataWidgetLoader.Widget className="zoom" />; |
| 84 | if (!state || (!state.mic.length && !state.video.length)) return null; |
| 85 | |
| 86 | const { mic, video } = state; |
| 87 | |
| 88 | const VideoIcon = video === "off" ? Icons.CameraOff : Icons.Camera; |
| 89 | const MicIcon = mic === "off" ? Icons.MicOff : Icons.MicOn; |
| 90 | |
| 91 | return ( |
| 92 | <DataWidget.Widget classes="zoom"> |
| 93 | {showVideo && ( |
| 94 | <SuspenseIcon> |
| 95 | <VideoIcon className={`zoom__icon zoom__icon--${video}`} /> |
| 96 | </SuspenseIcon> |
| 97 | )} |
| 98 | {showMic && ( |
| 99 | <SuspenseIcon> |
| 100 | <MicIcon className={`zoom__icon zoom__icon--${mic}`} /> |
| 101 | </SuspenseIcon> |
| 102 | )} |
| 103 | </DataWidget.Widget> |
| 104 | ); |
| 105 | }); |
| 106 | |
| 107 | Widget.displayName = "Zoom"; |