|
| 1 | +import { faClose, faPowerOff, faServer } from "@fortawesome/free-solid-svg-icons"; |
| 2 | +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; |
| 3 | +import { memo, type RefObject, useCallback, useContext, useMemo, useRef } from "react"; |
| 4 | +import { useTranslation } from "react-i18next"; |
| 5 | +import { Link } from "react-router"; |
| 6 | +import { ReadyState } from "react-use-websocket"; |
| 7 | +import { useShallow } from "zustand/react/shallow"; |
| 8 | +import { LOG_LEVELS_CMAP } from "../consts.js"; |
| 9 | +import { API_NAMES, API_URLS, useAppStore } from "../store.js"; |
| 10 | +import type { LogMessage } from "../types.js"; |
| 11 | +import { WebSocketApiRouterContext } from "../WebSocketApiRouterContext.js"; |
| 12 | +import Button from "./Button.js"; |
| 13 | +import ConfirmButton from "./ConfirmButton.js"; |
| 14 | +import SourceDot from "./SourceDot.js"; |
| 15 | + |
| 16 | +type NotificationProps = { |
| 17 | + log: LogMessage; |
| 18 | + onClick: (ref: RefObject<HTMLDivElement | null>) => void; |
| 19 | +}; |
| 20 | + |
| 21 | +type NotificationsProps = { sourceIdx: number; readyState: ReadyState }; |
| 22 | + |
| 23 | +const CONNECTION_STATUS = { |
| 24 | + [ReadyState.CONNECTING]: "text-info", |
| 25 | + [ReadyState.OPEN]: "text-success", |
| 26 | + [ReadyState.CLOSING]: "text-warning", |
| 27 | + [ReadyState.CLOSED]: "text-error", |
| 28 | + [ReadyState.UNINSTANTIATED]: "text-error", |
| 29 | +}; |
| 30 | + |
| 31 | +const Notification = memo(({ log, onClick }: NotificationProps) => { |
| 32 | + const alertRef = useRef<HTMLDivElement | null>(null); |
| 33 | + |
| 34 | + return ( |
| 35 | + <div ref={alertRef} className={`alert ${LOG_LEVELS_CMAP[log.level]} break-all gap-1 p-2 pe-0.5`} title={log.timestamp}> |
| 36 | + <span>{log.message}</span> |
| 37 | + <div className="justify-self-end"> |
| 38 | + <Button item={alertRef} onClick={onClick} className="btn btn-xs btn-square"> |
| 39 | + <FontAwesomeIcon icon={faClose} /> |
| 40 | + </Button> |
| 41 | + </div> |
| 42 | + </div> |
| 43 | + ); |
| 44 | +}); |
| 45 | + |
| 46 | +const SourceNotifications = memo(({ sourceIdx, readyState }: NotificationsProps) => { |
| 47 | + const { t } = useTranslation(["navbar", "common"]); |
| 48 | + const { sendMessage, transactionPrefixes } = useContext(WebSocketApiRouterContext); |
| 49 | + const notifications = useAppStore(useShallow((state) => state.notifications[sourceIdx])); |
| 50 | + const restartRequired = useAppStore(useShallow((state) => state.bridgeInfo[sourceIdx].restart_required)); |
| 51 | + const clearNotifications = useAppStore((state) => state.clearNotifications); |
| 52 | + |
| 53 | + const restart = useCallback(async () => await sendMessage(sourceIdx, "bridge/request/restart", ""), [sourceIdx, sendMessage]); |
| 54 | + const onNotificationClick = useCallback((ref: RefObject<HTMLDivElement | null>) => { |
| 55 | + if (ref?.current) { |
| 56 | + ref.current.className += " hidden"; |
| 57 | + } |
| 58 | + }, []); |
| 59 | + const onClearClick = useCallback(() => clearNotifications(sourceIdx), [sourceIdx, clearNotifications]); |
| 60 | + |
| 61 | + return ( |
| 62 | + <li> |
| 63 | + <details open={sourceIdx === 0}> |
| 64 | + <summary> |
| 65 | + <span title={`${sourceIdx} | ${t("transaction_prefix")}: ${transactionPrefixes[sourceIdx]}`}> |
| 66 | + {API_URLS.length > 1 ? ( |
| 67 | + <> |
| 68 | + <SourceDot idx={sourceIdx} /> {API_NAMES[sourceIdx]} |
| 69 | + </> |
| 70 | + ) : ( |
| 71 | + "Zigbee2MQTT" |
| 72 | + )} |
| 73 | + </span> |
| 74 | + <span className="ml-auto"> |
| 75 | + {restartRequired && ( |
| 76 | + <ConfirmButton |
| 77 | + className="btn btn-xs btn-square btn-error animate-pulse" |
| 78 | + onClick={restart} |
| 79 | + title={t("restart")} |
| 80 | + modalDescription={t("common:dialog_confirmation_prompt")} |
| 81 | + modalCancelLabel={t("common:cancel")} |
| 82 | + > |
| 83 | + <FontAwesomeIcon icon={faPowerOff} /> |
| 84 | + </ConfirmButton> |
| 85 | + )} |
| 86 | + <span title={`${t("websocket_status")}: ${ReadyState[readyState]}`}> |
| 87 | + <FontAwesomeIcon icon={faServer} className={CONNECTION_STATUS[readyState]} /> |
| 88 | + </span> |
| 89 | + </span> |
| 90 | + </summary> |
| 91 | + {notifications.map((log, idx) => ( |
| 92 | + <Notification key={`${idx}-${log.timestamp}`} log={log} onClick={onNotificationClick} /> |
| 93 | + ))} |
| 94 | + {notifications.length > 0 && ( |
| 95 | + <div className="flex flex-row justify-between mt-3 mb-1"> |
| 96 | + <Link to={`/logs/${sourceIdx}`} className="btn btn-sm btn-primary btn-outline" title={t("common:more")}> |
| 97 | + {t("common:more")} |
| 98 | + </Link> |
| 99 | + <Button className="btn btn-sm btn-error btn-outline" onClick={onClearClick} title={t("common:clear")}> |
| 100 | + {t("common:clear")} |
| 101 | + </Button> |
| 102 | + </div> |
| 103 | + )} |
| 104 | + </details> |
| 105 | + </li> |
| 106 | + ); |
| 107 | +}); |
| 108 | + |
| 109 | +const Notifications = memo(() => { |
| 110 | + const { t } = useTranslation("common"); |
| 111 | + const { readyStates } = useContext(WebSocketApiRouterContext); |
| 112 | + const clearAllNotifications = useAppStore((state) => state.clearAllNotifications); |
| 113 | + |
| 114 | + const sourceNotifications = useMemo( |
| 115 | + () => |
| 116 | + API_URLS.map((_v, idx) => ( |
| 117 | + // biome-ignore lint/suspicious/noArrayIndexKey: static |
| 118 | + <SourceNotifications key={`${idx}`} sourceIdx={idx} readyState={readyStates[idx]} /> |
| 119 | + )), |
| 120 | + [readyStates], |
| 121 | + ); |
| 122 | + |
| 123 | + return ( |
| 124 | + <aside className="bg-base-100 min-h-screen w-80"> |
| 125 | + <ul className="menu w-full px-1 py-0"> |
| 126 | + {sourceNotifications} |
| 127 | + {API_URLS.length > 1 && ( |
| 128 | + <ConfirmButton |
| 129 | + className="btn btn-sm btn-error btn-outline mt-5" |
| 130 | + onClick={clearAllNotifications} |
| 131 | + title={t("clear_all")} |
| 132 | + modalDescription={t("dialog_confirmation_prompt")} |
| 133 | + modalCancelLabel={t("cancel")} |
| 134 | + > |
| 135 | + {t("clear_all")} |
| 136 | + </ConfirmButton> |
| 137 | + )} |
| 138 | + </ul> |
| 139 | + </aside> |
| 140 | + ); |
| 141 | +}); |
| 142 | + |
| 143 | +export default Notifications; |
0 commit comments