Skip to content

Commit

Permalink
feat: improve detail view toggling
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Feb 14, 2024
1 parent 5871284 commit 97ff0bc
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 114 deletions.
8 changes: 1 addition & 7 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { Provider as DevicesProvider } from './context/Devices.js'
import { Provider as LwM2MProvider } from './context/LwM2M.js'
import { Provider as MapProvider } from './context/Map.js'
import { Provider as SettingsProvider } from './context/Settings.js'
import { Provider as HistoryChartProvider } from './context/showHistoryChart.js'
import { Provider as WirepasTopologyProvider } from './context/showWirepasTopology.js'
import { Provider as VisibleDevicesProvider } from './context/VisibleDevices.js'
import { Provider as WebsocketProvider } from './context/WebsocketConnection.js'
import { Dashboard } from './Dashboard.js'
Expand All @@ -19,11 +17,7 @@ export const App = () => (
<WebsocketProvider>
<LwM2MProvider>
<MapProvider authHelper={authHelper}>
<HistoryChartProvider>
<WirepasTopologyProvider>
<Dashboard />
</WirepasTopologyProvider>
</HistoryChartProvider>
<Dashboard />
</MapProvider>
</LwM2MProvider>
</WebsocketProvider>
Expand Down
1 change: 0 additions & 1 deletion src/ConfigureCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export const ConfigureCode = ({
<button
type="button"
onClick={() => {
console.log({ device: device.id, deviceCode })
onCode(deviceCode)
update({
managementCodes: {
Expand Down
5 changes: 2 additions & 3 deletions src/DeviceList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
isWirepasGateway,
type GeoLocation,
} from './context/Devices.js'
import { useHistoryChart } from './context/showHistoryChart.js'
import { NRPlusGatewayTile } from './NRPlusGatewayTile.js'
import { WirepasGatewayTile } from './wirepas/WirepasGatewayTile.js'
import { useVisibleDevices } from './context/VisibleDevices.js'
import { useMap } from './context/Map.js'
import { showDetails } from './hooks/useDetails.js'

const DeviceState = styled.section`
color: var(--color-nordic-light-grey);
Expand Down Expand Up @@ -111,7 +111,6 @@ export const IssuerName = styled.dd`
`

export const DeviceList = () => {
const { show: showHistoryChart } = useHistoryChart()
const devicesToShow = useVisibleDevices()
const map = useMap()

Expand Down Expand Up @@ -162,7 +161,7 @@ export const DeviceList = () => {
<HistoryOnly
device={device}
onClick={() => {
showHistoryChart(device.id)
showDetails(device.id)
}}
/>
</li>
Expand Down
5 changes: 2 additions & 3 deletions src/NRPlusGatewayTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { PinTile } from './PinTile.js'
import { ConfigureCode } from './ConfigureCode.js'
import { useSettings } from './context/Settings.js'
import { cancelEvent } from './cancelEvent.js'
import { useHistoryChart } from './context/showHistoryChart.js'
import { hideDetails } from './hooks/useDetails.js'

export const NRPlusGatewayTile = ({
gateway,
Expand All @@ -43,7 +43,6 @@ export const NRPlusGatewayTile = ({
settings: { managementCodes },
} = useSettings()
const { lastUpdateTs } = useDevices()
const { hide: hideHistoryChart } = useHistoryChart()
const lastUpdateTime = lastUpdateTs(gateway.id) as number
const [configureCode, setConfigureCode] = useState<boolean>(false)
const { location } = gateway
Expand All @@ -61,7 +60,7 @@ export const NRPlusGatewayTile = ({
if (deviceLocation !== undefined) {
onCenter(deviceLocation)
}
hideHistoryChart()
hideDetails()
})}
>
<NRPlus class="icon" />
Expand Down
9 changes: 4 additions & 5 deletions src/Tracker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
type GeoLocation,
} from './context/Devices.js'
import { useSettings } from './context/Settings.js'
import { useHistoryChart } from './context/showHistoryChart.js'
import { DKIcon } from './icons/DKIcon.js'
import { SIMIcon } from './icons/SIMIcon.js'
import { SoftSIMIcon } from './icons/SoftSIMIcon.js'
Expand All @@ -34,6 +33,7 @@ import { sortLocations } from './sortLocations.js'
import { FuelGauge } from './FuelGauge.js'
import { removeOldLocation } from './removeOldLocation.js'
import { PinTile } from './PinTile.js'
import { showDetails } from './hooks/useDetails.js'

const StyledSIMIcon = styled(SIMIcon)`
width: 20px;
Expand All @@ -54,7 +54,6 @@ export const Tracker = ({
onCenter: (location: GeoLocation) => void
}) => {
const { lastUpdateTs } = useDevices()
const { toggle: toggleHistoryChart } = useHistoryChart()
const {
settings: { showUpdateWarning },
} = useSettings()
Expand Down Expand Up @@ -85,7 +84,7 @@ export const Tracker = ({
if (deviceLocation !== undefined) {
onCenter(deviceLocation)
}
toggleHistoryChart(device.id)
showDetails(device.id)
}}
>
<BoardIcon class="icon" />
Expand Down Expand Up @@ -135,14 +134,14 @@ export const Tracker = ({
<EnvironmentInfo
device={device}
onClick={() => {
toggleHistoryChart(device.id)
showDetails(device.id)
}}
/>
{state?.fg !== undefined && (
<FuelGauge
fg={state.fg}
onClick={() => {
toggleHistoryChart(device.id)
showDetails(device.id)
}}
/>
)}
Expand Down
7 changes: 3 additions & 4 deletions src/chart/DeviceHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { useRef } from 'preact/hooks'
import styled from 'styled-components'
import { colors } from '../colors.js'
import { useDevices, type Reading } from '../context/Devices.js'
import { useHistoryChart } from '../context/showHistoryChart.js'
import { HistoryChart } from './HistoryChart.js'
import type { Dataset } from './chartMath.js'
import type { LucideProps } from '../icons/lucide.js'
import type { Ref } from 'preact'
import { useDetails, hideDetails } from '../hooks/useDetails.js'

const chartBaseWidth = 0.6 // percent of window width
const chartBaseHeight = 0.5 // percent of window width
Expand Down Expand Up @@ -64,7 +64,7 @@ const findLowerLimit = (v: Array<Reading>): number =>
*/
export const DeviceHistory = () => {
const { devices } = useDevices()
const { deviceId } = useHistoryChart()
const deviceId = useDetails()

if (deviceId === undefined) return null

Expand Down Expand Up @@ -162,7 +162,6 @@ const IconWithText = styled.div`

const Chart = ({ charts: charts }: { charts: ChartInfo[] }) => {
const containerRef = useRef<HTMLDivElement>()
const { hide } = useHistoryChart()
const [width, height] = [
window.innerWidth * chartBaseWidth,
((window.innerHeight * chartBaseHeight) / 3) * charts.length,
Expand Down Expand Up @@ -203,7 +202,7 @@ const Chart = ({ charts: charts }: { charts: ChartInfo[] }) => {
/>
</ChartWithIcon>
))}
<Button type={'button'} onClick={() => hide()}>
<Button type={'button'} onClick={() => hideDetails()}>
<X />
</Button>
</ChartContainer>
Expand Down
44 changes: 0 additions & 44 deletions src/context/showHistoryChart.tsx

This file was deleted.

35 changes: 0 additions & 35 deletions src/context/showWirepasTopology.tsx

This file was deleted.

28 changes: 28 additions & 0 deletions src/hooks/useDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useEffect, useState } from 'preact/hooks'

export const showDetails = (deviceId: string): void =>
document.location.assign(new URL(`/#${deviceId}`, document.location.href))

export const hideDetails = (): void =>
document.location.assign(new URL('/#', document.location.href))

export const useDetails = (): string | undefined => {
const [deviceId, setDeviceId] = useState<string>()

useEffect(() => {
const listener = (ev: HashChangeEvent) => {
try {
setDeviceId(new URL(ev.newURL).hash.slice(1))
} catch (err) {
console.debug(`[useDetails]`, err)
}
}
window.addEventListener('hashchange', listener)

return () => {
window.removeEventListener('hashchange', listener)
}
}, [])

return deviceId
}
8 changes: 2 additions & 6 deletions src/wirepas/WirepasGatewayTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ import { ButtonPressDiff } from '../ButtonPress.js'
import { sum } from 'lodash-es'
import { formatId } from './formatId.js'
import { cancelEvent } from '../cancelEvent.js'
import { useHistoryChart } from '../context/showHistoryChart.js'
import { useWirepasTopology } from '../context/showWirepasTopology.js'
import { showDetails } from '../hooks/useDetails.js'

export const WirepasGatewayTile = ({
gateway,
Expand All @@ -59,8 +58,6 @@ export const WirepasGatewayTile = ({
const deviceLocation = rankedLocations[0]
const code = managementCodes[gateway.id]
const hasCode = code !== undefined
const { hide: hideHistoryChart } = useHistoryChart()
const { show: showTopology } = useWirepasTopology()
const [expandNodes, setExpandNodes] = useState<boolean>(false)

const nodes = Object.entries(gateway.state.nodes)
Expand All @@ -74,8 +71,7 @@ export const WirepasGatewayTile = ({
if (deviceLocation !== undefined) {
onCenter(deviceLocation)
}
hideHistoryChart()
showTopology(gateway.id)
showDetails(gateway.id)
})}
>
<FiveGMesh class="icon" />
Expand Down
9 changes: 3 additions & 6 deletions src/wirepas/WirepasTopology.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import type { Ref } from 'preact'
import { ChartContainer, Button } from '../chart/DeviceHistory.js'
import { isWirepasGateway, useDevices } from '../context/Devices.js'
import { useWirepasTopology } from '../context/showWirepasTopology.js'
import { parseWirepasNodes } from './parseWirepasNodes.js'
import { useRef } from 'preact/hooks'
import { WirepasTopologyDiagram } from './WirepasTopologyDiagram.js'
import { X } from 'lucide-preact'
import { useDetails, hideDetails } from '../hooks/useDetails.js'

export const WirepasTopology = () => {
const { deviceId, hide } = useWirepasTopology()
const deviceId = useDetails()
const { devices, type } = useDevices()
const containerRef = useRef<HTMLDivElement>()
console.log(deviceId)
if (deviceId === undefined) return null
const maybeDevice = devices[deviceId]
if (maybeDevice === undefined) return null
Expand All @@ -20,8 +19,6 @@ export const WirepasTopology = () => {

const parsedWirepasTopology = parseWirepasNodes(gw.state.nodes)

console.log(parsedWirepasTopology)

const [width, height] = [500, 500]

return (
Expand All @@ -33,7 +30,7 @@ export const WirepasTopology = () => {
connections={parsedWirepasTopology}
size={{ width: 500, height: 500 }}
/>
<Button type={'button'} onClick={() => hide()}>
<Button type={'button'} onClick={() => hideDetails()}>
<X />
</Button>
</ChartContainer>
Expand Down

0 comments on commit 97ff0bc

Please sign in to comment.