:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(search): break re-render loop in Search.Root on AI homepage (#57800)
posthog[bot]def551a9c93a
diff --git a/frontend/src/lib/components/Search/Search.tsx b/frontend/src/lib/components/Search/Search.tsxindex a1432a8d553a..4de145996aa2 100644--- a/frontend/src/lib/components/Search/Search.tsx+++ b/frontend/src/lib/components/Search/Search.tsx@@ -72,6 +72,8 @@ const SETTINGS_THEME_ITEM_ID = '__settings_theme__'const SETTINGS_THEME_ITEM_QUERY = ['dark', 'light', 'theme', 'appearance']+const EMPTY_SUGGESTED_ITEMS: SearchItem[] = []+// ============================================================================// Hooks// ============================================================================@@ -251,19 +253,31 @@ function useDebouncedGroupedItems(const [stable, setStable] = useState(groupedItems)const prevSearchRef = useRef(searchValue)const searchJustChangedRef = useRef(false)+ const prevEnabledRef = useRef(enabled)+ // `stable` only stays in sync with `groupedItems` while `enabled` is true.+ // If we're disabled, or were disabled at any point (including initial+ // mount before async feature-flag hydration), `stable` lags. Track that+ // so the next enabled render bypasses `stable` and returns `groupedItems`+ // directly until the effect re-syncs — avoids a flash of stale results.+ const stableIsStaleRef = useRef(!enabled)if (searchValue !== prevSearchRef.current) {prevSearchRef.current = searchValuesearchJustChangedRef.current = true}+ if (enabled !== prevEnabledRef.current) {+ prevEnabledRef.current = enabled+ stableIsStaleRef.current = true+ }+useEffect(() => {if (!enabled) {- setStable(groupedItems)return}- if (searchJustChangedRef.current) {+ if (searchJustChangedRef.current || stableIsStaleRef.current) {searchJustChangedRef.current = false+ stableIsStaleRef.current = falsesetStable(groupedItems)return}@@ -271,7 +285,7 @@ function useDebouncedGroupedItems(return () => clearTimeout(timer)}, [groupedItems, enabled, searchValue])- if (!enabled || searchJustChangedRef.current) {+ if (!enabled || searchJustChangedRef.current || stableIsStaleRef.current) {return groupedItems}@@ -380,7 +394,7 @@ function SearchRoot({onAskAiClick,className = '',defaultSearchValue = '',- suggestedItems = [],+ suggestedItems = EMPTY_SUGGESTED_ITEMS,}: SearchRootProps): JSX.Element {const { allCategories, isSearching } = useValues(searchLogic({ logicKey }))const { setSearch } = useActions(searchLogic({ logicKey }))