106 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-05-26 17:28:52 -07:00
"use client"
2024-05-08 23:52:08 -07:00
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-26 17:28:52 -07:00
} from "lucide-react"
import { useRef, useState } 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,
src
2024-05-09 22:16:56 -07:00
}: {
2024-05-26 17:28:52 -07:00
collapsed: boolean
open: () => void
src: string
2024-05-09 22:16:56 -07:00
}) {
2024-05-26 17:28:52 -07:00
const ref = useRef<HTMLIFrameElement>(null)
const [iframeKey, setIframeKey] = useState(0)
2024-05-13 14:38:14 -07:00
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">
2024-05-26 17:28:52 -07:00
<div className="text-xs">Preview</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 */}
2024-05-25 20:13:31 -07:00
{/* <PreviewButton disabled onClick={() => {}}>
2024-05-09 22:16:56 -07:00
<TerminalSquare className="w-4 h-4" />
2024-05-25 20:13:31 -07:00
</PreviewButton> */}
2024-05-13 14:38:14 -07:00
<PreviewButton
onClick={() => {
navigator.clipboard.writeText(src)
2024-05-26 17:28:52 -07:00
toast.info("Copied preview link to clipboard")
2024-05-13 14:38:14 -07:00
}}
>
<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={() => {
2024-05-25 20:13:31 -07:00
// if (ref.current) {
// ref.current.contentWindow?.location.reload();
// }
2024-05-26 17:28:52 -07:00
setIframeKey((prev) => prev + 1)
2024-05-13 14:38:14 -07:00
}}
>
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-25 20:13:31 -07:00
key={iframeKey}
2024-05-13 14:38:14 -07:00
ref={ref}
width={"100%"}
height={"100%"}
src={src}
/>
</div>
2024-05-09 22:16:56 -07:00
)}
2024-05-08 23:52:08 -07:00
</>
2024-05-26 17:28:52 -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,
}: {
2024-05-26 17:28:52 -07:00
children: React.ReactNode
disabled?: boolean
onClick: () => void
2024-05-09 22:16:56 -07:00
}) {
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>
2024-05-26 17:28:52 -07:00
)
2024-05-09 22:16:56 -07:00
}