sandbox-web-ide/frontend/context/PreviewContext.tsx

45 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-07-23 17:30:35 -04:00
"use client"
2024-10-21 13:57:45 -06:00
import React, { createContext, useContext, useRef, useState } from "react"
import { ImperativePanelHandle } from "react-resizable-panels"
2024-07-23 17:30:35 -04:00
interface PreviewContextType {
2024-10-21 13:57:45 -06:00
isPreviewCollapsed: boolean
setIsPreviewCollapsed: React.Dispatch<React.SetStateAction<boolean>>
previewURL: string
setPreviewURL: React.Dispatch<React.SetStateAction<string>>
previewPanelRef: React.RefObject<ImperativePanelHandle>
2024-07-23 17:30:35 -04:00
}
2024-10-21 13:57:45 -06:00
const PreviewContext = createContext<PreviewContextType | undefined>(undefined)
2024-07-23 17:30:35 -04:00
2024-10-21 13:57:45 -06:00
export const PreviewProvider: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const [isPreviewCollapsed, setIsPreviewCollapsed] = useState(true)
const [previewURL, setPreviewURL] = useState<string>("")
const previewPanelRef = useRef<ImperativePanelHandle>(null)
2024-07-23 17:30:35 -04:00
return (
2024-10-21 13:57:45 -06:00
<PreviewContext.Provider
value={{
isPreviewCollapsed,
setIsPreviewCollapsed,
previewURL,
setPreviewURL,
previewPanelRef,
}}
>
2024-07-23 17:30:35 -04:00
{children}
</PreviewContext.Provider>
2024-10-21 13:57:45 -06:00
)
}
2024-07-23 17:30:35 -04:00
export const usePreview = () => {
2024-10-21 13:57:45 -06:00
const context = useContext(PreviewContext)
2024-07-23 17:30:35 -04:00
if (context === undefined) {
2024-10-21 13:57:45 -06:00
throw new Error("usePreview must be used within a PreviewProvider")
2024-07-23 17:30:35 -04:00
}
2024-10-21 13:57:45 -06:00
return context
}