From 8af172d527ee90a9b645674dc7d7de1ed4f6803f Mon Sep 17 00:00:00 2001 From: Quan HL Date: Tue, 17 Sep 2024 06:39:34 +0700 Subject: [PATCH 1/4] fixed null setting if remove active account --- src/storage/index.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/storage/index.ts b/src/storage/index.ts index d85bece..cb1790f 100644 --- a/src/storage/index.ts +++ b/src/storage/index.ts @@ -126,14 +126,16 @@ export const getActiveSettings = (): IAppSettings => { JSON.parse(str); const activeSettings = parsed.find((el) => el.active); - const decoded = { - active: activeSettings?.active, - decoded: JSON.parse( - Buffer.from(activeSettings?.encoded!, "base64").toString("utf-8") - ), - id: activeSettings?.id, - }; - return decoded as IAppSettings; + if (activeSettings) { + const decoded = { + active: activeSettings?.active, + decoded: JSON.parse( + Buffer.from(activeSettings?.encoded!, "base64").toString("utf-8") + ), + id: activeSettings?.id, + }; + return decoded as IAppSettings; + } } return {} as IAppSettings; }; From aae9fc40330556d3d8d7b653c9a37108f2cc1052 Mon Sep 17 00:00:00 2001 From: Quan HL Date: Tue, 17 Sep 2024 06:42:43 +0700 Subject: [PATCH 2/4] fixed null setting if remove active account --- src/storage/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/index.ts b/src/storage/index.ts index cb1790f..71f7bdf 100644 --- a/src/storage/index.ts +++ b/src/storage/index.ts @@ -130,7 +130,7 @@ export const getActiveSettings = (): IAppSettings => { const decoded = { active: activeSettings?.active, decoded: JSON.parse( - Buffer.from(activeSettings?.encoded!, "base64").toString("utf-8") + Buffer.from(activeSettings.encoded, "base64").toString("utf-8") ), id: activeSettings?.id, }; From 9d606185bf87d2516fedf129ebe8119e70c4db27 Mon Sep 17 00:00:00 2001 From: Quan HL Date: Tue, 17 Sep 2024 06:42:55 +0700 Subject: [PATCH 3/4] fixed null setting if remove active account --- src/storage/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/index.ts b/src/storage/index.ts index 71f7bdf..70d0ca8 100644 --- a/src/storage/index.ts +++ b/src/storage/index.ts @@ -132,7 +132,7 @@ export const getActiveSettings = (): IAppSettings => { decoded: JSON.parse( Buffer.from(activeSettings.encoded, "base64").toString("utf-8") ), - id: activeSettings?.id, + id: activeSettings.id, }; return decoded as IAppSettings; } From 9a7c8f0280ad88b8e5cd53fbf8fa43e9c492199f Mon Sep 17 00:00:00 2001 From: Quan HL Date: Tue, 17 Sep 2024 11:21:40 +0700 Subject: [PATCH 4/4] fix delete active account --- src/storage/index.ts | 14 ++++++++---- src/window/phone/index.tsx | 47 +++++++++++++++++++++----------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/storage/index.ts b/src/storage/index.ts index 70d0ca8..6531084 100644 --- a/src/storage/index.ts +++ b/src/storage/index.ts @@ -91,12 +91,18 @@ export const setActiveSettings = (id: number) => { export const deleteSettings = (id: number) => { const str = localStorage.getItem(SETTINGS_KEY); if (str) { - const parsed = JSON.parse(str); + const parsed = JSON.parse(str) as saveSettingFormat[]; - // for edit: - const newData = parsed.filter((el: saveSettingFormat) => el.id !== id); + const setting = parsed.find((s) => s.id === id); - localStorage.setItem(SETTINGS_KEY, JSON.stringify(newData)); + const newSettings = parsed.filter((el) => el.id !== id); + + // deleting active account, reassign active to next account + if (setting?.active && newSettings.length) { + newSettings[0].active = true; + } + + localStorage.setItem(SETTINGS_KEY, JSON.stringify(newSettings)); } }; diff --git a/src/window/phone/index.tsx b/src/window/phone/index.tsx index 9d777e9..c6b361a 100644 --- a/src/window/phone/index.tsx +++ b/src/window/phone/index.tsx @@ -120,7 +120,6 @@ export const Phone = forwardRef( ) => { const [inputNumber, setInputNumber] = useState(""); const [appName, setAppName] = useState(""); - const [isConfigured, setIsConfigured] = useState(false); const [callStatus, setCallStatus] = useState(SipConstants.SESSION_ENDED); const [sessionDirection, setSessionDirection] = useState(""); @@ -145,6 +144,7 @@ export const Phone = forwardRef( const sessionDirectionRef = useRef(sessionDirection); const sipUA = useRef(null); const timerRef = useRef(null); + const FetchUsertimerRef = useRef(null); const isRestartRef = useRef(false); const sipDomainRef = useRef(""); const sipUsernameRef = useRef(""); @@ -299,7 +299,7 @@ export const Phone = forwardRef( setStatus, startCallDurationCounter, toast, - setIsOnline + setIsOnline, ]); useEffect(() => { @@ -319,19 +319,9 @@ export const Phone = forwardRef( } else { createSipClient(); } - setIsConfigured(true); } else { - setIsConfigured(false); clientGoOffline(); } - fetchRegisterUser(); - getConferences() - ?.then(() => { - setShowConference(true); - }) - .catch(() => { - setShowConference(false); - }); }, [ sipDomain, sipUsername, @@ -405,14 +395,31 @@ export const Phone = forwardRef( } }, [status, setIsOnline, setIsSwitchingUserStatus]); + const clearFetchUserTimer = () => { + if (FetchUsertimerRef.current) { + clearInterval(FetchUsertimerRef.current); + FetchUsertimerRef.current = null; + } + }; + useEffect(() => { - const timer = setInterval(() => { - fetchRegisterUser(); - }, 10000); - return () => { - clearInterval(timer); - }; - }, []); + if (isAdvanceMode) { + // check conference aibility + getConferences() + .then(() => { + setShowConference(true); + }) + .catch(() => { + setShowConference(false); + }); + FetchUsertimerRef.current = setInterval(() => { + fetchRegisterUser(); + }, 10_000); + } else { + clearFetchUserTimer(); + setShowConference(false); + } + }, [isAdvanceMode]); useEffect(() => { if (showAccounts) { @@ -553,9 +560,7 @@ export const Phone = forwardRef( const handleSetActive = (id: number) => { setActiveSettings(id); setShowAccounts(false); - // fetchRegisterUser(); reload(); - // window.location.reload(); }; const handleClickOutside = (event: Event) => {