Skip to content

Commit 9e49768

Browse files
committed
client: Initialize Notification component to track window activity
1 parent 73f40a9 commit 9e49768

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

src/v2/components/PotentialFireList.jsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ import FireList from './FireList.jsx'
3939
const ShouldNotify = {
4040
MIN_INTERVAL_SECONDS: 30,
4141
MAX_RECENT_NOTIFICATIONS: 2,
42-
MAX_RECENT_SECONDS: 5 * 60
42+
MAX_RECENT_SECONDS: 5 * 60,
43+
// XXX: Used to initialize Notification component to track window activity.
44+
NO_NOTIFICATION: {disableActiveWindow: true, title: ''}
4345
}
4446

4547
const TIMESTAMP_LIMIT = 2 * Duration.HOUR
@@ -60,7 +62,7 @@ export default function PotentialFireList(props) {
6062
const [indexOfOldFires, setIndexOfOldFires] = useState(-1)
6163
const [region, setRegion] = useState(null)
6264
const [shouldNotify, setShouldNotify] = useState(false)
63-
const [notification, setNotification] = useState(null)
65+
const [notification, setNotification] = useState(ShouldNotify.NO_NOTIFICATION)
6466

6567
const allFiresRef = useRef([])
6668
const eventSourceRef = useRef()
@@ -79,6 +81,10 @@ export default function PotentialFireList(props) {
7981
}, [])
8082

8183
const handleNotification = useCallback(() => {
84+
if (!shouldNotify) {
85+
return setNotification(ShouldNotify.NO_NOTIFICATION)
86+
}
87+
8288
const {current: allFires} = allFiresRef
8389
const fire = allFires[0]
8490
const {
@@ -97,16 +103,16 @@ export default function PotentialFireList(props) {
97103
const mru = notifiedFires.length > 0 ? notifiedFires[0].timestamp : 0
98104

99105
// At least MIN_INTERVAL_SECONDS between notifications.
100-
if (timestamp - mru > ShouldNotify.MIN_INTERVAL_SECONDS) {
101-
return
106+
if (timestamp - mru <= ShouldNotify.MIN_INTERVAL_SECONDS) {
107+
return setNotification(ShouldNotify.NO_NOTIFICATION)
102108
}
103109

104110
const timestampLimit = timestamp - ShouldNotify.MAX_RECENT_SECONDS
105111
const recentlyNotifiedFires = notifiedFires.filter((x) => x.timestamp > timestampLimit)
106112

107113
// At most MAX_RECENT_NOTIFICATIONS every MAX_RECENT_SECONDS.
108114
if (recentlyNotifiedFires.length >= ShouldNotify.MAX_RECENT_NOTIFICATIONS) {
109-
return
115+
return setNotification(ShouldNotify.NO_NOTIFICATION)
110116
}
111117

112118
// -------------------------------------------------------------------------
@@ -115,14 +121,15 @@ export default function PotentialFireList(props) {
115121

116122
setNotification({
117123
title: 'Potential fire',
124+
disableActiveWindow: true,
118125
options: {
119126
body: `Camera ${cameraName} facing ${cameraDir}`,
120127
icon: '/wildfirecheck/checkfire192.png',
121128
lang: 'en',
122129
tag: `${timestamp}`
123130
}
124131
})
125-
}, [])
132+
}, [shouldNotify])
126133

127134
const handleToggleAllFires = useCallback(() => {
128135
const shouldIncludeAllFires = !includesAllFires
@@ -169,15 +176,15 @@ export default function PotentialFireList(props) {
169176
allFires.sort((a, b) => b.sortId - a.sortId)
170177

171178
const first = allFires[0]
172-
if (shouldNotify && first != null && first.isRealTime) {
173-
if (first.timestamp === timestamp && first.cameraID === cameraID) {
179+
if (first != null) {
180+
if (first.isRealTime && first.timestamp === timestamp && first.cameraID === cameraID) {
174181
handleNotification()
175182
}
176183
}
177184

178185
updateFires(includesAllFires)
179186
}
180-
}, [handleNotification, includesAllFires, region, shouldNotify, updateFires])
187+
}, [handleNotification, includesAllFires, region, updateFires])
181188

182189
useEffect(() => {
183190
const searchParams = new URLSearchParams(window.location.search)
@@ -225,7 +232,7 @@ export default function PotentialFireList(props) {
225232

226233
return 0,
227234
<>
228-
{ shouldNotify && notification && <Notification disableActiveWindow {...notification}/> }
235+
<Notification {...notification}/>
229236
<FireList
230237
fires={fires}
231238
firesByKey={firesByKeyRef.current}

0 commit comments

Comments
 (0)