MCPcopy Create free account
hub / github.com/dailydotdev/apps / PatchyOffer

Function PatchyOffer

packages/shared/src/features/patchy/PatchyOffer.tsx:49–256  ·  view source on GitHub ↗
({
  offer,
  anchorId,
  onAnswer,
  onQuiet,
}: PatchyOfferProps)

Source from the content-addressed store, hash-verified

47// while (paused while you hover it), and the ••• menu stops that kind of
48// suggestion or turns them all off.
49export const PatchyOffer = ({
50 offer,
51 anchorId,
52 onAnswer,
53 onQuiet,
54}: PatchyOfferProps): ReactElement | null => {
55 const prefersReducedMotion = usePrefersReducedMotion();
56 const [box, setBox] = useState<CSSProperties | null>(null);
57 const [isIn, setIsIn] = useState(false);
58 const [isLeaving, setIsLeaving] = useState(false);
59 const [isMenuOpen, setIsMenuOpen] = useState(false);
60 const [isHovered, setIsHovered] = useState(false);
61 const answerRef = useRef<{
62 answer: PatchyOfferAnswer;
63 button?: 'primary' | 'secondary';
64 } | null>(null);
65
66 useLayoutEffect(() => {
67 const anchor = document.getElementById(anchorId);
68 const rail = anchor?.closest('nav');
69 if (!anchor || !rail) {
70 onAnswer('ignore');
71 return;
72 }
73 const tab = anchor.getBoundingClientRect();
74 // Low in the rail (his own spot sits near the bottom) it opens upwards
75 // from his circle, higher up downwards, so it always stays on screen.
76 const isLow = tab.top + tab.height / 2 > window.innerHeight / 2;
77 setBox({
78 left: rail.getBoundingClientRect().right + GAP,
79 ...(isLow
80 ? { bottom: Math.max(GAP, window.innerHeight - tab.bottom - GAP) }
81 : { top: Math.max(GAP, tab.top - GAP) }),
82 zIndex: offer.isOverModal ? Z_INDEX_OVER_MODAL : Z_INDEX,
83 });
84 // eslint-disable-next-line react-hooks/exhaustive-deps
85 }, [anchorId, offer.id]);
86
87 useEffect(() => {
88 if (!box) {
89 return undefined;
90 }
91 const frame = requestAnimationFrame(() => setIsIn(true));
92 return () => cancelAnimationFrame(frame);
93 }, [box]);
94
95 const leave = (
96 answer: PatchyOfferAnswer,
97 button?: 'primary' | 'secondary',
98 ) => {
99 if (isLeaving) {
100 return;
101 }
102 answerRef.current = { answer, button };
103 setIsLeaving(true);
104 setIsIn(false);
105 };
106

Callers

nothing calls this directly

Calls 3

usePrefersReducedMotionFunction · 0.90
getPatchyConfigFunction · 0.90
leaveFunction · 0.70

Tested by

no test coverage detected