Saved layout
Persist the structural parts of each tile, then reattach application renderers when you load the layout.
Save tile geometry
Section titled “Save tile geometry”import type { Tile } from 'mosyn';
export type SavedTile = Pick<Tile, 'id' | 'x' | 'y' | 'w' | 'h' | 'data' | 'parentId'>;
export function serializeTiles(tiles: Tile[]): SavedTile[] { return tiles.map(({ id, x, y, w, h, data, parentId }) => ({ id, x, y, w, h, data, parentId, }));}Save the live root layout
Section titled “Save the live root layout”import { useGridStore } from 'mosyn';import { serializeTiles } from './layout';
export function SaveLayoutButton() { const tiles = useGridStore((state) => state.tiles);
const save = async () => { await fetch('/api/dashboard-layout', { method: 'PUT', headers: { 'content-type': 'application/json' }, body: JSON.stringify(serializeTiles(tiles ?? [])), }); };
return <button type="button" onClick={save}>Save layout</button>;}Reattach renderers when loading
Section titled “Reattach renderers when loading”import type { Tile } from 'mosyn';import type { SavedTile } from './layout';
const renderers: Record<string, Tile['renderer']> = { overview: <Overview />, activity: <Activity />, alerts: <Alerts />,};
export function hydrateTiles(savedTiles: SavedTile[]): Tile[] { return savedTiles.map((tile) => ({ ...tile, renderer: renderers[tile.id], }));}React elements are application UI, not saved-layout data. Rebuild them from the tile id and your product’s current capabilities.