@@ -2,6 +2,9 @@ import { ALL } from "@/config/constants";
22import AlbumService from "@/services/album-service" ;
33import { AuthStore } from "@/stores/Auth" ;
44import { computed , Ref , ref } from "vue" ;
5+ import { type SplitData , useSplitter } from "./splitter" ;
6+
7+ const { spliter, merge } = useSplitter ( ) ;
58
69export function useAlbumRefresher ( albumId : Ref < string > , photoId : Ref < string | undefined > , auth : AuthStore , isLoginOpen : Ref < boolean > ) {
710 const isPasswordProtected = ref ( false ) ;
@@ -15,6 +18,7 @@ export function useAlbumRefresher(albumId: Ref<string>, photoId: Ref<string | un
1518
1619 const photo = ref < App . Http . Resources . Models . PhotoResource | undefined > ( undefined ) ;
1720 const photos = ref < App . Http . Resources . Models . PhotoResource [ ] > ( [ ] ) ;
21+ const photosTimeline = ref < SplitData < App . Http . Resources . Models . PhotoResource > [ ] | undefined > ( undefined ) ;
1822
1923 const config = ref < App . Http . Resources . GalleryConfigs . AlbumConfig | undefined > ( undefined ) ;
2024 const rights = computed ( ( ) => album . value ?. rights ?? undefined ) ;
@@ -39,14 +43,43 @@ export function useAlbumRefresher(albumId: Ref<string>, photoId: Ref<string | un
3943 modelAlbum . value = undefined ;
4044 tagAlbum . value = undefined ;
4145 smartAlbum . value = undefined ;
46+ photosTimeline . value = undefined ;
4247 if ( data . data . config . is_model_album ) {
4348 modelAlbum . value = data . data . resource as App . Http . Resources . Models . AlbumResource ;
4449 } else if ( data . data . config . is_base_album ) {
4550 tagAlbum . value = data . data . resource as App . Http . Resources . Models . TagAlbumResource ;
4651 } else {
4752 smartAlbum . value = data . data . resource as App . Http . Resources . Models . SmartAlbumResource ;
4853 }
49- photos . value = album . value ?. photos ?? [ ] ;
54+
55+ // So what is going on here?
56+ // The problem is that the ordering of the photos from the API is not necessarily the same
57+ // as the ordering of the photos in the timeline. The timeline is constructed from the photos
58+ // taken_at data and if not provided, created_at data.
59+ // This is split into different chunks based on the granularity.
60+ // If the ordering is done by created_at, and the order of the photos match, we do not have problems.
61+ // But if one of the photos has a different taken_at date, that does not match the order, then all the following
62+ // photos are "moved" to different place which does not match the original index ordering.
63+ //
64+ // When we click on a photo, the index returned refers to the original ordering of the photos.
65+ // As a result, if the timeline is enabled, we first do the split and then merge the photos so that the
66+ // ordering is updated to reflect the timeline.
67+ //
68+ // Note that this is not something that can be fixed in the backend as we would need to assume that all the dates are
69+ // set properly. Furthermore, this would make the functionality unavailable if sorting by title is done.
70+ // By doing it in the front-end, we are able to display the photos by blocks of time,
71+ // and within the block, the ordering is done as expected.
72+ if ( data . data . config . is_photo_timeline_enabled ) {
73+ photosTimeline . value = spliter (
74+ data . data . resource ?. photos ?? [ ] ,
75+ ( p : App . Http . Resources . Models . PhotoResource ) => p . timeline ?. time_date ?? "" ,
76+ ( p : App . Http . Resources . Models . PhotoResource ) => p . timeline ?. format ?? "Others" ,
77+ ) ;
78+ photos . value = merge ( photosTimeline . value ) ;
79+ } else {
80+ // We are not using the timeline, so we can just use the photos as is.
81+ photos . value = album . value ?. photos ?? [ ] ;
82+ }
5083 } )
5184 . catch ( ( error ) => {
5285 if ( error . response && error . response . status === 401 && error . response . data . message === "Password required" ) {
@@ -84,6 +117,7 @@ export function useAlbumRefresher(albumId: Ref<string>, photoId: Ref<string | un
84117 rights,
85118 photo,
86119 photos,
120+ photosTimeline,
87121 config,
88122 loadUser,
89123 loadAlbum,
0 commit comments