import { useEffect, useRef, useState } from 'react' import { useFloating, arrow, FloatingArrow, offset as offsetMiddleware, Placement } from '@floating-ui/react' import Button from './Button' interface Props extends React.ComponentProps { initialTooltip: { content: string placement?: Placement localStorageKey?: string | null offset?: number } alwaysTooltip?: string } const ARROW_HEIGHT = 7 const GAP = 0 export default ({ initialTooltip, alwaysTooltip, ...args }: Props) => { const { localStorageKey = 'firstTimeTooltip', offset = 0 } = initialTooltip const [showTooltips, setShowTooltips] = useState(alwaysTooltip || (localStorageKey ? localStorage[localStorageKey] !== 'false' : true)) useEffect(() => { let timeout function hide () { if (!localStorageKey) return localStorage[localStorageKey] = 'false' setShowTooltips(false) } if (showTooltips && localStorageKey) { // todo wait for user interaction mouseoe timeout = setTimeout(() => { hide() }, 10_000) } return () => { // unmounted, probably switched view if (timeout) clearTimeout(timeout) hide() } }, []) const arrowRef = useRef(null) const { refs, floatingStyles, context } = useFloating({ middleware: [ arrow({ element: arrowRef }), offsetMiddleware(ARROW_HEIGHT + GAP + offset), ], placement: initialTooltip.placement, }) return <>