summaryrefslogtreecommitdiff
path: root/apps/web/src/components/ui/use-overlay-open-change.ts
blob: c5ef5331fff6616df9d324373ab54bd41b32bffd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { useCallback, useEffect, useId, useRef } from "react";
import { useKeybindingsStore } from "@/stores/keybindings-store";

export function useOverlayOpenChange({
	source,
	open,
	onOpenChange,
}: {
	source: string;
	open?: boolean;
	onOpenChange?: (open: boolean) => void;
}) {
	const { openOverlay, closeOverlay } = useKeybindingsStore();
	const isTrackedRef = useRef(false);
	const isControlled = typeof open === "boolean";
	const overlayId = useId();

	useEffect(() => {
		if (!isControlled) return;

		if (open && !isTrackedRef.current) {
			openOverlay(overlayId, source);
			isTrackedRef.current = true;
			return;
		}

		if (!open && isTrackedRef.current) {
			closeOverlay(overlayId, source);
			isTrackedRef.current = false;
		}
	}, [closeOverlay, isControlled, open, openOverlay, overlayId, source]);

	useEffect(() => {
		return () => {
			if (!isTrackedRef.current) return;
			// #region agent log
			fetch(
				"http://127.0.0.1:7245/ingest/669b22f8-172b-4e65-aa3f-1c702ede83f7",
				{
					method: "POST",
					headers: {
						"Content-Type": "application/json",
						"X-Debug-Session-Id": "3997d9",
					},
					body: JSON.stringify({
						sessionId: "3997d9",
						runId: "post-fix",
						hypothesisId: "H2",
						location: "use-overlay-open-change.ts:cleanup",
						message: "Overlay closed during unmount cleanup",
						data: { source, overlayId },
						timestamp: Date.now(),
					}),
				},
			).catch(() => {});
			// #endregion
			closeOverlay(overlayId, source);
			isTrackedRef.current = false;
		};
	}, [closeOverlay, overlayId, source]);

	return useCallback(
		(nextOpen: boolean) => {
			if (!isControlled) {
				if (nextOpen && !isTrackedRef.current) {
					openOverlay(overlayId, source);
					isTrackedRef.current = true;
				} else if (!nextOpen && isTrackedRef.current) {
					closeOverlay(overlayId, source);
					isTrackedRef.current = false;
				}
			}

			onOpenChange?.(nextOpen);
		},
		[closeOverlay, isControlled, onOpenChange, openOverlay, overlayId, source],
	);
}