@@ -9,6 +9,24 @@ import {
99
1010import DriveContents from "../../drive-contents" ;
1111
12+ async function getAllParents ( folderId : number ) {
13+ const parents = [ ] ;
14+ let currentId : number | null = folderId ;
15+ while ( currentId != null ) {
16+ const folders = await db
17+ . selectDistinct ( )
18+ . from ( folderSchema )
19+ . where ( eq ( folderSchema . id , currentId ) ) ;
20+
21+ if ( ! folders [ 0 ] ) throw new Error ( "parent folder not found" ) ;
22+
23+ parents . unshift ( folders [ 0 ] ) ;
24+ currentId = folders [ 0 ] ?. parent ; // parent can be null
25+ }
26+
27+ return parents ;
28+ }
29+
1230export default async function GoogleDriveClone ( props : {
1331 params : Promise < { folderId : number } > ;
1432} ) {
@@ -20,16 +38,22 @@ export default async function GoogleDriveClone(props: {
2038 if ( ! success ) return < div > Invalid Folder ID</ div > ;
2139
2240 const folderId = data . folderId ;
23- console . log ( folderId ) ;
41+ const parentsPromise = getAllParents ( folderId ) ;
2442
25- const folders = await db
43+ const foldersPromise = db
2644 . select ( )
2745 . from ( folderSchema )
2846 . where ( eq ( folderSchema . parent , folderId ) ) ;
29- const files = await db
47+ const filesPromise = db
3048 . select ( )
3149 . from ( fileSchema )
3250 . where ( eq ( fileSchema . parent , folderId ) ) ;
3351
34- return < DriveContents folders = { folders } files = { files } /> ;
52+ const [ folders , files , parents ] = await Promise . all ( [
53+ foldersPromise ,
54+ filesPromise ,
55+ parentsPromise ,
56+ ] ) ;
57+
58+ return < DriveContents folders = { folders } files = { files } parents = { parents } /> ;
3559}
0 commit comments