@@ -7,7 +7,6 @@ import { Box } from "ink";
77import React , { Fragment , forwardRef , memo , useEffect , useImperativeHandle , useMemo , useRef } from "react" ;
88
99import { useCodeTerminalSize } from "../hooks/useCodeTerminalSize" ;
10- import { useIsMounted } from "../hooks/useIsMounted" ;
1110
1211import { CodeContent } from "./CodeContent" ;
1312import { CodeExtendLine } from "./CodeExtendLine" ;
@@ -28,6 +27,7 @@ export type CodeViewProps<T> = {
2827 fileName ?: string | null ;
2928 fileLang ?: DiffHighlighterLang | string | null ;
3029 } ;
30+ file ?: File ;
3131 extendData ?: Record < string , { data : T } > ;
3232 width ?: number ;
3333 codeViewTheme ?: "light" | "dark" ;
@@ -115,12 +115,12 @@ CodeLine.displayName = "CodeLine";
115115/**
116116 * CodeViewContent - renders all code lines using terminal size from context
117117 */
118- const CodeViewContent = memo ( ( { file, theme } : { file : File ; theme : "light" | "dark" } ) => {
118+ const CodeViewContent = memo ( ( { file, theme, width } : { file : File ; theme : "light" | "dark" , width ?: number } ) => {
119119 const { useCodeContext } = useCodeViewContext ( ) ;
120120
121121 const enableHighlight = useCodeContext ( ( s ) => s . enableHighlight ) ;
122122
123- const { columns } = useCodeTerminalSize ( ) ;
123+ const { columns : _columns } = useCodeTerminalSize ( ) ;
124124
125125 // Calculate line number width based on max line number
126126 const lineNumWidth = useMemo ( ( ) => {
@@ -134,6 +134,8 @@ const CodeViewContent = memo(({ file, theme }: { file: File; theme: "light" | "d
134134 return Array . from ( { length : totalLines } , ( _ , i ) => i + 1 ) ;
135135 } , [ file . rawLength ] ) ;
136136
137+ const columns = width || _columns ;
138+
137139 if ( ! columns ) return null ;
138140
139141 return (
@@ -163,14 +165,13 @@ CodeViewContent.displayName = "CodeViewContent";
163165const InternalCodeView = < T , > (
164166 props : Omit < CodeViewProps < T > , "data" > & {
165167 file : File ;
166- isMounted : boolean ;
167168 wrapperRef ?: RefObject < DOMElement > ;
168169 }
169170) => {
170171 const {
171172 file,
173+ width : _width ,
172174 codeViewHighlight,
173- isMounted,
174175 wrapperRef,
175176 extendData,
176177 renderExtendLine,
@@ -188,8 +189,8 @@ const InternalCodeView = <T,>(
188189 const {
189190 id,
190191 setId,
191- mounted ,
192- setMounted ,
192+ width ,
193+ setWidth ,
193194 enableHighlight,
194195 setEnableHighlight,
195196 setExtendData,
@@ -205,8 +206,8 @@ const InternalCodeView = <T,>(
205206 setId ( fileId ) ;
206207 }
207208
208- if ( mounted !== isMounted ) {
209- setMounted ( isMounted ) ;
209+ if ( _width !== width ) {
210+ setWidth ( _width ) ;
210211 }
211212
212213 if ( codeViewHighlight !== enableHighlight ) {
@@ -229,10 +230,10 @@ const InternalCodeView = <T,>(
229230 setTabWidth ( codeViewTabWidth ) ;
230231 }
231232 } , [
233+ _width ,
232234 useCodeContext ,
233235 codeViewHighlight ,
234236 fileId ,
235- isMounted ,
236237 extendData ,
237238 renderExtendLine ,
238239 codeViewTabSpace ,
@@ -253,7 +254,7 @@ const InternalCodeView = <T,>(
253254 return (
254255 < CodeViewContext . Provider value = { value } >
255256 < Box data-component = "git-code-view" data-theme = { theme } data-version = { __VERSION__ } flexDirection = "column" >
256- < CodeViewContent file = { file } theme = { theme } />
257+ < CodeViewContent file = { file } theme = { theme } width = { _width } />
257258 </ Box >
258259 </ CodeViewContext . Provider >
259260 ) ;
@@ -262,42 +263,37 @@ const InternalCodeView = <T,>(
262263const MemoedInternalCodeView = memo ( InternalCodeView ) ;
263264
264265const CodeViewContainerWithRef = < T , > ( props : CodeViewProps < T > , ref : ForwardedRef < { getFileInstance : ( ) => File } > ) => {
265- const { registerHighlighter, data, codeViewTheme, width , ...restProps } = props ;
266+ const { registerHighlighter, data, codeViewTheme, file , ...restProps } = props ;
266267
267268 const domRef = useRef < DOMElement > ( null ) ;
268269
269270 const theme = codeViewTheme || "light" ;
270271
271- const file = useMemo ( ( ) => {
272+ const width = restProps . width ;
273+
274+ const finalFile = useMemo ( ( ) => {
275+ if ( file ) return file ;
272276 if ( data ) {
273277 return getFile ( data . content || "" , data . fileLang || "" , theme , data . fileName || "" ) ;
274278 }
275279 return null ;
276- } , [ data , theme ] ) ;
277-
278- const fileRef = useRef ( file ) ;
279-
280- if ( fileRef . current && fileRef . current !== file ) {
281- fileRef . current = file ;
282- }
283-
284- const isMounted = useIsMounted ( ) ;
280+ } , [ data , theme , file ] ) ;
285281
286282 useEffect ( ( ) => {
287- if ( ! file ) return ;
288- file . doRaw ( ) ;
289- } , [ file ] ) ;
283+ if ( ! finalFile ) return ;
284+ finalFile . doRaw ( ) ;
285+ } , [ finalFile ] ) ;
290286
291287 useEffect ( ( ) => {
292- if ( ! file ) return ;
288+ if ( ! finalFile ) return ;
293289 if ( props . codeViewHighlight ) {
294- file . doSyntax ( { registerHighlighter : registerHighlighter , theme : codeViewTheme } ) ;
290+ finalFile . doSyntax ( { registerHighlighter : registerHighlighter , theme : codeViewTheme } ) ;
295291 }
296- } , [ file , props . codeViewHighlight , codeViewTheme , registerHighlighter ] ) ;
292+ } , [ finalFile , props . codeViewHighlight , codeViewTheme , registerHighlighter ] ) ;
297293
298- useImperativeHandle ( ref , ( ) => ( { getFileInstance : ( ) => file } ) , [ file ] ) ;
294+ useImperativeHandle ( ref , ( ) => ( { getFileInstance : ( ) => finalFile } ) , [ finalFile ] ) ;
299295
300- if ( ! file ) return null ;
296+ if ( ! finalFile ) return null ;
301297
302298 return (
303299 < Box
@@ -307,11 +303,10 @@ const CodeViewContainerWithRef = <T,>(props: CodeViewProps<T>, ref: ForwardedRef
307303 flexShrink = { typeof width === "number" ? 0 : undefined }
308304 >
309305 < MemoedInternalCodeView
310- key = { file . fileName }
306+ key = { finalFile . getId ( ) }
311307 { ...restProps }
312308 wrapperRef = { domRef }
313- file = { file }
314- isMounted = { isMounted }
309+ file = { finalFile }
315310 codeViewTheme = { codeViewTheme }
316311 codeViewTabWidth = { props . codeViewTabWidth || "medium" }
317312 />
0 commit comments