@@ -33,13 +33,13 @@ export function usePhotos(houseId: number) {
3333 }
3434
3535 const rootPhotos = computed ( ( ) =>
36- photos . value . filter ( ( p ) => p . folderId === null ) . sort ( ( a , b ) => b . createdAt - a . createdAt ) ,
36+ photos . value . filter ( ( p ) => p . folderId === null ) . sort ( ( a , b ) => a . sortOrder - b . sortOrder ) ,
3737 )
3838
3939 function photosInFolder ( folderId : number ) : Photo [ ] {
4040 return photos . value
4141 . filter ( ( p ) => p . folderId === folderId )
42- . sort ( ( a , b ) => b . createdAt - a . createdAt )
42+ . sort ( ( a , b ) => a . sortOrder - b . sortOrder )
4343 }
4444
4545 // ----- Photos -----
@@ -56,8 +56,12 @@ export function usePhotos(houseId: number) {
5656 const created = await api . uploadPhoto ( houseId , file , folderId , null , ( progress ) => {
5757 uploads . value = uploads . value . map ( ( u ) => ( u . id === entry . id ? { ...u , progress } : u ) )
5858 } )
59- photos . value = [ ...photos . value , created ]
60- return created
59+ // Place new photo first by giving it a sortOrder below the current minimum.
60+ const siblings = photos . value . filter ( ( p ) => p . folderId === ( folderId ?? null ) )
61+ const minSort = siblings . length > 0 ? Math . min ( ...siblings . map ( ( p ) => p . sortOrder ) ) : 0
62+ const placed = { ...created , sortOrder : minSort - 1 }
63+ photos . value = [ ...photos . value , placed ]
64+ return placed
6165 } finally {
6266 uploads . value = uploads . value . filter ( ( u ) => u . id !== entry . id )
6367 }
@@ -77,12 +81,12 @@ export function usePhotos(houseId: number) {
7781 }
7882
7983 async function reorderPhotos ( items : { id : number ; sortOrder : number } [ ] ) : Promise < void > {
80- await api . reorderPhotos ( houseId , items )
81- // Apply locally
84+ // Apply optimistically so there's no visual jump while the API call is in flight.
8285 const map = new Map ( items . map ( ( i ) => [ i . id , i . sortOrder ] ) )
8386 photos . value = photos . value
8487 . map ( ( p ) => ( map . has ( p . id ) ? { ...p , sortOrder : map . get ( p . id ) ! } : p ) )
8588 . sort ( ( a , b ) => a . sortOrder - b . sortOrder )
89+ await api . reorderPhotos ( houseId , items )
8690 }
8791
8892 // ----- Folders -----
@@ -109,11 +113,11 @@ export function usePhotos(houseId: number) {
109113 }
110114
111115 async function reorderFolders ( items : { id : number ; sortOrder : number } [ ] ) : Promise < void > {
112- await api . reorderFolders ( houseId , items )
113116 const map = new Map ( items . map ( ( i ) => [ i . id , i . sortOrder ] ) )
114117 folders . value = folders . value
115118 . map ( ( f ) => ( map . has ( f . id ) ? { ...f , sortOrder : map . get ( f . id ) ! } : f ) )
116119 . sort ( ( a , b ) => a . sortOrder - b . sortOrder )
120+ await api . reorderFolders ( houseId , items )
117121 }
118122
119123 return {
0 commit comments