MCPcopy Create free account
hub / github.com/Roy3838/Observer / useAgentGridLayout

Function useAgentGridLayout

app/src/hooks/useAgentGridLayout.ts:66–196  ·  view source on GitHub ↗
(agentIds: string[])

Source from the content-addressed store, hash-verified

64}
65
66export function useAgentGridLayout(agentIds: string[]) {
67 // measureBeforeMount so `width` is never a wrong guess (e.g. desktop-sized
68 // default width used to size a brand-new tile on a phone). The reconcile
69 // effect below additionally waits for a positive `width` — see the comment
70 // there for why `mounted` alone isn't a strong enough guard.
71 const { width, containerRef, mounted } = useContainerWidth({ measureBeforeMount: true });
72 const [layout, setLayout] = useState<LayoutItem[]>(loadLayout);
73 const layoutRef = useRef(layout);
74 layoutRef.current = layout;
75
76 const cols = colsForWidth(width || 1);
77
78 // Below md every tile fills the row. The stored sizes are absolute pixels (half the viewport by
79 // default), which on a phone left each card a fraction of the column width.
80 const [isNarrow, setIsNarrow] = useState(
81 () => typeof window !== 'undefined' && window.matchMedia(NARROW_QUERY).matches
82 );
83 useEffect(() => {
84 const mq = window.matchMedia(NARROW_QUERY);
85 const onChange = (e: MediaQueryListEvent) => setIsNarrow(e.matches);
86 setIsNarrow(mq.matches);
87 mq.addEventListener('change', onChange);
88 return () => mq.removeEventListener('change', onChange);
89 }, []);
90
91 const persist = useCallback((next: LayoutItem[]) => {
92 setLayout(next);
93 try {
94 localStorage.setItem(LAYOUT_KEY, JSON.stringify(next));
95 } catch {
96 // best-effort persistence — a full quota or private-mode failure just
97 // means layout resets next reload, not worth surfacing to the user
98 }
99 }, []);
100
101 // Give any agent that doesn't have a tile yet a default one, sized in
102 // absolute pixels (clamped to the grid's current column count).
103 //
104 // Gated on a *real* positive `width`, not just `mounted`: the grid's
105 // wrapper is toggled with `hidden` (display:none) whenever the GetStarted
106 // hero is showing (e.g. every agent minimized), and ResizeObserver reports
107 // that as width 0. If this effect fired on that stale 0 the moment the
108 // grid became visible again — same commit, before ResizeObserver's async
109 // callback had delivered the real width — it would create a degenerate,
110 // zero/negative-size tile for whichever agent just got un-minimized. Width
111 // is a dependency here specifically so the effect retries once a real
112 // measurement lands instead of running once on bad data.
113 useEffect(() => {
114 if (!mounted || width <= 0) return;
115 const missing = agentIds.filter(id => !layoutRef.current.some(item => item.i === id));
116 if (missing.length === 0) return;
117
118 const currentCols = colsForWidth(width);
119 const target = defaultSizePx();
120 const { w, h } = calcWHRaw(positionParamsFor(width, currentCols), target.width, target.height);
121 const clampedW = Math.max(1, Math.min(w, currentCols));
122 const clampedH = Math.max(1, h);
123

Callers 1

AppContentFunction · 0.90

Calls 4

colsForWidthFunction · 0.85
defaultSizePxFunction · 0.85
positionParamsForFunction · 0.85
filterMethod · 0.80

Tested by

no test coverage detected