Skip to content

Commit 66f6c0b

Browse files
committed
fix: floor plan engine state
1 parent d1b1674 commit 66f6c0b

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

grafana/floor-panel/src/components/FloorPanel.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { useState } from 'react'
22
import { FloorPlanEngine } from '@archilogic/floor-plan-sdk'
33
import { PanelProps } from '@grafana/data'
44

@@ -10,10 +10,10 @@ import { FloorOptions } from '../types'
1010
interface Props extends PanelProps<FloorOptions> {}
1111
const HIGHLIGHT_COLOR: [number, number, number] = [100, 200, 100]
1212

13-
let floorPlan: FloorPlanEngine
14-
let isFloorPlanLoaded = false
15-
1613
export const FloorPanel: React.FC<Props> = props => {
14+
const [floorPlan, setFloorPlan] = useState(undefined as FloorPlanEngine)
15+
const [isFloorPlanLoaded, setIsFloorPlanLoaded] = useState(false)
16+
1717
const { id, token, nodeId, colorFrom, colorTo } = props.options
1818
const { data } = props
1919
const { ids, values } = getSeries(data)
@@ -39,8 +39,8 @@ export const FloorPanel: React.FC<Props> = props => {
3939
}
4040
}
4141
function handleEvents(fpe: FloorPlanEngine) {
42-
isFloorPlanLoaded = true
43-
floorPlan = fpe
42+
setIsFloorPlanLoaded(true)
43+
setFloorPlan(fpe)
4444
handleInputSourceData()
4545
handleSpaceId()
4646
}

utils/components/FloorPlan/FloorPlan.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { useState } from 'react'
22
import { FloorPlanEngine } from '@archilogic/floor-plan-sdk'
33
import './FloorPlan.css'
44

@@ -10,22 +10,22 @@ interface FloorOptions {
1010
onLoad?: (floorPlan: FloorPlanEngine) => void
1111
}
1212

13-
const FLOOR_PLAN_ID = `floor-plan-${new Date().getTime()}`
14-
let floorPlan: FloorPlanEngine
15-
1613
const FloorPanel: React.FC<FloorOptions> = props => {
14+
const [floorPlan, setFloorPlan] = useState(undefined as FloorPlanEngine);
15+
const floorPlanId = `floor-plan-${new Date().getTime()}`
1716
const tokenOptions = {
1817
publishableAccessToken: props.token
1918
}
2019

2120
const initFloorPlan = () => {
22-
const container = document.getElementById(FLOOR_PLAN_ID)
21+
const container = document.getElementById(floorPlanId)
2322
if (container) {
24-
if (!props.loaded) {
25-
floorPlan = new FloorPlanEngine({ container, options: props.startupOptions })
26-
floorPlan.loadScene(props.id, tokenOptions).then(() => {
23+
if (!props.loaded && !floorPlan) {
24+
const fpe = new FloorPlanEngine({ container, options: props.startupOptions })
25+
fpe.loadScene(props.id, tokenOptions).then(() => {
2726
if (props.onLoad) props.onLoad(floorPlan)
2827
})
28+
setFloorPlan(fpe)
2929
} else {
3030
if (props.onLoad) props.onLoad(floorPlan)
3131
}
@@ -35,7 +35,7 @@ const FloorPanel: React.FC<FloorOptions> = props => {
3535

3636
return (
3737
<div className="plan-wrapper">
38-
<div id={FLOOR_PLAN_ID} className="plan-container"></div>
38+
<div id={floorPlanId} className="plan-container"></div>
3939
</div>
4040
)
4141
}

0 commit comments

Comments
 (0)