Skip to content

Commit 5e2ea86

Browse files
committed
fix: add the RESUME event that was thrown after an axios 401 error. performs the auto login again
1 parent 9b146ee commit 5e2ea86

File tree

15 files changed

+86
-67
lines changed

15 files changed

+86
-67
lines changed

src/main/classes/controllers/AccountController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export class AccountController {
176176

177177
setAccountNethLinkBounds(nethlinkBounds: Electron.Rectangle | undefined): void {
178178
const account = store.store.account
179-
Log.info('MAIN PRESENCE BACK', account?.data?.mainPresence)
179+
Log.debug('MAIN PRESENCE BACK', account?.data?.mainPresence)
180180
const auth = store.store.auth
181181
if (account) {
182182
account!.nethlinkBounds = nethlinkBounds

src/main/lib/mainStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Store<T> {
5959

6060
updateStore(newState: T, from: string) {
6161
const diff = difference(Object.values(newState as any || {}), Object.values(this.store as any || {}))
62-
Log.info('STORE update shared store from', from, Object.keys(newState as any || {}))
62+
Log.debug('STORE update shared store from', from, Object.keys(newState as any || {}))
6363
if (diff.length > 0 || this.store === undefined) {
6464
this.store = Object.assign({}, newState)
6565
}

src/main/main.ts

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ function startup() {
8989
showLogin()
9090
})
9191

92+
ipcMain.on(IPC_EVENTS.RESUME, async (_event) => {
93+
Log.info('logout after resume event')
94+
onAppResume()
95+
})
96+
9297
getPermissions()
9398

9499
attachOnReadyProcess()
@@ -514,44 +519,44 @@ function attachPowerMonitor() {
514519
powerMonitor.on('suspend', onAppSuspend);
515520
powerMonitor.on('resume', onAppResume);
516521
powerMonitor.on('shutdown', onAppShutdown)
522+
}
517523

518-
async function onAppShutdown() {
519-
Log.info('APP POWER SHUTDOWN')
520-
await AppController.safeQuit()
521-
}
524+
async function onAppShutdown() {
525+
Log.info('APP POWER SHUTDOWN')
526+
await AppController.safeQuit()
527+
}
522528

523-
async function onAppSuspend() {
524-
store.saveToDisk()
525-
Log.info('APP POWER SUSPEND')
526-
}
529+
async function onAppSuspend() {
530+
store.saveToDisk()
531+
Log.info('APP POWER SUSPEND')
532+
}
527533

528-
async function onAppResume() {
529-
debouncer('onAppResume', async () => {
530-
const data = store.getFromDisk()
531-
store.updateStore(data, 'onAppResume')
532-
Log.info('APP POWER RESUME')
533-
let showNethlink = true
534-
if (store.store.account && NethLinkController.instance) {
535-
const isOpen = NethLinkController.instance.window?.isOpen()
536-
showNethlink = isOpen ?? true
537-
try {
538-
await PhoneIslandController.instance.logout()
539-
} catch (e) {
540-
Log.error('POWER RESUME ERROR on logout', e)
541-
}
542-
try {
543-
NethLinkController.instance.logout()
544-
} catch (e) {
545-
Log.error('POWER RESUME ERROR on logout', e)
546-
}
547-
const autoLoginResult = await AccountController.instance.autoLogin()
548-
if (autoLoginResult) {
549-
ipcMain.emit(IPC_EVENTS.LOGIN, undefined, { showNethlink })
550-
}
534+
async function onAppResume() {
535+
debouncer('onAppResume', async () => {
536+
const data = store.getFromDisk()
537+
store.updateStore(data, 'onAppResume')
538+
Log.info('APP POWER RESUME')
539+
let showNethlink = true
540+
if (store.store.account && NethLinkController.instance) {
541+
const isOpen = NethLinkController.instance.window?.isOpen()
542+
showNethlink = isOpen ?? true
543+
try {
544+
await PhoneIslandController.instance.logout()
545+
} catch (e) {
546+
Log.error('POWER RESUME ERROR on logout', e)
547+
}
548+
try {
549+
NethLinkController.instance.logout()
550+
} catch (e) {
551+
Log.error('POWER RESUME ERROR on logout', e)
551552
}
553+
const autoLoginResult = await AccountController.instance.autoLogin()
554+
if (autoLoginResult) {
555+
ipcMain.emit(IPC_EVENTS.LOGIN, undefined, { showNethlink })
556+
}
557+
}
552558

553-
}, 2000)
554-
}
559+
}, 2000)
555560
}
556561

557562
function changeNethlinkTheme() {

src/renderer/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const RequestStateComponent = () => {
6666
}, [account?.username, pageData?.page])
6767

6868
const loader = async () => {
69-
Log.info('check i18n initialization')
69+
Log.debug('check i18n initialization')
7070
let time = 0
7171
//I wait for the language to load or 200 milliseconds
7272
while (time < 20 && !i18next.isInitialized) {
@@ -142,7 +142,7 @@ const Layout = ({ theme, page }: { theme?: AvailableThemes, page?: PAGES }) => {
142142
export default function App() {
143143

144144
useInitialize(() => {
145-
Log.info('initialize i18n')
145+
Log.debug('initialize i18n')
146146
loadI18n()
147147
})
148148
return (

src/renderer/src/hooks/useAccount.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ export const useAccount = () => {
3030
if (device?.type === 'physical') {
3131
const devices = await NethVoiceAPI.AstProxy.extensions()
3232
_status = devices[device.id].status || 'offline'
33-
Log.info('update device', device?.id || 'ND', device?.type || 'ND', devices[device?.id || ''].status || 'ND', _status)
33+
Log.debug('update device', device?.id || 'ND', device?.type || 'ND', devices[device?.id || ''].status || 'ND', _status)
3434
}
3535
}
3636
setStatus(() => _status)
37-
Log.info('update device status', _status, device?.id, device?.type)
37+
Log.debug('update device status', _status, device?.id, device?.type)
3838
setIsCallsEnabled(() => !(_status === 'busy' || _status === 'ringing' || _status === 'offline'))
3939
} else {
4040
setStatus('offline')
@@ -48,7 +48,7 @@ export const useAccount = () => {
4848

4949
const updateAccountData = async () => {
5050
const me = await NethVoiceAPI.User.me()
51-
Log.info('phone-island-default-device-updated', me.default_device)
51+
Log.debug('phone-island-default-device-updated', me.default_device)
5252
const device = {
5353
type: me.default_device.type as AvailableDevices,
5454
id: me.default_device.id

src/renderer/src/hooks/usePhoneIsland.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ export const usePhoneIsland = () => {
3535
}) => {
3636
return new Promise<void>((resolve) => {
3737
const listener = () => {
38-
Log.info('D&W received', awaitEvent)
38+
Log.debug('D&W received', awaitEvent)
3939
timer && clearTimeout(timer)
4040
window.removeEventListener(awaitEvent, listener)
4141
resolve()
4242
}
4343
let timer = setTimeout(() => {
44-
Log.warning('D&W timeout', event)
44+
Log.debug('D&W timeout', event)
4545
window.removeEventListener(awaitEvent, listener)
4646
resolve()
4747
}, options?.timeout || 300)
48-
Log.info('D&W add event listener from', event, 'to', awaitEvent)
48+
Log.debug('D&W add event listener from', event, 'to', awaitEvent)
4949
window.addEventListener(awaitEvent, listener)
50-
Log.info('D&W Disaptch', event)
50+
Log.debug('D&W Disaptch', event)
5151
eventDispatch(event, options?.data)
5252
})
5353
}

src/renderer/src/hooks/usePhoneIslandEventListeners.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const usePhoneIslandEventListener = () => {
4141

4242
useEffect(() => {
4343
const a = getPhoneIslandSize(phoneIslandData)
44-
Log.info('state', phoneIslandData, a)
44+
Log.debug('state', phoneIslandData, a)
4545
setPhoneIslandSized(() => ({ ...a }))
4646
}, [phoneIslandData])
4747

@@ -53,7 +53,7 @@ export const usePhoneIslandEventListener = () => {
5353
...eventHandler(PHONE_ISLAND_EVENTS["phone-island-action-physical"], async (data) => {
5454
//const res = await GET(data.urlCallObject.url)
5555
window.electron.send(IPC_EVENTS.START_CALL_BY_URL, data.urlCallObject.url)
56-
Log.info('phone-island-action-physical', data.urlCallObject.url)
56+
Log.debug('phone-island-action-physical', data.urlCallObject.url)
5757
}),
5858
...eventHandler(PHONE_ISLAND_EVENTS["phone-island-call-ringing"], () => {
5959
setPhoneIslandData((p) => ({
@@ -244,7 +244,7 @@ export const usePhoneIslandEventListener = () => {
244244
...eventHandler(PHONE_ISLAND_EVENTS["phone-island-default-device-change"]),
245245
...eventHandler(PHONE_ISLAND_EVENTS["phone-island-default-device-changed"]),
246246
...eventHandler(PHONE_ISLAND_EVENTS["phone-island-default-device-updated"], (e) => {
247-
Log.info('"phone-island-default-device-updated', e)
247+
Log.debug('"phone-island-default-device-updated', e)
248248
window.electron.send(IPC_EVENTS.UPDATE_ACCOUNT)
249249
}), //update the status of device from server
250250
...eventHandler(PHONE_ISLAND_EVENTS["phone-island-detach"]),

src/renderer/src/pages/LoginPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export function LoginPage({ themeMode, handleRefreshConnection }: LoginPageProps
139139
if (errorsData?.generalError) {
140140
loginWindowHeight += LoginSizes.LOGIN_FAILURE
141141
}
142-
Log.info({ loginWindowHeight })
142+
Log.debug({ loginWindowHeight })
143143
setWindowHeight(loginWindowHeight)
144144
}
145145

src/renderer/src/pages/NethLinkPage.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { useAccount } from '@renderer/hooks/useAccount'
1414
import { Sidebar } from '@renderer/components/Modules/NethVoice/BaseModule/Sidebar'
1515
import { AvailableDevices } from '@shared/types'
1616
import { sendNotification } from '@renderer/utils'
17+
import { AxiosError } from 'axios'
1718

1819

1920
export interface NethLinkPageProps {
@@ -106,7 +107,7 @@ export function NethLinkPage({ handleRefreshConnection }: NethLinkPageProps) {
106107
}
107108

108109
async function loadData() {
109-
Log.info('update account')
110+
Log.debug('update account')
110111
NethVoiceAPI.fetchOperators().then((op) => {
111112
saveOperators(op)
112113
updateAccountData()
@@ -120,11 +121,19 @@ export function NethLinkPage({ handleRefreshConnection }: NethLinkPageProps) {
120121
}
121122

122123
async function reloadData() {
123-
Log.info('RELOAD DATA', isFetching.current)
124+
Log.debug('RELOAD DATA', isFetching.current)
124125
if (!isFetching.current) {
125126
isFetching.current = true
126-
NethVoiceAPI.Phonebook.getSpeeddials().then(saveSpeeddials)
127-
updateAccountData()
127+
NethVoiceAPI.Phonebook.getSpeeddials().then((s) => {
128+
saveSpeeddials(s)
129+
updateAccountData()
130+
}).catch((e: AxiosError) => {
131+
Log.warning(e)
132+
if (e.status === 401) {
133+
Log.error(e)
134+
window.electron.send(IPC_EVENTS.RESUME)
135+
}
136+
})
128137
}
129138
debouncer('speeddial-fetch', () => {
130139
isFetching.current = false

src/renderer/src/pages/PhoneIslandPage.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ export function PhoneIslandPage() {
3030

3131
useEffect(() => {
3232
debouncer('phoneisland-resize', () => {
33-
Log.info('RESIZE')
33+
Log.debug('RESIZE')
3434
resize(phoneIsalndSizes.size, state)
3535
}, 50)
3636
}, [phoneIsalndSizes, state])
3737

3838
useInitialize(() => {
39-
Log.info('INITIALIZE PHONE ISLAND BASE EVENTS')
39+
Log.debug('INITIALIZE PHONE ISLAND BASE EVENTS')
4040
loadPath.current = getI18nLoadPath()
4141

4242
window.electron.receive(IPC_EVENTS.LOGOUT, logout)
@@ -64,9 +64,9 @@ export function PhoneIslandPage() {
6464
})
6565

6666
window.electron.receive(IPC_EVENTS.CHANGE_DEFAULT_DEVICE, async (deviceInformationObject, force) => {
67-
Log.info('CHANGE_DEFAULT_DEVICE', { force, deviceInformationObject, })
67+
Log.debug('CHANGE_DEFAULT_DEVICE', { force, deviceInformationObject, })
6868
const changed = await NethVoiceAPI.User.default_device(deviceInformationObject, force)
69-
Log.info('CHANGE_DEFAULT_DEVICE', { changed })
69+
Log.debug('CHANGE_DEFAULT_DEVICE', { changed })
7070
if (changed) {
7171
eventDispatch(PHONE_ISLAND_EVENTS['phone-island-default-device-change'], { deviceInformationObject })
7272
}
@@ -77,7 +77,7 @@ export function PhoneIslandPage() {
7777
if (!isOnLogout.current && (lastSize.current?.w !== size.w || lastSize.current?.h !== size.h)) {
7878
lastSize.current = size
7979
const { view } = state
80-
Log.info(`RESIZE ${size.w}x${size.h} ${account?.username} ${view}`)
80+
Log.debug(`RESIZE ${size.w}x${size.h} ${account?.username} ${view}`)
8181
if (view === PhoneIslandView.KEYPAD || view === PhoneIslandView.TRANSFER || state.currentCall.transferring) {
8282
phoneIslandContainer.current?.children[1].setAttribute('style', 'height: calc(100vh + 40px); position: relative;')
8383
} else if (view === PhoneIslandView.CALL) {
@@ -103,7 +103,7 @@ export function PhoneIslandPage() {
103103

104104
useEffect(() => {
105105
if (account && Object.keys(eventsRef.current).length > 0 && !attachedListener.current) {
106-
Log.info(account?.username, 'attachd listeners', Object.keys(eventsRef.current).length)
106+
Log.debug(account?.username, 'attachd listeners', Object.keys(eventsRef.current).length)
107107
Object.entries(eventsRef.current).forEach(([phoneIslandEventName, callback]) => {
108108
window.addEventListener(phoneIslandEventName, callback)
109109
})
@@ -112,7 +112,7 @@ export function PhoneIslandPage() {
112112
}, [account, dataConfig])
113113

114114
const destroyAllListeners = () => {
115-
Log.info(account?.username, 'deattached listeners', Object.keys(eventsRef.current).length)
115+
Log.debug(account?.username, 'deattached listeners', Object.keys(eventsRef.current).length)
116116
Object.entries(eventsRef.current).forEach(([phoneIslandEventName, callback]) => {
117117
window.removeEventListener(phoneIslandEventName, callback)
118118
})

0 commit comments

Comments
 (0)