Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(visibleWidgets): fix visibleWidget tracking bug, cleanup #412

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ const BatteriesOverview = ({ componentMode = "full", pageSelectorPropsSetter }:

const { temperatureUnitToHumanReadable } = useAppStore()

useVisibilityNotifier({ widgetName: BOX_TYPES.BATTERIES, visible: !!(batteries && batteries.length) })
const hasValidData = !!(batteries && batteries.length)

useVisibilityNotifier({ widgetName: BOX_TYPES.BATTERIES, isVisible: hasValidData })

const sortedBatteries = sortBatteries(batteries ?? [])
const overviewBatteries = batteriesForOverview(sortedBatteries)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ import { ISize } from "@m2Types/generic/size"
import { AC_SOURCE, BOX_TYPES } from "../../../utils/constants/generic"
import { RELAY_FUNCTION } from "../../../utils/constants/devices/generators"

interface Props {
componentMode?: ComponentMode
pageSelectorPropsSetter?: (arg0: PageSelectorProps) => void
}

const DevicesOverview = ({ componentMode = "full", pageSelectorPropsSetter }: Props) => {
const { inverters } = useInverters()
const { instanceId: vebusInstanceId, vebusInverters } = useVebus()
Expand All @@ -51,7 +56,9 @@ const DevicesOverview = ({ componentMode = "full", pageSelectorPropsSetter }: Pr
componentMode
)

useVisibilityNotifier({ widgetName: BOX_TYPES.DEVICES, visible: !!boxes.length })
const hasValidData = !!boxes.length

useVisibilityNotifier({ widgetName: BOX_TYPES.DEVICES, isVisible: hasValidData })

if (!boxes.length) {
return null
Expand Down Expand Up @@ -186,9 +193,4 @@ const getAvailableDeviceBoxes = function (
return devices
}

interface Props {
componentMode?: ComponentMode
pageSelectorPropsSetter?: (arg0: PageSelectorProps) => void
}

export default observer(DevicesOverview)
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ const EnergyOverview: FC<Props> = ({ componentMode = "full", pageSelectorPropsSe
const boxes = useAvailableEnergyBoxes(compactBoxSize, componentMode)
const activeStyles = applyStyles(compactBoxSize, defaultBoxStyles)

// TODO: it seems that visibility logic can be improved since the energy component always has an overview box
useVisibilityNotifier({ widgetName: BOX_TYPES.ENERGY, visible: boxes.length > 0 })
const hasValidData = boxes.length > 0

useVisibilityNotifier({ widgetName: BOX_TYPES.ENERGY, isVisible: hasValidData })

if (!boxes.length) {
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ const EnvironmentOverview = ({ componentMode = "full", pageSelectorPropsSetter }
(sensor: any) => sensor.temperature || sensor.humidity || sensor.pressure
)

useVisibilityNotifier({ widgetName: BOX_TYPES.ENVIRONMENT, visible: sensorHasData })
const hasValidData = sensorHasData

useVisibilityNotifier({ widgetName: BOX_TYPES.ENVIRONMENT, isVisible: hasValidData })

if (componentMode === "compact") {
return (
Expand Down
4 changes: 3 additions & 1 deletion src/app/Marine2/components/boxes/Tanks/Tanks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const Tanks = ({ componentMode = "full", className }: Props) => {

const [boxSize, setBoxSize] = useState<ISize>({ width: 0, height: 0 })

useVisibilityNotifier({ widgetName: BOX_TYPES.TANKS, visible: !!filteredTanks.length })
const hasValidData = !!filteredTanks.length

useVisibilityNotifier({ widgetName: BOX_TYPES.TANKS, isVisible: hasValidData })

const gridRef = useRef<HTMLDivElement>(null)
const [orientation, setOrientation] = useState<ScreenOrientation>("vertical")
Expand Down
4 changes: 3 additions & 1 deletion src/app/Marine2/components/views/RootView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ const RootView = () => {
const visibleBoxes: JSX.Element[] = []
const hiddenBoxes: JSX.Element[] = []
for (const type of Object.values(BOX_TYPES)) {
const isVisible = visibleWidgetsStore.visibleElements.has(type)

const elem = getBoxByType(type)
if (!elem) continue

if (visibleWidgetsStore.visibleElements.has(type)) {
if (isVisible) {
visibleBoxes.push(elem)
} else {
hiddenBoxes.push(elem)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { useEffect } from "react"
import { notifyParams, useVisibleWidgetsStore } from "./VisibleWidgets.store"
import { VisibilityParams, useVisibleWidgetsStore } from "./VisibleWidgets.store"

export const useVisibilityNotifier = ({ widgetName, visible }: notifyParams) => {
export const useVisibilityNotifier = ({ widgetName, isVisible }: VisibilityParams) => {
const visibleWidgetsStore = useVisibleWidgetsStore()

useEffect(() => {
visibleWidgetsStore.notifyVisibility({ widgetName, visible })
visibleWidgetsStore.changeVisibility({ widgetName, isVisible })

return () => visibleWidgetsStore.notifyVisibility({ widgetName, visible: false })
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [visible, widgetName])
}, [widgetName, isVisible])
}
13 changes: 6 additions & 7 deletions src/app/Marine2/modules/MetricsWidgets/VisibleWidgets.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@ import { makeAutoObservable } from "mobx"
import { useMemo } from "react"
import { BOX_TYPES } from "../../utils/constants/generic"

export type notifyParams = {
export type VisibilityParams = {
widgetName: BOX_TYPES
visible: boolean
isVisible: boolean
}

export class VisibleWidgets {
// TODO: add order prop to have a consistent order of boxes
visibleElements: Set<BOX_TYPES> = new Set()

constructor() {
makeAutoObservable(this)
}

notifyVisibility(element: notifyParams) {
changeVisibility(element: VisibilityParams) {
let isDirty = false
if (element.visible && !this.visibleElements.has(element.widgetName)) {
if (element.isVisible && !this.visibleElements.has(element.widgetName)) {
this.visibleElements.add(element.widgetName)
isDirty = true
} else if (this.visibleElements.has(element.widgetName)) {
} else if (!element.isVisible && this.visibleElements.has(element.widgetName)) {
this.visibleElements.delete(element.widgetName)
isDirty = true
}
Expand All @@ -38,7 +37,7 @@ export class VisibleWidgets {
}

clearVisibleElements() {
this.visibleElements.clear()
this.visibleElements = new Set()
}
}

Expand Down
Loading