Skip to content

State and persistence

Mosyn owns the live layout while a grid is mounted. Use useGridStore to observe that layout from application UI, then persist the tile data wherever your product stores user preferences.

Call useGridStore inside the shared provider to subscribe to the root grid’s live tile array.

SaveLayoutButton.tsx
import { useGridStore } from 'mosyn';
export function SaveLayoutButton() {
const tiles = useGridStore((state) => state.tiles);
return (
<button type="button" onClick={() => saveDashboardLayout(tiles ?? [])}>
Save layout
</button>
);
}

The value updates as the grid changes, so your save action always receives the current arrangement.

Most applications should persist the structural tile fields plus their own data payload. Keep rendered React content in application code rather than attempting to serialize it.

layout.ts
export type SavedTile = {
id: string;
x: number;
y: number;
w: number;
h: number;
data?: unknown;
};
export function toSavedTiles(tiles: Tile[]): SavedTile[] {
return tiles.map(({ id, x, y, w, h, data }) => ({ id, x, y, w, h, data }));
}

When loading a saved layout, combine the saved geometry with the renderer defined by your application.

tiles.tsx
export const tiles: Tile[] = savedTiles.map((saved) => ({
...saved,
renderer: tileRenderers[saved.id],
}));

Pass the container tile id as the second argument to useGridStore when you need the layout inside one nested grid.

WorkspaceSummary.tsx
const workspaceTiles = useGridStore((state) => state.tiles, 'workspace');

Use this for workspace-level counters, save actions, or summaries outside the container itself.

To discard the live layout and begin again with a fresh tile array, remount the provider with a new key.

Dashboard.tsx
const [version, setVersion] = useState(0);
const tiles = useMemo(() => createTiles(), [version]);
return (
<MosynManager key={version}>
<Mosyn tiles={tiles} />
<button type="button" onClick={() => setVersion((value) => value + 1)}>
Reset layout
</button>
</MosynManager>
);

This creates a new interaction manager and seeds the grid from the new tile array.