({
className,
...attrs
}: ComponentProps<'section'>)
| 26 | const DWELL_MS = 4200; |
| 27 | |
| 28 | export const PlusFeedBanner = ({ |
| 29 | className, |
| 30 | ...attrs |
| 31 | }: ComponentProps<'section'>): ReactElement => { |
| 32 | const { logSubscriptionEvent } = usePlusSubscription(); |
| 33 | const isReducedMotion = usePrefersReducedMotion(); |
| 34 | const [selected, setSelected] = useState(plusBannerFeatures[0].id); |
| 35 | const [pinned, setPinned] = useState(false); |
| 36 | const [paused, setPaused] = useState(false); |
| 37 | const tabsRef = useRef<HTMLDivElement>(null); |
| 38 | const { ref: inViewRef, inView } = useInView({ threshold: 0.4 }); |
| 39 | const isPlaying = inView && !pinned && !paused && !isReducedMotion; |
| 40 | const uid = useId(); |
| 41 | const tabId = (id: string) => `${uid}-tab-${id.replace(/\s+/g, '-')}`; |
| 42 | const panelId = `${uid}-panel`; |
| 43 | const feature = |
| 44 | plusBannerFeatures.find((item) => item.id === selected) ?? |
| 45 | plusBannerFeatures[0]; |
| 46 | |
| 47 | useLogEventOnce(() => ({ |
| 48 | event_name: LogEvent.Impression, |
| 49 | target_type: TargetType.Plus, |
| 50 | target_id: TargetId.Scroll, |
| 51 | })); |
| 52 | |
| 53 | useEffect(() => { |
| 54 | if (!isPlaying) { |
| 55 | return undefined; |
| 56 | } |
| 57 | const id = window.setTimeout(() => { |
| 58 | setSelected((current) => { |
| 59 | const index = plusBannerFeatures.findIndex( |
| 60 | (item) => item.id === current, |
| 61 | ); |
| 62 | return plusBannerFeatures[(index + 1) % plusBannerFeatures.length].id; |
| 63 | }); |
| 64 | }, DWELL_MS); |
| 65 | return () => window.clearTimeout(id); |
| 66 | }, [isPlaying, selected]); |
| 67 | |
| 68 | useEffect(() => { |
| 69 | const tabs = tabsRef.current; |
| 70 | const tab = tabs?.querySelector<HTMLElement>(`[data-id="${selected}"]`); |
| 71 | if (!tabs || !tab || tabs.scrollWidth <= tabs.clientWidth) { |
| 72 | return; |
| 73 | } |
| 74 | tabs.scrollTo({ |
| 75 | left: tab.offsetLeft - 24, |
| 76 | behavior: isReducedMotion ? 'auto' : 'smooth', |
| 77 | }); |
| 78 | }, [selected, isReducedMotion]); |
| 79 | |
| 80 | const select = (id: string) => { |
| 81 | setPinned(true); |
| 82 | setSelected(id); |
| 83 | }; |
| 84 | |
| 85 | const pick = (id: string) => { |
nothing calls this directly
no test coverage detected