From 7ca8026436fff5029a2eff8e9c76b2e3969608ee Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Sun, 8 Dec 2024 15:42:30 +0100 Subject: [PATCH] fix(permit_join): update to new permit join API --- src/components/navbar/StartStopJoinButton.tsx | 21 ++++++++++++++++--- src/types.ts | 3 ++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/components/navbar/StartStopJoinButton.tsx b/src/components/navbar/StartStopJoinButton.tsx index c23951bf3..356e1c77c 100644 --- a/src/components/navbar/StartStopJoinButton.tsx +++ b/src/components/navbar/StartStopJoinButton.tsx @@ -1,4 +1,4 @@ -import React, { RefObject, useState } from 'react'; +import React, { RefObject, useState, useEffect } from 'react'; import Button from '../button'; import cx from 'classnames'; import useComponentVisible from '../../hooks/useComponentVisible'; @@ -14,8 +14,8 @@ export function StartStopJoinButton({ devices, setPermitJoin, bridgeInfo }: Star const { t } = useTranslation(['navbar']); const { ref, isComponentVisible, setIsComponentVisible } = useComponentVisible(false); const [selectedRouter, setSelectedRouter] = useState({} as Device); - const { permit_join_timeout: permitJoinTimeout } = bridgeInfo; - const permitJoin = permitJoinTimeout > 0; + const [permitJoinTimeout, setPermitJoinTimeout] = useState(undefined); + const { permit_join: permitJoin, permit_join_end: permitJoinEnd } = bridgeInfo; const selectAndHide = (device: Device) => { setSelectedRouter(device); @@ -39,6 +39,21 @@ export function StartStopJoinButton({ devices, setPermitJoin, bridgeInfo }: Star const onBtnClick = () => { setPermitJoin(permitJoin ? 0 : 254, selectedRouter); }; + const updatePermitJoinTimeout = () => { + setPermitJoinTimeout(permitJoinEnd ? Math.round((permitJoinEnd - Date.now()) / 1000) : undefined); + }; + + useEffect(() => { + let interval; + updatePermitJoinTimeout(); + + if (permitJoinEnd) { + interval = setInterval(() => updatePermitJoinTimeout(), 1000); + } + + return () => clearInterval(interval); + }, [permitJoinEnd]); + const permitJoinTimer = ( <> {permitJoinTimeout ? ( diff --git a/src/types.ts b/src/types.ts index 57e7891cd..3abca493b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -97,7 +97,8 @@ export type BridgeState = 'online' | 'offline'; export interface BridgeInfo { config: Z2MConfig; config_schema: JSONSchema7; - permit_join_timeout: number; + permit_join: boolean; + permit_join_end: number | undefined; commit?: string; version?: string; zigbee_herdsman_converters: { version: string };