diff options
Diffstat (limited to 'users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/github.jsx')
| -rwxr-xr-x | users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/github.jsx | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/github.jsx b/users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/github.jsx new file mode 100755 index 0000000..89308b5 --- /dev/null +++ b/users/ryan/modules/simple-bar/simple-bar-source/lib/components/data/github.jsx | |||
| @@ -0,0 +1,105 @@ | |||
| 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 useWidgetRefresh from "../../hooks/use-widget-refresh.js"; | ||
| 6 | import useServerSocket from "../../hooks/use-server-socket.js"; | ||
| 7 | import { useSimpleBarContext } from "../simple-bar-context.jsx"; | ||
| 8 | import * as Utils from "../../utils.js"; | ||
| 9 | |||
| 10 | export { githubStyles as styles } from "../../styles/components/data/github.js"; | ||
| 11 | |||
| 12 | const { React } = Uebersicht; | ||
| 13 | |||
| 14 | const DEFAULT_REFRESH_FREQUENCY = 1000 * 60 * 10; | ||
| 15 | |||
| 16 | /** | ||
| 17 | * GitHub notification widget component | ||
| 18 | * @returns {JSX.Element|null} The GitHub notification widget component | ||
| 19 | */ | ||
| 20 | export const Widget = React.memo(() => { | ||
| 21 | const { displayIndex, settings } = useSimpleBarContext(); | ||
| 22 | const { widgets, githubWidgetOptions } = settings; | ||
| 23 | const { githubWidget } = widgets; | ||
| 24 | const { | ||
| 25 | refreshFrequency, | ||
| 26 | showOnDisplay, | ||
| 27 | hideWhenNoNotification, | ||
| 28 | notificationUrl, | ||
| 29 | ghBinaryPath, | ||
| 30 | showIcon, | ||
| 31 | } = githubWidgetOptions; | ||
| 32 | |||
| 33 | const isDisabled = React.useRef(false); | ||
| 34 | |||
| 35 | // Calculate the refresh frequency for the widget | ||
| 36 | const refresh = React.useMemo( | ||
| 37 | () => | ||
| 38 | Utils.getRefreshFrequency(refreshFrequency, DEFAULT_REFRESH_FREQUENCY), | ||
| 39 | [refreshFrequency], | ||
| 40 | ); | ||
| 41 | |||
| 42 | // Determine if the widget should be visible | ||
| 43 | const visible = | ||
| 44 | Utils.isVisibleOnDisplay(displayIndex, showOnDisplay) && githubWidget; | ||
| 45 | |||
| 46 | const [state, setState] = React.useState(); | ||
| 47 | const [loading, setLoading] = React.useState(visible); | ||
| 48 | |||
| 49 | /** | ||
| 50 | * Reset the widget state | ||
| 51 | */ | ||
| 52 | const resetWidget = () => { | ||
| 53 | setState(undefined); | ||
| 54 | setLoading(false); | ||
| 55 | }; | ||
| 56 | |||
| 57 | /** | ||
| 58 | * Fetch GPU data and update the widget state | ||
| 59 | */ | ||
| 60 | const getGitHub = React.useCallback(async () => { | ||
| 61 | if (!visible) return; | ||
| 62 | setLoading(true); | ||
| 63 | const result = await Utils.cachedRun( | ||
| 64 | `${ghBinaryPath} api notifications`, | ||
| 65 | refresh, | ||
| 66 | ); | ||
| 67 | if (!visible || isDisabled.current) { | ||
| 68 | return setLoading(false); | ||
| 69 | } | ||
| 70 | const json = JSON.parse(result); | ||
| 71 | const count = json.length; | ||
| 72 | setState({ count: count > 99 ? "99+" : count }); | ||
| 73 | setLoading(false); | ||
| 74 | }, [ghBinaryPath, visible, refresh]); | ||
| 75 | |||
| 76 | // Update the disabled state based on visibility | ||
| 77 | React.useEffect(() => { | ||
| 78 | isDisabled.current = !visible; | ||
| 79 | }, [visible]); | ||
| 80 | |||
| 81 | // Use server socket to fetch GPU data | ||
| 82 | useServerSocket("github", visible, getGitHub, resetWidget, setLoading); | ||
| 83 | // Use widget refresh hook to periodically refresh the widget | ||
| 84 | useWidgetRefresh(visible, getGitHub, refresh); | ||
| 85 | |||
| 86 | if (loading) return <DataWidgetLoader.Widget className="github" />; | ||
| 87 | if (!state) return null; | ||
| 88 | |||
| 89 | const { count } = state; | ||
| 90 | |||
| 91 | if (hideWhenNoNotification && count === 0) return null; | ||
| 92 | |||
| 93 | return ( | ||
| 94 | <DataWidget.Widget | ||
| 95 | classes="github" | ||
| 96 | href={notificationUrl} | ||
| 97 | Icon={showIcon ? Icons.GitHub : null} | ||
| 98 | onRightClick={getGitHub} | ||
| 99 | > | ||
| 100 | <span className="github__count">{count}</span> | ||
| 101 | </DataWidget.Widget> | ||
| 102 | ); | ||
| 103 | }); | ||
| 104 | |||
| 105 | Widget.displayName = "GitHub"; | ||
