@@ -7,92 +7,91 @@ import { parseError } from "~/client/lib/errors";
77import { normalizeAbsolutePath } from "@zerobyte/core/utils" ;
88import { logger } from "~/client/lib/logger" ;
99
10+ function createPathPrefixFns ( basePath : string ) {
11+ return {
12+ strip ( path : string ) {
13+ if ( basePath === "/" ) return path ;
14+ if ( path === basePath ) return "/" ;
15+ if ( path . startsWith ( `${ basePath } /` ) ) return path . slice ( basePath . length ) ;
16+ return path ;
17+ } ,
18+ add ( displayPath : string ) {
19+ if ( basePath === "/" ) return displayPath ;
20+ if ( displayPath === "/" ) return basePath ;
21+ return `${ basePath } ${ displayPath } ` ;
22+ } ,
23+ } ;
24+ }
25+
1026type SnapshotTreeBrowserProps = FileBrowserUiProps & {
1127 repositoryId : string ;
1228 snapshotId : string ;
13- basePath ?: string ;
29+ queryBasePath ?: string ;
30+ displayBasePath ?: string ;
1431 pageSize ?: number ;
1532 enabled ?: boolean ;
1633 onSingleSelectionKindChange ?: ( kind : "file" | "dir" | null ) => void ;
1734} ;
1835
19- export const SnapshotTreeBrowser = ( {
20- repositoryId,
21- snapshotId,
22- basePath = "/" ,
23- pageSize = 500 ,
24- enabled = true ,
25- ...uiProps
26- } : SnapshotTreeBrowserProps ) => {
36+ export const SnapshotTreeBrowser = ( props : SnapshotTreeBrowserProps ) => {
37+ const {
38+ repositoryId,
39+ snapshotId,
40+ queryBasePath = "/" ,
41+ displayBasePath,
42+ pageSize = 500 ,
43+ enabled = true ,
44+ ...uiProps
45+ } = props ;
46+
2747 const { selectedPaths, onSelectionChange, onSingleSelectionKindChange, ...fileBrowserUiProps } = uiProps ;
2848 const queryClient = useQueryClient ( ) ;
29- const normalizedBasePath = normalizeAbsolutePath ( basePath ) ;
49+ const normalizedQueryBasePath = normalizeAbsolutePath ( queryBasePath ) ;
50+ const normalizedDisplayBasePath = normalizeAbsolutePath ( displayBasePath ?? normalizedQueryBasePath ) ;
3051
3152 const { data, isLoading, error } = useQuery ( {
3253 ...listSnapshotFilesOptions ( {
3354 path : { shortId : repositoryId , snapshotId } ,
34- query : { path : normalizedBasePath } ,
55+ query : { path : normalizedQueryBasePath } ,
3556 } ) ,
3657 enabled,
3758 } ) ;
3859
39- const stripBasePath = useCallback (
40- ( path : string ) : string => {
41- if ( normalizedBasePath === "/" ) return path ;
42- if ( path === normalizedBasePath ) return "/" ;
43- if ( path . startsWith ( `${ normalizedBasePath } /` ) ) {
44- return path . slice ( normalizedBasePath . length ) ;
45- }
46- return path ;
47- } ,
48- [ normalizedBasePath ] ,
49- ) ;
50-
51- const addBasePath = useCallback (
52- ( displayPath : string ) : string => {
53- if ( normalizedBasePath === "/" ) return displayPath ;
54- if ( displayPath === "/" ) return normalizedBasePath ;
55- return `${ normalizedBasePath } ${ displayPath } ` ;
56- } ,
57- [ normalizedBasePath ] ,
58- ) ;
60+ const displayPathFns = useMemo ( ( ) => createPathPrefixFns ( normalizedDisplayBasePath ) , [ normalizedDisplayBasePath ] ) ;
5961
6062 const displaySelectedPaths = useMemo ( ( ) => {
6163 if ( ! selectedPaths ) return undefined ;
6264
6365 const displayPaths = new Set < string > ( ) ;
6466 for ( const fullPath of selectedPaths ) {
65- displayPaths . add ( stripBasePath ( fullPath ) ) ;
67+ displayPaths . add ( displayPathFns . strip ( fullPath ) ) ;
6668 }
6769
6870 return displayPaths ;
69- } , [ selectedPaths , stripBasePath ] ) ;
71+ } , [ displayPathFns , selectedPaths ] ) ;
7072
7173 const fileBrowser = useFileBrowser ( {
7274 initialData : data ,
7375 isLoading,
74- fetchFolder : async ( path , offset = 0 ) => {
76+ fetchFolder : async ( displayPath , offset = 0 ) => {
7577 return await queryClient . ensureQueryData (
7678 listSnapshotFilesOptions ( {
7779 path : { shortId : repositoryId , snapshotId } ,
78- query : { path, offset : offset , limit : pageSize } ,
80+ query : { path : displayPathFns . add ( displayPath ) , offset : offset , limit : pageSize } ,
7981 } ) ,
8082 ) ;
8183 } ,
82- prefetchFolder : ( path ) => {
84+ prefetchFolder : ( displayPath ) => {
8385 void queryClient
8486 . prefetchQuery (
8587 listSnapshotFilesOptions ( {
8688 path : { shortId : repositoryId , snapshotId } ,
87- query : { path, offset : 0 , limit : pageSize } ,
89+ query : { path : displayPathFns . add ( displayPath ) , offset : 0 , limit : pageSize } ,
8890 } ) ,
8991 )
9092 . catch ( ( e ) => logger . error ( e ) ) ;
9193 } ,
92- pathTransform : {
93- strip : stripBasePath ,
94- add : addBasePath ,
95- } ,
94+ pathTransform : displayPathFns ,
9695 } ) ;
9796
9897 const displayPathKinds = useMemo ( ( ) => {
@@ -109,7 +108,7 @@ export const SnapshotTreeBrowser = ({
109108
110109 const nextFullPaths = new Set < string > ( ) ;
111110 for ( const displayPath of nextDisplayPaths ) {
112- nextFullPaths . add ( addBasePath ( displayPath ) ) ;
111+ nextFullPaths . add ( displayPathFns . add ( displayPath ) ) ;
113112 }
114113
115114 if ( onSingleSelectionKindChange ) {
@@ -127,7 +126,7 @@ export const SnapshotTreeBrowser = ({
127126
128127 onSelectionChange ( nextFullPaths ) ;
129128 } ,
130- [ onSelectionChange , addBasePath , onSingleSelectionKindChange , displayPathKinds ] ,
129+ [ displayPathFns , displayPathKinds , onSelectionChange , onSingleSelectionKindChange ] ,
131130 ) ;
132131
133132 return (
0 commit comments