1- import { FileCode2 , Maximize2 , X as XIcon } from "lucide-react" ;
21import { useEffect , useState } from "react" ;
32
43import { Button } from "@/components/ui/button" ;
5- import { cn } from "@/lib/utils " ;
4+ import { Icon } from "@/components/ui/icon " ;
65
7- import TooltipButton from "../Buttons/TooltipButton" ;
86import { FullscreenElement } from "../FullscreenElement" ;
97import CodeSyntaxHighlighter from "./CodeSyntaxHighlighter" ;
108
119interface CodeViewerProps {
1210 code : string ;
1311 language ?: string ;
1412 filename ?: string ;
15- showInlineContent ?: boolean ;
13+ fullscreen ?: boolean ;
14+ onClose ?: ( ) => void ;
1615}
1716
1817const DEFAULT_CODE_VIEWER_HEIGHT = 128 ;
@@ -21,28 +20,19 @@ const CodeViewer = ({
2120 code,
2221 language = "yaml" ,
2322 filename = "" ,
24- showInlineContent = true ,
23+ fullscreen = false ,
24+ onClose,
2525} : CodeViewerProps ) => {
26- const [ isFullscreen , setIsFullscreen ] = useState ( false ) ;
27- const shouldRenderInlineCode = showInlineContent || isFullscreen ;
26+ const [ isFullscreen , setIsFullscreen ] = useState ( fullscreen ) ;
27+
28+ const handleToggleFullscreen = ( ) => {
29+ if ( isFullscreen && onClose ) {
30+ onClose ( ) ;
31+ }
2832
29- const handleEnterFullscreen = ( ) => {
3033 setIsFullscreen ( ( prev ) => ! prev ) ;
3134 } ;
3235
33- const compactButton = (
34- < TooltipButton
35- type = "button"
36- variant = "outline"
37- size = "icon"
38- tooltip = "View YAML"
39- onClick = { handleEnterFullscreen }
40- aria-label = "View YAML"
41- >
42- < FileCode2 className = "size-4" />
43- </ TooltipButton >
44- ) ;
45-
4636 useEffect ( ( ) => {
4737 const handleEscapeKey = ( e : KeyboardEvent ) => {
4838 if ( isFullscreen && e . key === "Escape" ) {
@@ -61,54 +51,37 @@ const CodeViewer = ({
6151
6252 return (
6353 < FullscreenElement fullscreen = { isFullscreen } >
64- < div
65- className = { cn (
66- "flex flex-col transition-shadow duration-150" ,
67- shouldRenderInlineCode
68- ? "bg-slate-900 h-full rounded-md"
69- : "bg-transparent" ,
70- ) }
71- >
72- { shouldRenderInlineCode ? (
73- < div className = "flex items-center justify-between gap-2 bg-slate-800 sticky top-0 z-10 rounded-t-md px-3 py-2.5" >
74- < div className = "flex items-baseline gap-2" >
75- < span className = "font-semibold text-base text-secondary" >
76- { filename }
77- </ span >
78- < span className = "text-sm text-secondary" > (Read Only)</ span >
79- </ div >
80- < Button
81- type = "button"
82- variant = "ghost"
83- size = "icon"
84- onClick = { handleEnterFullscreen }
85- className = "text-gray-200 hover:text-white"
86- title = { isFullscreen ? "Exit fullscreen" : "View fullscreen" }
87- aria-label = { isFullscreen ? "Exit fullscreen" : "View fullscreen" }
88- >
89- { isFullscreen ? (
90- < XIcon className = "size-4" />
91- ) : (
92- < Maximize2 className = "size-4" />
93- ) }
94- </ Button >
54+ < div className = "flex flex-col transition-shadow duration-150 bg-slate-900 h-full rounded-md" >
55+ < div className = "flex items-center justify-between gap-2 bg-slate-800 sticky top-0 z-10 rounded-t-md px-3 py-2.5" >
56+ < div className = "flex items-baseline gap-2" >
57+ < span className = "font-semibold text-base text-secondary" >
58+ { filename }
59+ </ span >
60+ < span className = "text-sm text-secondary" > (Read Only)</ span >
9561 </ div >
96- ) : (
97- < div className = "flex" > { compactButton } </ div >
98- ) }
99- { shouldRenderInlineCode && (
100- < div className = "flex-1 relative" >
101- < div
102- className = "absolute inset-0 overflow-y-auto bg-slate-900"
103- style = { {
104- willChange : "transform" ,
105- minHeight : DEFAULT_CODE_VIEWER_HEIGHT ,
106- } }
107- >
108- < CodeSyntaxHighlighter code = { code } language = { language } />
109- </ div >
62+ < Button
63+ type = "button"
64+ variant = "ghost"
65+ size = "icon"
66+ onClick = { handleToggleFullscreen }
67+ className = "text-gray-200 hover:text-black"
68+ title = { isFullscreen ? "Exit fullscreen" : "View fullscreen" }
69+ aria-label = { isFullscreen ? "Exit fullscreen" : "View fullscreen" }
70+ >
71+ { isFullscreen ? < Icon name = "X" /> : < Icon name = "Maximize2" /> }
72+ </ Button >
73+ </ div >
74+ < div className = "flex-1 relative" >
75+ < div
76+ className = "absolute inset-0 overflow-y-auto bg-slate-900"
77+ style = { {
78+ willChange : "transform" ,
79+ minHeight : DEFAULT_CODE_VIEWER_HEIGHT ,
80+ } }
81+ >
82+ < CodeSyntaxHighlighter code = { code } language = { language } />
11083 </ div >
111- ) }
84+ </ div >
11285 </ div >
11386 </ FullscreenElement >
11487 ) ;
0 commit comments