22
33import { useCallback , useEffect , useMemo , useRef , useState } from 'react'
44import { createLogger } from '@sim/logger'
5- import { useParams } from 'next/navigation'
5+ import { useParams , useRouter } from 'next/navigation'
66import {
77 Button ,
88 Columns2 ,
@@ -123,7 +123,10 @@ export function Files() {
123123 const saveRef = useRef < ( ( ) => Promise < void > ) | null > ( null )
124124
125125 const params = useParams ( )
126+ const router = useRouter ( )
126127 const workspaceId = params ?. workspaceId as string
128+ const fileIdFromRoute =
129+ typeof params ?. fileId === 'string' && params . fileId . length > 0 ? params . fileId : null
127130 const userPermissions = useUserPermissionsContext ( )
128131
129132 const { data : files = [ ] , isLoading, error } = useWorkspaceFiles ( workspaceId )
@@ -157,7 +160,6 @@ export function Files() {
157160 const [ uploading , setUploading ] = useState ( false )
158161 const [ uploadProgress , setUploadProgress ] = useState ( { completed : 0 , total : 0 } )
159162 const [ searchTerm , setSearchTerm ] = useState ( '' )
160- const [ selectedFileId , setSelectedFileId ] = useState < string | null > ( null )
161163 const [ creatingFile , setCreatingFile ] = useState ( false )
162164 const [ isDirty , setIsDirty ] = useState ( false )
163165 const [ saveStatus , setSaveStatus ] = useState < SaveStatus > ( 'idle' )
@@ -178,8 +180,8 @@ export function Files() {
178180 } )
179181
180182 const selectedFile = useMemo (
181- ( ) => ( selectedFileId ? files . find ( ( f ) => f . id === selectedFileId ) : null ) ,
182- [ selectedFileId , files ]
183+ ( ) => ( fileIdFromRoute ? files . find ( ( f ) => f . id === fileIdFromRoute ) : null ) ,
184+ [ fileIdFromRoute , files ]
183185 )
184186
185187 const filteredFiles = useMemo ( ( ) => {
@@ -297,15 +299,15 @@ export function Files() {
297299 } )
298300 setShowDeleteConfirm ( false )
299301 setDeleteTargetFile ( null )
300- if ( selectedFileId === target . id ) {
302+ if ( fileIdFromRoute === target . id ) {
301303 setIsDirty ( false )
302304 setSaveStatus ( 'idle' )
303- setSelectedFileId ( null )
305+ router . push ( `/workspace/ ${ workspaceId } /files` )
304306 }
305307 } catch ( err ) {
306308 logger . error ( 'Failed to delete file:' , err )
307309 }
308- } , [ deleteTargetFile , workspaceId , selectedFileId ] )
310+ } , [ deleteTargetFile , workspaceId , fileIdFromRoute , router ] )
309311
310312 const handleSave = useCallback ( async ( ) => {
311313 if ( ! saveRef . current || ! isDirty || saveStatus === 'saving' ) return
@@ -317,9 +319,9 @@ export function Files() {
317319 setShowUnsavedChangesAlert ( true )
318320 } else {
319321 setPreviewMode ( 'editor' )
320- setSelectedFileId ( null )
322+ router . push ( `/workspace/ ${ workspaceId } /files` )
321323 }
322- } , [ isDirty ] )
324+ } , [ isDirty , router , workspaceId ] )
323325
324326 const handleStartHeaderRename = useCallback ( ( ) => {
325327 if ( selectedFile ) headerRename . startRename ( selectedFile . id , selectedFile . name )
@@ -391,8 +393,8 @@ export function Files() {
391393 setIsDirty ( false )
392394 setSaveStatus ( 'idle' )
393395 setPreviewMode ( 'editor' )
394- setSelectedFileId ( null )
395- } , [ ] )
396+ router . push ( `/workspace/ ${ workspaceId } /files` )
397+ } , [ router , workspaceId ] )
396398
397399 const handleCreateFile = useCallback ( async ( ) => {
398400 if ( creatingFile ) return
@@ -414,14 +416,14 @@ export function Files() {
414416 const fileId = result . file ?. id
415417 if ( fileId ) {
416418 justCreatedFileIdRef . current = fileId
417- setSelectedFileId ( fileId )
419+ router . push ( `/workspace/ ${ workspaceId } /files/ ${ fileId } ` )
418420 }
419421 } catch ( err ) {
420422 logger . error ( 'Failed to create file:' , err )
421423 } finally {
422424 setCreatingFile ( false )
423425 }
424- } , [ creatingFile , files , workspaceId ] )
426+ } , [ creatingFile , files , workspaceId , router ] )
425427
426428 const handleRowContextMenu = useCallback (
427429 ( e : React . MouseEvent , rowId : string ) => {
@@ -436,9 +438,9 @@ export function Files() {
436438
437439 const handleContextMenuOpen = useCallback ( ( ) => {
438440 if ( ! contextMenuFile ) return
439- setSelectedFileId ( contextMenuFile . id )
441+ router . push ( `/workspace/ ${ workspaceId } /files/ ${ contextMenuFile . id } ` )
440442 closeContextMenu ( )
441- } , [ contextMenuFile , closeContextMenu ] )
443+ } , [ contextMenuFile , closeContextMenu , router , workspaceId ] )
442444
443445 const handleContextMenuDownload = useCallback ( ( ) => {
444446 if ( ! contextMenuFile ) return
@@ -478,18 +480,19 @@ export function Files() {
478480 } , [ closeListContextMenu ] )
479481
480482 useEffect ( ( ) => {
481- const isJustCreated = selectedFileId != null && justCreatedFileIdRef . current === selectedFileId
483+ const isJustCreated =
484+ fileIdFromRoute != null && justCreatedFileIdRef . current === fileIdFromRoute
482485 if ( justCreatedFileIdRef . current && ! isJustCreated ) {
483486 justCreatedFileIdRef . current = null
484487 }
485488 if ( isJustCreated ) {
486489 setPreviewMode ( 'editor' )
487490 } else {
488- const file = selectedFileId ? filesRef . current . find ( ( f ) => f . id === selectedFileId ) : null
491+ const file = fileIdFromRoute ? filesRef . current . find ( ( f ) => f . id === fileIdFromRoute ) : null
489492 const canPreview = file ? isPreviewable ( file ) : false
490493 setPreviewMode ( canPreview ? 'preview' : 'editor' )
491494 }
492- } , [ selectedFileId ] )
495+ } , [ fileIdFromRoute ] )
493496
494497 useEffect ( ( ) => {
495498 if ( ! selectedFile ) return
@@ -597,13 +600,16 @@ export function Files() {
597600 handleDeleteSelected ,
598601 ] )
599602
600- if ( selectedFileId && ! selectedFile ) {
603+ if ( fileIdFromRoute && ! selectedFile ) {
601604 return (
602605 < div className = 'flex h-full flex-1 flex-col overflow-hidden bg-[var(--bg)]' >
603606 < ResourceHeader
604607 icon = { FilesIcon }
605608 breadcrumbs = { [
606- { label : 'Files' , onClick : ( ) => setSelectedFileId ( null ) } ,
609+ {
610+ label : 'Files' ,
611+ onClick : ( ) => router . push ( `/workspace/${ workspaceId } /files` ) ,
612+ } ,
607613 { label : '...' } ,
608614 ] }
609615 />
@@ -699,7 +705,9 @@ export function Files() {
699705 columns = { COLUMNS }
700706 rows = { rows }
701707 onRowClick = { ( id ) => {
702- if ( listRename . editingId !== id && ! headerRename . editingId ) setSelectedFileId ( id )
708+ if ( listRename . editingId !== id && ! headerRename . editingId ) {
709+ router . push ( `/workspace/${ workspaceId } /files/${ id } ` )
710+ }
703711 } }
704712 onRowContextMenu = { handleRowContextMenu }
705713 isLoading = { isLoading }
0 commit comments