918 B39 lines
Blame
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8import type {ReactNode} from 'react';
9
10import {useEffect, useState} from 'react';
11
12/**
13 * Hide children until the given timestamp.
14 */
15export function Delayed({
16 children,
17 hideUntil,
18}: {
19 children: ReactNode;
20 hideUntil: Date;
21}): JSX.Element {
22 const [visible, setVisible] = useState(false);
23 useEffect(() => {
24 const delay = hideUntil.getTime() - Date.now();
25 if (delay > 0) {
26 setVisible(false);
27 const timer = setTimeout(() => {
28 setVisible(true);
29 }, delay);
30 return () => clearTimeout(timer);
31 } else {
32 setVisible(true);
33 }
34 }, [hideUntil, setVisible]);
35
36 // Cast to JSX.Element to make testing-library happy.
37 return (visible ? children : null) as JSX.Element;
38}
39