Skip to content

Commit

Permalink
feat(map): only show favorited devices
Browse files Browse the repository at this point in the history
Closes #46
  • Loading branch information
coderbyheart committed Nov 8, 2023
1 parent 93cc37d commit 778d4ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const FavButton = ({ id }: { id: string }) => {
}

const FavoriteSelector = () => {
const { devices, alias } = useDevices()
const { devices, alias, lastUpdateTs } = useDevices()
const {
settings: { favorites },
update,
Expand All @@ -165,6 +165,10 @@ const FavoriteSelector = () => {
return (
<ul class="list-group">
{Object.entries(devices)
.filter(
([, d]) =>
(lastUpdateTs(d.id) ?? 0) > Date.now() - 24 * 60 * 60 * 1000,
)
.sort(([id1], [id2]) => {
const i1 = favorites.indexOf(id1)
const i2 = favorites.indexOf(id2)
Expand Down
14 changes: 12 additions & 2 deletions src/map/DeviceLocations.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { useEffect } from 'preact/hooks'
import { useDevices } from '../context/Devices.js'
import { useMap } from '../context/Map.js'
import { useSettings } from '../context/Settings.js'

export const DeviceLocations = () => {
const { devices, alias } = useDevices()
const {
settings: { showFavorites, favorites },
} = useSettings()
const map = useMap()

useEffect(() => {
if (map === undefined) return
const hiddenDevices = Object.keys(devices).filter((deviceId) => {
if (!showFavorites) return false
return !favorites.includes(deviceId)
})
for (const [deviceId, { location, hiddenLocations }] of Object.entries(
devices,
)) {
Expand All @@ -18,11 +26,13 @@ export const DeviceLocations = () => {
deviceId,
deviceAlias: deviceAlias ?? deviceId,
location: l,
hidden: hiddenLocations?.[l.source] ?? false,
hidden:
hiddenDevices.includes(deviceId) ||
(hiddenLocations?.[l.source] ?? false),
})
}
}
}, [devices, map])
}, [devices, map, showFavorites, favorites])

return null
}

0 comments on commit 778d4ac

Please sign in to comment.