33import React , { useState , useEffect } from 'react' ;
44import { useTheme } from '@/context/ThemeContext' ;
55import { useDrive } from '@/context/DriveContext' ;
6+ import { useAuth } from '@/context/AuthContext' ;
67
7- const FileItem = ( { item, source, theme, onDownload, onShare, onDelete } ) => {
8+ const FileItem = ( { item, source, theme, onDownload, onShare, onDelete, currentUserId } ) => {
89 const isGdrive = source === 'gdrive' ;
910 const icon = isGdrive ? (
1011 < img src = { item . iconLink } alt = "" className = "w-5 h-5" />
@@ -17,6 +18,9 @@ const FileItem = ({ item, source, theme, onDownload, onShare, onDelete }) => {
1718 // Get the correct file ID - use item.id for Google Drive, item._id for local files
1819 const fileId = item . id || item . _id ;
1920
21+ // Check if current user is the owner (for local files)
22+ const isOwner = ! isGdrive && item . owner && item . owner . _id === currentUserId ;
23+
2024 return (
2125 < li
2226 className = { `flex justify-between items-center p-2 rounded ${ theme . app . button_subtle_hover } ` }
@@ -29,6 +33,9 @@ const FileItem = ({ item, source, theme, onDownload, onShare, onDelete }) => {
2933 >
3034 { item . name }
3135 { isDirectory ? '/' : '' }
36+ { ! isGdrive && ! isOwner && (
37+ < span className = "text-xs text-gray-500 ml-2" > (Shared)</ span >
38+ ) }
3239 </ span >
3340 </ div >
3441 < div className = "flex gap-2" >
@@ -59,11 +66,15 @@ const FileItem = ({ item, source, theme, onDownload, onShare, onDelete }) => {
5966 Share
6067 </ button >
6168 < button
62- onClick = { ( ) => onDelete ( fileId ) } // Also fix delete
63- className = "px-2 py-1 text-sm rounded bg-red-500 text-white hover:bg-red-600"
64- title = "Delete File"
69+ onClick = { ( ) => onDelete ( fileId , item . name , isOwner ) } // Also fixed delete
70+ className = { `px-2 py-1 text-sm rounded ${
71+ isOwner
72+ ? 'bg-red-500 hover:bg-red-600 text-white'
73+ : 'bg-orange-500 hover:bg-orange-600 text-white'
74+ } `}
75+ title = { isOwner ? 'Delete File' : 'Unshare File' }
6576 >
66- Delete
77+ { isOwner ? ' Delete' : 'Unshare' }
6778 </ button >
6879 </ >
6980 )
@@ -81,6 +92,7 @@ const StatusMessage = ({ children }) => (
8192
8293export default function FileManagerApp ( ) {
8394 const { theme } = useTheme ( ) ;
95+ const { user : currentUser } = useAuth ( ) ; // Get current user from auth context
8496 const [ activeSource , setActiveSource ] = useState ( 'gdrive' ) ;
8597 const [ localItems , setLocalItems ] = useState ( [ ] ) ;
8698 const [ isLoadingLocal , setIsLoadingLocal ] = useState ( true ) ;
@@ -116,7 +128,8 @@ export default function FileManagerApp() {
116128 const data = await res . json ( ) ;
117129 setLocalItems ( data . files || [ ] ) ;
118130 } catch ( error ) {
119- setError ( 'Failed to load files.' ) ;
131+ console . error ( 'Failed to load files:' , error ) ;
132+ setError ( 'Failed to load files from server.' ) ;
120133 setLocalItems ( [ ] ) ;
121134 } finally {
122135 setIsLoadingLocal ( false ) ;
@@ -151,9 +164,42 @@ export default function FileManagerApp() {
151164 }
152165 } ;
153166
154- const handleDownload = ( filename ) => {
155- const url = `/api/files/download?file=${ encodeURIComponent ( filename ) } ` ;
156- window . open ( url , '_blank' ) ;
167+ const handleDownload = async ( filename ) => {
168+ try {
169+ // Use the same method as Notes app - fetch from /api/files
170+ const response = await fetch ( '/api/files' ) ;
171+
172+ if ( ! response . ok ) {
173+ throw new Error ( `Download failed: ${ response . status } ` ) ;
174+ }
175+
176+ const data = await response . json ( ) ;
177+ const files = data . files || [ ] ;
178+ const file = files . find ( f => f . name === filename ) ;
179+
180+ if ( ! file ) {
181+ throw new Error ( `File "${ filename } " not found` ) ;
182+ }
183+
184+ const content = file . content || '' ;
185+
186+ // Create download
187+ const blob = new Blob ( [ content ] , { type : 'text/plain' } ) ;
188+ const downloadUrl = URL . createObjectURL ( blob ) ;
189+
190+ const link = document . createElement ( 'a' ) ;
191+ link . href = downloadUrl ;
192+ link . download = filename ;
193+ document . body . appendChild ( link ) ;
194+ link . click ( ) ;
195+ document . body . removeChild ( link ) ;
196+ URL . revokeObjectURL ( downloadUrl ) ;
197+
198+ showNotification ( `Downloaded "${ filename } " successfully` , 'success' ) ;
199+ } catch ( error ) {
200+ console . error ( 'Download failed:' , error ) ;
201+ showNotification ( `Download failed: ${ error . message } ` , 'error' ) ;
202+ }
157203 } ;
158204
159205 const handleShareFile = async ( fileId , userEmail , permission ) => {
@@ -179,16 +225,34 @@ export default function FileManagerApp() {
179225 showNotification ( error . message , 'error' ) ;
180226 }
181227 } ;
228+
229+ const handleDelete = async ( fileId , filename , isOwner ) => {
230+ if ( ! confirm ( `Are you sure you want to ${ isOwner ? 'delete' : 'unshare' } "${ filename } "?` ) ) {
231+ return ;
232+ }
182233
183- const handleDelete = async ( fileId ) => {
184234 try {
185- const res = await fetch ( `/api/files/${ fileId } ` , { method : 'DELETE' } ) ;
186- if ( ! res . ok ) throw new Error ( 'Delete failed' ) ;
235+ const response = await fetch ( `/api/files/${ fileId } ` , {
236+ method : 'DELETE' ,
237+ headers : {
238+ 'Content-Type' : 'application/json' ,
239+ } ,
240+ } ) ;
241+
242+ if ( ! response . ok ) {
243+ const errorData = await response . json ( ) ;
244+ throw new Error ( errorData . error || `Failed to ${ isOwner ? 'delete' : 'unshare' } file` ) ;
245+ }
246+
247+ const result = await response . json ( ) ;
248+
249+ setLocalItems ( prev => prev . filter ( item => ( item . id !== fileId && item . _id !== fileId ) ) ) ;
250+
251+ showNotification ( result . message , 'success' ) ;
187252 await fetchLocalItems ( ) ;
188- showNotification ( 'File deleted successfully' , 'success' ) ;
189253 } catch ( error ) {
190254 console . error ( 'Delete file failed:' , error ) ;
191- showNotification ( 'File deletion failed' , 'error' ) ;
255+ showNotification ( error . message , 'error' ) ;
192256 }
193257 } ;
194258
@@ -265,6 +329,7 @@ export default function FileManagerApp() {
265329 onDownload = { handleDownload }
266330 onShare = { handleShareClick }
267331 onDelete = { handleDelete }
332+ currentUserId = { currentUser ?. id }
268333 />
269334 ) ) }
270335 </ ul >
0 commit comments