From 892acd2f595b417d6bf63563b9f47c6b0064bd63 Mon Sep 17 00:00:00 2001 From: J Francisco Rader Date: Fri, 5 Feb 2021 15:08:25 -0500 Subject: [PATCH] Fix warnings + Improve CallTimer --- phone/src/apps/contacts/hooks/useContacts.ts | 1 - .../apps/dialer/components/DialerNavBar.tsx | 4 -- phone/src/modal/components/CallModal.tsx | 5 +- phone/src/modal/components/CallTimer.tsx | 49 ++++++++----------- phone/src/modal/hooks/useCall.ts | 2 +- phone/src/os/nui-events/hooks/useNuiEvent.ts | 2 +- 6 files changed, 24 insertions(+), 39 deletions(-) diff --git a/phone/src/apps/contacts/hooks/useContacts.ts b/phone/src/apps/contacts/hooks/useContacts.ts index 15df383..780e993 100644 --- a/phone/src/apps/contacts/hooks/useContacts.ts +++ b/phone/src/apps/contacts/hooks/useContacts.ts @@ -2,7 +2,6 @@ import { useRecoilValue } from 'recoil'; import { contactsState } from './state'; import { ContactProps } from '../../../common/typings/contact'; -import { useCall } from '../../../modal/hooks/useCall'; import { useCallback } from 'react'; interface IUseContacts { diff --git a/phone/src/apps/dialer/components/DialerNavBar.tsx b/phone/src/apps/dialer/components/DialerNavBar.tsx index 3da86d9..6951e4f 100644 --- a/phone/src/apps/dialer/components/DialerNavBar.tsx +++ b/phone/src/apps/dialer/components/DialerNavBar.tsx @@ -1,11 +1,7 @@ import React, { ChangeEvent, useState } from 'react'; import { BottomNavigation, BottomNavigationAction } from '@material-ui/core'; -import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; -import { faHome } from '@fortawesome/free-solid-svg-icons'; -import AddCircleIcon from '@material-ui/icons/AddCircle'; import { Link } from 'react-router-dom'; import { makeStyles, Theme } from '@material-ui/core/styles'; -import PhoneCallbackIcon from '@material-ui/icons/PhoneCallback'; import PhoneIcon from '@material-ui/icons/Phone'; import { History } from '@material-ui/icons'; diff --git a/phone/src/modal/components/CallModal.tsx b/phone/src/modal/components/CallModal.tsx index ff33f4e..6603f33 100644 --- a/phone/src/modal/components/CallModal.tsx +++ b/phone/src/modal/components/CallModal.tsx @@ -1,7 +1,6 @@ -import React, { useState } from 'react'; -import { Button, Fab } from '@material-ui/core'; +import React from 'react'; +import { Fab } from '@material-ui/core'; import { AppWrapper } from '../../ui/components/AppWrapper'; -import { AppTitle } from '../../ui/components/AppTitle'; import { AppContent } from '../../ui/components/AppContent'; import { useCall } from '../hooks/useCall'; import CallIcon from '@material-ui/icons/Call'; diff --git a/phone/src/modal/components/CallTimer.tsx b/phone/src/modal/components/CallTimer.tsx index ceea576..724dca8 100644 --- a/phone/src/modal/components/CallTimer.tsx +++ b/phone/src/modal/components/CallTimer.tsx @@ -1,8 +1,26 @@ -import React, { useState, useCallback, useEffect } from 'react'; +import React, { useCallback, useEffect, useRef } from 'react'; import { useDuration } from '../hooks/useDuration'; function CallTimer({ isAccepted }) { const { duration, setDuration } = useDuration(); + const updatedTime = useRef({ ms: 0, s: 0, m: 0, h: 0 }); + + const run = useCallback(() => { + let newTime = { ...updatedTime.current }; + if (newTime.m >= 60) { + newTime = { ...newTime, h: newTime.h + 1, m: 0 }; + } + if (newTime.s >= 60) { + newTime = { ...newTime, m: newTime.m + 1, s: 0 }; + } + if (newTime.ms >= 100) { + newTime = { ...newTime, s: newTime.s + 1, ms: 0 }; + } + + newTime = { ...newTime, ms: newTime.ms + 1 }; + updatedTime.current = newTime; + setDuration(newTime); + }, [setDuration]); useEffect(() => { let timer; @@ -12,37 +30,10 @@ function CallTimer({ isAccepted }) { } else { clearInterval(timer); } - }, []); - - let updatedMs = duration.ms, - updatedS = duration.s, - updatedM = duration.m, - updatedH = duration.h; + }, [isAccepted, run]); if (!duration) return null; - const run = () => { - if (updatedM === 60) { - updatedH++; - updatedM = 0; - } - if (updatedS === 60) { - updatedM++; - updatedS = 0; - } - if (updatedMs === 100) { - updatedS++; - updatedMs = 0; - } - updatedMs++; - return setDuration({ - ms: updatedMs, - s: updatedS, - m: updatedM, - h: updatedH, - }); - }; - return (
window.removeEventListener(eventName, eventListener, opts); - }, [app, method, capture, passive, once]); + }, [app, method, capture, passive, once, currentState]); };