Fixed grid not updating properly

This commit is contained in:
Jan Zípek 2022-09-03 22:28:25 +02:00
parent f077ce94bd
commit 3d37331fce
Signed by: kamen
GPG Key ID: A17882625B33AC31
4 changed files with 13 additions and 5 deletions

View File

@ -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(

View File

@ -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)

View File

@ -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(() => {

View File

@ -0,0 +1,2 @@
export const nextFrame = () =>
new Promise((resolve) => requestAnimationFrame(resolve))