import {
memo,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
type FC
} from "react";
import StyledPeekWindow from "./StyledPeekWindow";
import usePeekTransition from "./usePeekTransition";
import useWindowPeek from "./useWindowPeek";
import { CloseIcon } from "../../../Window/Titlebar/WindowActionIcons";
import useWindowActions from "../../../Window/Titlebar/useWindowActions";
import { useWindows } from "@/contexts/windows";
import { useSession } from "@/contexts/session";
import Button from "@/styles/common/Button";
import { FOCUSABLE_ELEMENT, HIGH_PRIORITY_ELEMENT } from "@/utils/constants";
import { haltEvent, label, viewWidth } from "@/utils/functions";
type PeekWindowProps = {
id: string;
};
const Pause = memo(() => (
));
const Play = memo(() => (
));
const PeekWindow: FC = ({ id }) => {
const {
minimize,
windows: { [id]: currentWindow },
} = useWindows();
const { pause, paused, play, minimized = false, title = id } = currentWindow || {};
const { setForegroundId, setPreviewID } = useSession();
const { onClose } = useWindowActions(id);
const [offsetX, setOffsetX] = useState(0);
const image = useWindowPeek(id);
const showControls = useMemo(
() => Boolean(play && pause && paused),
[pause, paused, play]
);
const peekTransition = usePeekTransition(showControls);
const peekRef = useRef(null);
const onClick = (): void => {
if (minimized) minimize(id);
setPreviewID(false);
setForegroundId(id);
};
const [isPaused, setIsPaused] = useState(false);
const monitoringPaused = useRef(false);
useEffect(() => {
if (showControls && paused && !monitoringPaused.current) {
monitoringPaused.current = true;
setIsPaused(paused(setIsPaused));
}
}, [paused, showControls]);
useLayoutEffect(() => {
if (image) {
const { left = 0, right = 0 } =
peekRef.current?.getBoundingClientRect() || {};
const vw = viewWidth();
if (left < 0) {
setOffsetX(Math.abs(left));
} else if (right > vw) {
setOffsetX(vw - right);
}
}
}, [image]);
return image ? (
{showControls && (
{isPaused && (
)}
{!isPaused && (
)}
)}
) : null;
};
export default memo(PeekWindow);