summaryrefslogtreecommitdiff
path: root/apps/web/src/hooks/use-menu-preview.ts
blob: a3c14489b1a2750a32081cd1596a129155fcafe8 (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
import { useRef } from "react";
import { useEditor } from "@/hooks/use-editor";

export function useMenuPreview() {
	const editor = useEditor();
	const didCommitRef = useRef(false);

	const discard = () => {
		if (!didCommitRef.current && editor.timeline.isPreviewActive()) {
			editor.timeline.discardPreview();
		}
	};

	const markCommitted = () => {
		didCommitRef.current = true;
	};

	const onOpenChange = (isOpen: boolean) => {
		if (!isOpen) {
			discard();
			didCommitRef.current = false;
		}
	};

	return { onPointerLeave: discard, onOpenChange, markCommitted };
}