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