2023-06-08 17:22:45 +02:00
|
|
|
import { AssetGridState, BucketPosition } from '$lib/models/asset-grid-state';
|
2023-08-01 03:27:56 +02:00
|
|
|
import { api, AssetCountByTimeBucketResponseDto, AssetResponseDto } from '@api';
|
2023-01-27 15:32:26 +01:00
|
|
|
import { writable } from 'svelte/store';
|
2022-09-04 15:34:39 +02:00
|
|
|
|
2023-08-01 03:27:56 +02:00
|
|
|
export interface AssetStore {
|
|
|
|
setInitialState: (
|
|
|
|
viewportHeight: number,
|
|
|
|
viewportWidth: number,
|
|
|
|
data: AssetCountByTimeBucketResponseDto,
|
|
|
|
userId: string | undefined,
|
|
|
|
) => void;
|
|
|
|
getAssetsByBucket: (bucket: string, position: BucketPosition) => Promise<void>;
|
|
|
|
updateBucketHeight: (bucket: string, actualBucketHeight: number) => number;
|
|
|
|
cancelBucketRequest: (token: AbortController, bucketDate: string) => Promise<void>;
|
|
|
|
getAdjacentAsset: (assetId: string, direction: 'next' | 'previous') => Promise<string | null>;
|
|
|
|
removeAsset: (assetId: string) => void;
|
|
|
|
updateAsset: (assetId: string, isFavorite: boolean) => void;
|
|
|
|
subscribe: (run: (value: AssetGridState) => void, invalidate?: (value?: AssetGridState) => void) => () => void;
|
|
|
|
}
|
2022-09-04 15:34:39 +02:00
|
|
|
|
2023-08-01 03:27:56 +02:00
|
|
|
export function createAssetStore(): AssetStore {
|
|
|
|
let _loadingBuckets: { [key: string]: boolean } = {};
|
2023-07-01 06:50:47 +02:00
|
|
|
let _assetGridState = new AssetGridState();
|
|
|
|
|
2023-08-01 03:27:56 +02:00
|
|
|
const { subscribe, set, update } = writable(new AssetGridState());
|
|
|
|
|
|
|
|
subscribe((state) => {
|
|
|
|
_assetGridState = state;
|
2023-07-01 06:50:47 +02:00
|
|
|
});
|
|
|
|
|
2023-08-01 03:27:56 +02:00
|
|
|
const _estimateViewportHeight = (assetCount: number, viewportWidth: number): number => {
|
2023-07-01 06:50:47 +02:00
|
|
|
// Ideally we would use the average aspect ratio for the photoset, however assume
|
|
|
|
// a normal landscape aspect ratio of 3:2, then discount for the likelihood we
|
|
|
|
// will be scaling down and coalescing.
|
|
|
|
const thumbnailHeight = 235;
|
|
|
|
const unwrappedWidth = (3 / 2) * assetCount * thumbnailHeight * (7 / 10);
|
|
|
|
const rows = Math.ceil(unwrappedWidth / viewportWidth);
|
|
|
|
const height = rows * thumbnailHeight;
|
|
|
|
return height;
|
|
|
|
};
|
|
|
|
|
2023-07-05 22:24:23 +02:00
|
|
|
const refreshLoadedAssets = (state: AssetGridState): void => {
|
|
|
|
state.loadedAssets = {};
|
|
|
|
state.buckets.forEach((bucket, bucketIndex) =>
|
|
|
|
bucket.assets.map((asset) => {
|
|
|
|
state.loadedAssets[asset.id] = bucketIndex;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-07-01 06:50:47 +02:00
|
|
|
const setInitialState = (
|
|
|
|
viewportHeight: number,
|
|
|
|
viewportWidth: number,
|
|
|
|
data: AssetCountByTimeBucketResponseDto,
|
|
|
|
userId: string | undefined,
|
|
|
|
) => {
|
2023-08-01 03:27:56 +02:00
|
|
|
set({
|
2023-07-01 06:50:47 +02:00
|
|
|
viewportHeight,
|
|
|
|
viewportWidth,
|
|
|
|
timelineHeight: 0,
|
|
|
|
buckets: data.buckets.map((bucket) => ({
|
|
|
|
bucketDate: bucket.timeBucket,
|
2023-08-01 03:27:56 +02:00
|
|
|
bucketHeight: _estimateViewportHeight(bucket.count, viewportWidth),
|
2023-07-01 06:50:47 +02:00
|
|
|
assets: [],
|
|
|
|
cancelToken: new AbortController(),
|
|
|
|
position: BucketPosition.Unknown,
|
|
|
|
})),
|
|
|
|
assets: [],
|
2023-07-05 22:24:23 +02:00
|
|
|
loadedAssets: {},
|
2023-07-01 06:50:47 +02:00
|
|
|
userId,
|
|
|
|
});
|
|
|
|
|
2023-08-01 03:27:56 +02:00
|
|
|
update((state) => {
|
2023-07-03 11:56:58 +02:00
|
|
|
state.timelineHeight = state.buckets.reduce((acc, b) => acc + b.bucketHeight, 0);
|
2023-07-01 06:50:47 +02:00
|
|
|
return state;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const getAssetsByBucket = async (bucket: string, position: BucketPosition) => {
|
|
|
|
try {
|
|
|
|
const currentBucketData = _assetGridState.buckets.find((b) => b.bucketDate === bucket);
|
|
|
|
if (currentBucketData?.assets && currentBucketData.assets.length > 0) {
|
2023-08-01 03:27:56 +02:00
|
|
|
update((state) => {
|
2023-07-01 06:50:47 +02:00
|
|
|
const bucketIndex = state.buckets.findIndex((b) => b.bucketDate === bucket);
|
|
|
|
state.buckets[bucketIndex].position = position;
|
|
|
|
return state;
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-01 03:27:56 +02:00
|
|
|
_loadingBuckets = { ..._loadingBuckets, [bucket]: true };
|
2023-07-01 06:50:47 +02:00
|
|
|
const { data: assets } = await api.assetApi.getAssetByTimeBucket(
|
|
|
|
{
|
|
|
|
getAssetByTimeBucketDto: {
|
|
|
|
timeBucket: [bucket],
|
|
|
|
userId: _assetGridState.userId,
|
|
|
|
withoutThumbs: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ signal: currentBucketData?.cancelToken.signal },
|
|
|
|
);
|
2023-08-01 03:27:56 +02:00
|
|
|
_loadingBuckets = { ..._loadingBuckets, [bucket]: false };
|
2023-07-01 06:50:47 +02:00
|
|
|
|
2023-08-01 03:27:56 +02:00
|
|
|
update((state) => {
|
2023-07-01 06:50:47 +02:00
|
|
|
const bucketIndex = state.buckets.findIndex((b) => b.bucketDate === bucket);
|
|
|
|
state.buckets[bucketIndex].assets = assets;
|
|
|
|
state.buckets[bucketIndex].position = position;
|
2023-07-03 11:56:58 +02:00
|
|
|
state.assets = state.buckets.flatMap((b) => b.assets);
|
2023-07-05 22:24:23 +02:00
|
|
|
refreshLoadedAssets(state);
|
2023-07-01 06:50:47 +02:00
|
|
|
return state;
|
|
|
|
});
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
} catch (e: any) {
|
|
|
|
if (e.name === 'CanceledError') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.error('Failed to get asset for bucket ', bucket);
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeAsset = (assetId: string) => {
|
2023-08-01 03:27:56 +02:00
|
|
|
update((state) => {
|
2023-07-01 06:50:47 +02:00
|
|
|
const bucketIndex = state.buckets.findIndex((b) => b.assets.some((a) => a.id === assetId));
|
|
|
|
const assetIndex = state.buckets[bucketIndex].assets.findIndex((a) => a.id === assetId);
|
|
|
|
state.buckets[bucketIndex].assets.splice(assetIndex, 1);
|
|
|
|
|
|
|
|
if (state.buckets[bucketIndex].assets.length === 0) {
|
|
|
|
_removeBucket(state.buckets[bucketIndex].bucketDate);
|
|
|
|
}
|
2023-07-03 11:56:58 +02:00
|
|
|
state.assets = state.buckets.flatMap((b) => b.assets);
|
2023-07-05 22:24:23 +02:00
|
|
|
refreshLoadedAssets(state);
|
2023-07-01 06:50:47 +02:00
|
|
|
return state;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const _removeBucket = (bucketDate: string) => {
|
2023-08-01 03:27:56 +02:00
|
|
|
update((state) => {
|
2023-07-01 06:50:47 +02:00
|
|
|
const bucketIndex = state.buckets.findIndex((b) => b.bucketDate === bucketDate);
|
|
|
|
state.buckets.splice(bucketIndex, 1);
|
2023-07-03 11:56:58 +02:00
|
|
|
state.assets = state.buckets.flatMap((b) => b.assets);
|
2023-07-05 22:24:23 +02:00
|
|
|
refreshLoadedAssets(state);
|
2023-07-01 06:50:47 +02:00
|
|
|
return state;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateBucketHeight = (bucket: string, actualBucketHeight: number): number => {
|
|
|
|
let scrollTimeline = false;
|
|
|
|
let heightDelta = 0;
|
|
|
|
|
2023-08-01 03:27:56 +02:00
|
|
|
update((state) => {
|
2023-07-01 06:50:47 +02:00
|
|
|
const bucketIndex = state.buckets.findIndex((b) => b.bucketDate === bucket);
|
|
|
|
// Update timeline height based on the new bucket height
|
|
|
|
const estimateBucketHeight = state.buckets[bucketIndex].bucketHeight;
|
|
|
|
|
|
|
|
heightDelta = actualBucketHeight - estimateBucketHeight;
|
|
|
|
state.timelineHeight += heightDelta;
|
|
|
|
|
|
|
|
scrollTimeline = state.buckets[bucketIndex].position == BucketPosition.Above;
|
|
|
|
|
|
|
|
state.buckets[bucketIndex].bucketHeight = actualBucketHeight;
|
|
|
|
state.buckets[bucketIndex].position = BucketPosition.Unknown;
|
|
|
|
|
|
|
|
return state;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (scrollTimeline) {
|
|
|
|
return heightDelta;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
const cancelBucketRequest = async (token: AbortController, bucketDate: string) => {
|
2023-08-01 03:27:56 +02:00
|
|
|
if (!_loadingBuckets[bucketDate]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-01 06:50:47 +02:00
|
|
|
token.abort();
|
2023-08-01 03:27:56 +02:00
|
|
|
|
|
|
|
update((state) => {
|
2023-07-01 06:50:47 +02:00
|
|
|
const bucketIndex = state.buckets.findIndex((b) => b.bucketDate === bucketDate);
|
|
|
|
state.buckets[bucketIndex].cancelToken = new AbortController();
|
|
|
|
return state;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateAsset = (assetId: string, isFavorite: boolean) => {
|
2023-08-01 03:27:56 +02:00
|
|
|
update((state) => {
|
2023-07-01 06:50:47 +02:00
|
|
|
const bucketIndex = state.buckets.findIndex((b) => b.assets.some((a) => a.id === assetId));
|
|
|
|
const assetIndex = state.buckets[bucketIndex].assets.findIndex((a) => a.id === assetId);
|
|
|
|
state.buckets[bucketIndex].assets[assetIndex].isFavorite = isFavorite;
|
|
|
|
|
2023-07-03 11:56:58 +02:00
|
|
|
state.assets = state.buckets.flatMap((b) => b.assets);
|
2023-07-05 22:24:23 +02:00
|
|
|
refreshLoadedAssets(state);
|
2023-07-01 06:50:47 +02:00
|
|
|
return state;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-08-01 03:27:56 +02:00
|
|
|
const _getNextAsset = async (currentBucketIndex: number, assetId: string): Promise<AssetResponseDto | null> => {
|
|
|
|
const currentBucket = _assetGridState.buckets[currentBucketIndex];
|
|
|
|
const assetIndex = currentBucket.assets.findIndex(({ id }) => id == assetId);
|
|
|
|
if (assetIndex === -1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (assetIndex + 1 < currentBucket.assets.length) {
|
|
|
|
return currentBucket.assets[assetIndex + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextBucketIndex = currentBucketIndex + 1;
|
|
|
|
if (nextBucketIndex >= _assetGridState.buckets.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextBucket = _assetGridState.buckets[nextBucketIndex];
|
|
|
|
await getAssetsByBucket(nextBucket.bucketDate, BucketPosition.Unknown);
|
|
|
|
|
|
|
|
return nextBucket.assets[0] ?? null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const _getPrevAsset = async (currentBucketIndex: number, assetId: string): Promise<AssetResponseDto | null> => {
|
|
|
|
const currentBucket = _assetGridState.buckets[currentBucketIndex];
|
|
|
|
const assetIndex = currentBucket.assets.findIndex(({ id }) => id == assetId);
|
|
|
|
if (assetIndex === -1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (assetIndex > 0) {
|
|
|
|
return currentBucket.assets[assetIndex - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
const prevBucketIndex = currentBucketIndex - 1;
|
|
|
|
if (prevBucketIndex < 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const prevBucket = _assetGridState.buckets[prevBucketIndex];
|
|
|
|
await getAssetsByBucket(prevBucket.bucketDate, BucketPosition.Unknown);
|
|
|
|
|
|
|
|
return prevBucket.assets[prevBucket.assets.length - 1] ?? null;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getAdjacentAsset = async (assetId: string, direction: 'next' | 'previous'): Promise<string | null> => {
|
|
|
|
const currentBucketIndex = _assetGridState.loadedAssets[assetId];
|
|
|
|
if (currentBucketIndex < 0 || currentBucketIndex >= _assetGridState.buckets.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const asset =
|
|
|
|
direction === 'next'
|
|
|
|
? await _getNextAsset(currentBucketIndex, assetId)
|
|
|
|
: await _getPrevAsset(currentBucketIndex, assetId);
|
|
|
|
|
|
|
|
return asset?.id ?? null;
|
|
|
|
};
|
|
|
|
|
2023-07-01 06:50:47 +02:00
|
|
|
return {
|
|
|
|
setInitialState,
|
|
|
|
getAssetsByBucket,
|
|
|
|
removeAsset,
|
|
|
|
updateBucketHeight,
|
|
|
|
cancelBucketRequest,
|
2023-08-01 03:27:56 +02:00
|
|
|
getAdjacentAsset,
|
2023-07-01 06:50:47 +02:00
|
|
|
updateAsset,
|
2023-08-01 03:27:56 +02:00
|
|
|
subscribe,
|
2023-07-01 06:50:47 +02:00
|
|
|
};
|
2022-09-04 15:34:39 +02:00
|
|
|
}
|