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

Function PatchyOffer

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

usePrefersReducedMotionFunction · 0.90
getPatchyConfigFunction · 0.90
leaveFunction · 0.70

Tested by

no test coverage detected