From 2318d75bc78b5ce5e1f46a902567d481db222bd9 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Wed, 18 Jan 2023 11:33:40 -0500 Subject: [PATCH] prettier --- src/video-grid/NewVideoGrid.tsx | 142 ++++++++++++++++++-------------- 1 file changed, 82 insertions(+), 60 deletions(-) diff --git a/src/video-grid/NewVideoGrid.tsx b/src/video-grid/NewVideoGrid.tsx index 003c0709..82c4b32e 100644 --- a/src/video-grid/NewVideoGrid.tsx +++ b/src/video-grid/NewVideoGrid.tsx @@ -9,45 +9,46 @@ interface Cell { /** * The item held by the slot containing this cell. */ - item: TileDescriptor + item: TileDescriptor; /** * Whether this cell is the first cell of the containing slot. */ - slot: boolean + slot: boolean; /** * The width, in columns, of the containing slot. */ - columns: number + columns: number; /** * The height, in rows, of the containing slot. */ - rows: number + rows: number; } interface Rect { - x: number - y: number - width: number - height: number + x: number; + y: number; + width: number; + height: number; } interface Tile extends Rect { - item: TileDescriptor - dragging: boolean + item: TileDescriptor; + dragging: boolean; } interface SlotsProps { - count: number + count: number; } /** * Generates a number of empty slot divs. */ const Slots: FC = memo(({ count }) => { - const slots = new Array(count) - for (let i = 0; i < count; i++) slots[i] =
- return <>{slots} -}) + const slots = new Array(count); + for (let i = 0; i < count; i++) + slots[i] =
; + return <>{slots}; +}); export const NewVideoGrid: FC = ({ items, children }) => { const slotGridRef = useRef(null); @@ -56,51 +57,71 @@ export const NewVideoGrid: FC = ({ items, children }) => { const slotRects = useMemo(() => { if (slotGridRef.current === null) return []; - const slots = slotGridRef.current.getElementsByClassName(styles.slot) - const rects = new Array(slots.length) + const slots = slotGridRef.current.getElementsByClassName(styles.slot); + const rects = new Array(slots.length); for (let i = 0; i < slots.length; i++) { - const slot = slots[i] as HTMLElement + const slot = slots[i] as HTMLElement; rects[i] = { x: slot.offsetLeft, y: slot.offsetTop, width: slot.offsetWidth, height: slot.offsetHeight, - } + }; } return rects; }, [items, gridBounds]); - const cells: Cell[] = useMemo(() => items.map(item => ({ - item, - slot: true, - columns: 1, - rows: 1, - })), [items]) + const cells: Cell[] = useMemo( + () => + items.map((item) => ({ + item, + slot: true, + columns: 1, + rows: 1, + })), + [items] + ); - const slotCells = useMemo(() => cells.filter(cell => cell.slot), [cells]) + const slotCells = useMemo(() => cells.filter((cell) => cell.slot), [cells]); - const tiles: Tile[] = useMemo(() => slotRects.flatMap((slot, i) => { - const cell = slotCells[i] - if (cell === undefined) return [] + const tiles: Tile[] = useMemo( + () => + slotRects.flatMap((slot, i) => { + const cell = slotCells[i]; + if (cell === undefined) return []; - return [{ - item: cell.item, - x: slot.x, - y: slot.y, - width: slot.width, - height: slot.height, - dragging: false, - }] - }), [slotRects, cells]) + return [ + { + item: cell.item, + x: slot.x, + y: slot.y, + width: slot.width, + height: slot.height, + dragging: false, + }, + ]; + }), + [slotRects, cells] + ); - const [tileTransitions] = useTransition(tiles, () => ({ - key: ({ item }: Tile) => item.id, - from: { opacity: 0 }, - enter: ({ x, y, width, height }: Tile) => ({ opacity: 1, x, y, width, height }), - update: ({ x, y, width, height }: Tile) => ({ x, y, width, height }), - leave: { opacity: 0 }, - }), [tiles]) + const [tileTransitions] = useTransition( + tiles, + () => ({ + key: ({ item }: Tile) => item.id, + from: { opacity: 0 }, + enter: ({ x, y, width, height }: Tile) => ({ + opacity: 1, + x, + y, + width, + height, + }), + update: ({ x, y, width, height }: Tile) => ({ x, y, width, height }), + leave: { opacity: 0 }, + }), + [tiles] + ); const slotGridStyle = useMemo(() => { const columnCount = gridBounds.width >= 800 ? 6 : 3; @@ -111,28 +132,29 @@ export const NewVideoGrid: FC = ({ items, children }) => { // Render nothing if the bounds are not yet known if (gridBounds.width === 0) { - return
- {/* It's important that we always attach slotGridRef to something, + return ( +
+ {/* It's important that we always attach slotGridRef to something, or else we may not receive the initial slot rects. */} -
-
+
+
+ ); } return ( -
+
- {tileTransitions((style, tile) => children({ - key: tile.item.id, - style: style as any, - width: tile.width, - height: tile.height, - item: tile.item, - }))} + {tileTransitions((style, tile) => + children({ + key: tile.item.id, + style: style as any, + width: tile.width, + height: tile.height, + item: tile.item, + }) + )}
); };