Fixed grid not updating properly
This commit is contained in:
parent
f077ce94bd
commit
3d37331fce
|
|
@ -12,7 +12,6 @@ export const DashboardGrid = () => {
|
||||||
<EditableBox
|
<EditableBox
|
||||||
box={b}
|
box={b}
|
||||||
key={b.id}
|
key={b.id}
|
||||||
boxes={boxes}
|
|
||||||
onPosition={(p) => {
|
onPosition={(p) => {
|
||||||
setBoxes((previous) =>
|
setBoxes((previous) =>
|
||||||
normalizeBoxes(
|
normalizeBoxes(
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,6 @@ import { BoxSettings } from '../../BoxSettings/BoxSettings'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
box: BoxDefinition
|
box: BoxDefinition
|
||||||
boxes: BoxDefinition[]
|
|
||||||
onPosition: (p: { x: number; y: number }) => void
|
onPosition: (p: { x: number; y: number }) => void
|
||||||
onResize: (p: { w: number; h: number }) => void
|
onResize: (p: { w: number; h: number }) => void
|
||||||
onEdit: (box: BoxDefinition) => void
|
onEdit: (box: BoxDefinition) => void
|
||||||
|
|
@ -22,12 +21,13 @@ type Props = {
|
||||||
|
|
||||||
export const EditableBox = ({
|
export const EditableBox = ({
|
||||||
box,
|
box,
|
||||||
boxes,
|
|
||||||
onPosition,
|
onPosition,
|
||||||
onResize,
|
onResize,
|
||||||
onEdit,
|
onEdit,
|
||||||
onRemove,
|
onRemove,
|
||||||
}: Props) => {
|
}: Props) => {
|
||||||
|
const { boxes } = useDashboardContext()
|
||||||
|
|
||||||
const [boxRef, setBoxRef] = useState<HTMLDivElement>()
|
const [boxRef, setBoxRef] = useState<HTMLDivElement>()
|
||||||
const refreshRef = useRef<() => void>(null)
|
const refreshRef = useRef<() => void>(null)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import {
|
||||||
import { parseDashboard } from '@/utils/dashboard/parseDashboard'
|
import { parseDashboard } from '@/utils/dashboard/parseDashboard'
|
||||||
import { useViewportSize } from '@/utils/hooks/useViewportSize'
|
import { useViewportSize } from '@/utils/hooks/useViewportSize'
|
||||||
import { intervalToRange } from '@/utils/intervalToRange'
|
import { intervalToRange } from '@/utils/intervalToRange'
|
||||||
|
import { nextFrame } from '@/utils/nextFrame'
|
||||||
import { ComponentChild, createContext } from 'preact'
|
import { ComponentChild, createContext } from 'preact'
|
||||||
import {
|
import {
|
||||||
StateUpdater,
|
StateUpdater,
|
||||||
|
|
@ -62,9 +63,15 @@ export const DashboardContextProvider = ({
|
||||||
const [boxes, setBoxes] = useState(dashboardContent?.boxes ?? [])
|
const [boxes, setBoxes] = useState(dashboardContent?.boxes ?? [])
|
||||||
|
|
||||||
const handleChange = useCallback(
|
const handleChange = useCallback(
|
||||||
(cb: (boxes: BoxDefinition[]) => BoxDefinition[]) => {
|
async (cb: (boxes: BoxDefinition[]) => BoxDefinition[]) => {
|
||||||
const newBoxes = cb(boxes)
|
const newBoxes = cb(boxes)
|
||||||
|
|
||||||
|
// Some nasty preact optimizations will result into the updated box
|
||||||
|
// not being updated unless we make this async. Probably because
|
||||||
|
// the modified box called this cb and it decided that it doesn't
|
||||||
|
// need to know the update result?
|
||||||
|
await nextFrame()
|
||||||
|
|
||||||
setBoxes(newBoxes)
|
setBoxes(newBoxes)
|
||||||
|
|
||||||
if (dashboard.data && dashboardContent) {
|
if (dashboard.data && dashboardContent) {
|
||||||
|
|
@ -77,7 +84,7 @@ export const DashboardContextProvider = ({
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[dashboard.data]
|
[dashboard.data, boxes, dashboardContent]
|
||||||
)
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
export const nextFrame = () =>
|
||||||
|
new Promise((resolve) => requestAnimationFrame(resolve))
|
||||||
Loading…
Reference in New Issue