Skip to content

Commit 0cdb0f4

Browse files
authored
prevent flash jump of experiments by delaying display (#55508)
1 parent 4018ac2 commit 0cdb0f4

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/events/components/experiments/useShouldShowExperiment.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function useShouldShowExperiment(experimentKey: ExperimentNames | { key:
1111
}
1212

1313
const [showExperiment, setShowExperiment] = useState(false)
14+
const [experimentLoading, setExperimentLoading] = useState(true)
1415
const router = useRouter()
1516
const mainContext = useMainContext()
1617
const [isStaff, setIsStaff] = useState<boolean>(false)
@@ -28,6 +29,21 @@ export function useShouldShowExperiment(experimentKey: ExperimentNames | { key:
2829
}
2930
}, [])
3031

32+
useEffect(() => {
33+
// After 1.5 seconds, if the experiment logic hasn't resolved, force it to stop loading
34+
const timer = setTimeout(() => {
35+
if (experimentLoading) {
36+
setExperimentLoading(false)
37+
}
38+
}, 1500)
39+
return () => {
40+
clearTimeout(timer)
41+
if (experimentLoading) {
42+
setExperimentLoading(false)
43+
}
44+
}
45+
}, [experimentLoading])
46+
3147
useEffect(() => {
3248
const updateShouldShow = async () => {
3349
const isStaff = await getIsStaff()
@@ -40,6 +56,7 @@ export function useShouldShowExperiment(experimentKey: ExperimentNames | { key:
4056
router.query,
4157
),
4258
)
59+
setExperimentLoading(false)
4360
}
4461

4562
updateShouldShow()
@@ -52,5 +69,8 @@ export function useShouldShowExperiment(experimentKey: ExperimentNames | { key:
5269
}
5370
}, [experimentKey, router.locale, mainContext.currentVersion, router.query, isStaff])
5471

55-
return showExperiment
72+
return {
73+
showExperiment,
74+
experimentLoading,
75+
}
5676
}

src/frame/components/page-header/Header.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ export const Header = () => {
5353
const searchButtonRef = useRef<HTMLButtonElement>(null)
5454
const { initializeCTA } = useCTAPopoverContext()
5555

56-
const showNewSearch = useShouldShowExperiment(EXPERIMENTS.ai_search_experiment)
56+
const { showExperiment: showNewSearch, experimentLoading: newSearchLoading } =
57+
useShouldShowExperiment(EXPERIMENTS.ai_search_experiment)
5758
let SearchButton: JSX.Element | null = (
5859
<SearchBarButton
5960
isSearchOpen={isSearchOpen}
@@ -189,10 +190,10 @@ export const Header = () => {
189190
<div className="hide-sm border-left pl-3 d-flex flex-items-center">
190191
<VersionPicker />
191192
{/* In larger viewports, we want to show the search bar next to the version picker */}
192-
<div className={styles.displayOverLarge}>{SearchButton}</div>
193+
{!newSearchLoading && <div className={styles.displayOverLarge}>{SearchButton}</div>}
193194
</div>
194195
</div>
195-
{showNewSearch ? (
196+
{newSearchLoading ? null : showNewSearch ? (
196197
<HeaderSearchAndWidgets
197198
isSearchOpen={isSearchOpen}
198199
SearchButton={SearchButton}

0 commit comments

Comments
 (0)