110 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-05-08 23:52:08 -07:00
"use client";
import {
ChevronLeft,
ChevronRight,
2024-05-11 19:28:37 -07:00
Globe,
2024-05-13 14:38:14 -07:00
Link,
2024-05-08 23:52:08 -07:00
RotateCw,
TerminalSquare,
2024-05-09 22:16:56 -07:00
UnfoldVertical,
2024-05-08 23:52:08 -07:00
} from "lucide-react";
2024-05-13 14:38:14 -07:00
import { useRef } from "react";
import { toast } from "sonner";
2024-05-08 23:52:08 -07:00
2024-05-09 22:16:56 -07:00
export default function PreviewWindow({
collapsed,
open,
sandboxId,
2024-05-09 22:16:56 -07:00
}: {
collapsed: boolean;
open: () => void;
sandboxId: string;
2024-05-09 22:16:56 -07:00
}) {
2024-05-13 14:38:14 -07:00
const ref = useRef<HTMLIFrameElement>(null);
2024-05-08 23:52:08 -07:00
return (
<>
2024-05-09 22:16:56 -07:00
<div
className={`${
collapsed ? "h-full" : "h-10"
} select-none w-full flex gap-2`}
>
2024-05-11 19:28:37 -07:00
<div className="h-8 rounded-md px-3 bg-secondary flex items-center w-full justify-between">
<div className="text-xs">
Preview
<span className="inline-block ml-2 items-center font-mono text-muted-foreground">
2024-05-13 14:38:14 -07:00
localhost:3000
2024-05-11 19:28:37 -07:00
</span>
</div>
2024-05-08 23:52:08 -07:00
<div className="flex space-x-1 translate-x-1">
2024-05-09 22:16:56 -07:00
{collapsed ? (
<PreviewButton onClick={open}>
<UnfoldVertical className="w-4 h-4" />
</PreviewButton>
) : (
<>
2024-05-13 14:38:14 -07:00
{/* Todo, make this open inspector */}
<PreviewButton disabled onClick={() => {}}>
2024-05-09 22:16:56 -07:00
<TerminalSquare className="w-4 h-4" />
</PreviewButton>
2024-05-13 14:38:14 -07:00
<PreviewButton
onClick={() => {
navigator.clipboard.writeText(
`http://${sandboxId}.sandbox.ishaand.com`
);
toast.info("Copied preview link to clipboard");
}}
>
<Link className="w-4 h-4" />
2024-05-09 22:16:56 -07:00
</PreviewButton>
2024-05-13 14:38:14 -07:00
<PreviewButton
onClick={() => {
if (ref.current) {
ref.current.contentWindow?.location.reload();
}
}}
>
2024-05-09 22:16:56 -07:00
<RotateCw className="w-3 h-3" />
</PreviewButton>
</>
)}
2024-05-08 23:52:08 -07:00
</div>
</div>
</div>
2024-05-09 22:16:56 -07:00
{collapsed ? null : (
<div className="w-full grow rounded-md overflow-hidden bg-foreground">
<iframe
2024-05-13 14:38:14 -07:00
ref={ref}
width={"100%"}
height={"100%"}
2024-05-13 14:38:14 -07:00
src={`http://${sandboxId}.sandbox.ishaand.com`}
/>
</div>
2024-05-09 22:16:56 -07:00
)}
2024-05-08 23:52:08 -07:00
</>
);
}
2024-05-09 22:16:56 -07:00
function PreviewButton({
children,
2024-05-13 14:38:14 -07:00
disabled = false,
2024-05-09 22:16:56 -07:00
onClick,
}: {
children: React.ReactNode;
2024-05-13 14:38:14 -07:00
disabled?: boolean;
2024-05-09 22:16:56 -07:00
onClick: () => void;
}) {
return (
<div
2024-05-13 14:38:14 -07:00
className={`${
disabled ? "pointer-events-none opacity-50" : ""
} p-0.5 h-5 w-5 ml-0.5 flex items-center justify-center transition-colors bg-transparent hover:bg-muted-foreground/25 cursor-pointer rounded-sm`}
2024-05-09 22:16:56 -07:00
onClick={onClick}
>
{children}
</div>
);
}