2023-08-04 23:07:15 +02:00
|
|
|
import { api, AssetApiGetTimeBucketsRequest, AssetResponseDto } from '@api';
|
2023-08-03 17:44:12 +02:00
|
|
|
import { writable } from 'svelte/store';
|
|
|
|
import { handleError } from '../utils/handle-error';
|
2022-09-04 15:34:39 +02:00
|
|
|
|
2023-08-03 17:44:12 +02:00
|
|
|
export enum BucketPosition {
|
|
|
|
Above = 'above',
|
|
|
|
Below = 'below',
|
|
|
|
Visible = 'visible',
|
|
|
|
Unknown = 'unknown',
|
|
|
|
}
|
2023-08-03 03:57:11 +02:00
|
|
|
|
2023-08-04 23:07:15 +02:00
|
|
|
export type AssetStoreOptions = AssetApiGetTimeBucketsRequest;
|
2023-08-03 03:57:11 +02:00
|
|
|
|
2023-08-03 17:44:12 +02:00
|
|
|
export interface Viewport {
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
}
|
2023-08-03 03:57:11 +02:00
|
|
|
|
2023-08-03 17:44:12 +02:00
|
|
|
interface AssetLookup {
|
|
|
|
bucket: AssetBucket;
|
|
|
|
bucketIndex: number;
|
|
|
|
assetIndex: number;
|
|
|
|
}
|
2023-08-03 03:57:11 +02:00
|
|
|
|
2023-08-03 17:44:12 +02:00
|
|
|
export class AssetBucket {
|
|
|
|
/**
|
|
|
|
* The DOM height of the bucket in pixel
|
|
|
|
* This value is first estimated by the number of asset and later is corrected as the user scroll
|
|
|
|
*/
|
|
|
|
bucketHeight!: number;
|
|
|
|
bucketDate!: string;
|
|
|
|
assets!: AssetResponseDto[];
|
|
|
|
cancelToken!: AbortController | null;
|
|
|
|
position!: BucketPosition;
|
2023-08-01 03:27:56 +02:00
|
|
|
}
|
2022-09-04 15:34:39 +02:00
|
|
|
|
2023-08-03 17:44:12 +02:00
|
|
|
const THUMBNAIL_HEIGHT = 235;
|
|
|
|
|
|
|
|
export class AssetStore {
|
|
|
|
private store$ = writable(this);
|
|
|
|
private assetToBucket: Record<string, AssetLookup> = {};
|
|
|
|
|
|
|
|
timelineHeight = 0;
|
|
|
|
buckets: AssetBucket[] = [];
|
|
|
|
assets: AssetResponseDto[] = [];
|
2023-08-11 18:00:51 +02:00
|
|
|
albumAssets: Set<string> = new Set();
|
2023-08-03 17:44:12 +02:00
|
|
|
|
2023-08-11 18:00:51 +02:00
|
|
|
constructor(private options: AssetStoreOptions, private albumId?: string) {
|
2023-08-03 17:44:12 +02:00
|
|
|
this.store$.set(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribe = this.store$.subscribe;
|
|
|
|
|
|
|
|
async init(viewport: Viewport) {
|
2023-08-11 18:00:51 +02:00
|
|
|
this.timelineHeight = 0;
|
|
|
|
this.buckets = [];
|
|
|
|
this.assets = [];
|
|
|
|
this.assetToBucket = {};
|
|
|
|
this.albumAssets = new Set();
|
|
|
|
|
2023-08-04 23:07:15 +02:00
|
|
|
const { data: buckets } = await api.assetApi.getTimeBuckets(this.options);
|
2023-08-03 17:44:12 +02:00
|
|
|
|
2023-08-04 23:07:15 +02:00
|
|
|
this.buckets = buckets.map((bucket) => {
|
2023-08-03 17:44:12 +02:00
|
|
|
const unwrappedWidth = (3 / 2) * bucket.count * THUMBNAIL_HEIGHT * (7 / 10);
|
|
|
|
const rows = Math.ceil(unwrappedWidth / viewport.width);
|
|
|
|
const height = rows * THUMBNAIL_HEIGHT;
|
|
|
|
|
|
|
|
return {
|
|
|
|
bucketDate: bucket.timeBucket,
|
|
|
|
bucketHeight: height,
|
|
|
|
assets: [],
|
|
|
|
cancelToken: null,
|
|
|
|
position: BucketPosition.Unknown,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
this.timelineHeight = this.buckets.reduce((acc, b) => acc + b.bucketHeight, 0);
|
|
|
|
|
|
|
|
this.emit(false);
|
|
|
|
|
|
|
|
let height = 0;
|
|
|
|
for (const bucket of this.buckets) {
|
|
|
|
if (height < viewport.height) {
|
|
|
|
height += bucket.bucketHeight;
|
|
|
|
this.loadBucket(bucket.bucketDate, BucketPosition.Visible);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadBucket(bucketDate: string, position: BucketPosition): Promise<void> {
|
|
|
|
try {
|
|
|
|
const bucket = this.getBucketByDate(bucketDate);
|
|
|
|
if (!bucket) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket.position = position;
|
|
|
|
|
|
|
|
if (bucket.assets.length !== 0) {
|
|
|
|
this.emit(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket.cancelToken = new AbortController();
|
|
|
|
|
2023-08-04 23:07:15 +02:00
|
|
|
const { data: assets } = await api.assetApi.getByTimeBucket(
|
|
|
|
{ ...this.options, timeBucket: bucketDate },
|
2023-08-03 17:44:12 +02:00
|
|
|
{ signal: bucket.cancelToken.signal },
|
|
|
|
);
|
|
|
|
|
2023-08-11 18:00:51 +02:00
|
|
|
if (this.albumId) {
|
|
|
|
const { data: albumAssets } = await api.assetApi.getByTimeBucket(
|
|
|
|
{
|
|
|
|
albumId: this.albumId,
|
|
|
|
timeBucket: bucketDate,
|
|
|
|
size: this.options.size,
|
|
|
|
key: this.options.key,
|
|
|
|
},
|
|
|
|
{ signal: bucket.cancelToken.signal },
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const asset of albumAssets) {
|
|
|
|
this.albumAssets.add(asset.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 17:44:12 +02:00
|
|
|
bucket.assets = assets;
|
|
|
|
this.emit(true);
|
|
|
|
} catch (error) {
|
|
|
|
handleError(error, 'Failed to load assets');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelBucket(bucket: AssetBucket) {
|
|
|
|
bucket.cancelToken?.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateBucket(bucketDate: string, height: number) {
|
|
|
|
const bucket = this.getBucketByDate(bucketDate);
|
|
|
|
if (!bucket) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const delta = height - bucket.bucketHeight;
|
|
|
|
const scrollTimeline = bucket.position == BucketPosition.Above;
|
|
|
|
|
|
|
|
bucket.bucketHeight = height;
|
|
|
|
bucket.position = BucketPosition.Unknown;
|
|
|
|
|
|
|
|
this.timelineHeight += delta;
|
|
|
|
|
|
|
|
this.emit(false);
|
|
|
|
|
|
|
|
return scrollTimeline ? delta : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
getBucketByDate(bucketDate: string): AssetBucket | null {
|
|
|
|
return this.buckets.find((bucket) => bucket.bucketDate === bucketDate) || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
getBucketInfoForAssetId(assetId: string) {
|
|
|
|
return this.assetToBucket[assetId] || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
getBucketIndexByAssetId(assetId: string) {
|
|
|
|
return this.assetToBucket[assetId]?.bucketIndex ?? null;
|
|
|
|
}
|
|
|
|
|
2023-08-05 05:26:28 +02:00
|
|
|
updateAsset(_asset: AssetResponseDto) {
|
|
|
|
const asset = this.assets.find((asset) => asset.id === _asset.id);
|
2023-08-03 17:44:12 +02:00
|
|
|
if (!asset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-05 05:26:28 +02:00
|
|
|
Object.assign(asset, _asset);
|
|
|
|
|
2023-08-03 17:44:12 +02:00
|
|
|
this.emit(false);
|
|
|
|
}
|
|
|
|
|
2023-08-16 22:04:55 +02:00
|
|
|
removeAssets(ids: string[]) {
|
|
|
|
// TODO: this could probably be more efficient
|
|
|
|
for (const id of ids) {
|
|
|
|
this.removeAsset(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeAsset(id: string) {
|
2023-08-03 17:44:12 +02:00
|
|
|
for (let i = 0; i < this.buckets.length; i++) {
|
|
|
|
const bucket = this.buckets[i];
|
|
|
|
for (let j = 0; j < bucket.assets.length; j++) {
|
|
|
|
const asset = bucket.assets[j];
|
2023-08-16 22:04:55 +02:00
|
|
|
if (asset.id !== id) {
|
2023-08-03 17:44:12 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket.assets.splice(j, 1);
|
|
|
|
if (bucket.assets.length === 0) {
|
|
|
|
this.buckets.splice(i, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.emit(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getPreviousAssetId(assetId: string): Promise<string | null> {
|
|
|
|
const info = this.getBucketInfoForAssetId(assetId);
|
|
|
|
if (!info) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { bucket, assetIndex, bucketIndex } = info;
|
|
|
|
|
|
|
|
if (assetIndex !== 0) {
|
|
|
|
return bucket.assets[assetIndex - 1].id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bucketIndex === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const previousBucket = this.buckets[bucketIndex - 1];
|
|
|
|
await this.loadBucket(previousBucket.bucketDate, BucketPosition.Unknown);
|
|
|
|
return previousBucket.assets.at(-1)?.id || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getNextAssetId(assetId: string): Promise<string | null> {
|
|
|
|
const info = this.getBucketInfoForAssetId(assetId);
|
|
|
|
if (!info) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { bucket, assetIndex, bucketIndex } = info;
|
|
|
|
|
|
|
|
if (assetIndex !== bucket.assets.length - 1) {
|
|
|
|
return bucket.assets[assetIndex + 1].id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bucketIndex === this.buckets.length - 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextBucket = this.buckets[bucketIndex + 1];
|
|
|
|
await this.loadBucket(nextBucket.bucketDate, BucketPosition.Unknown);
|
|
|
|
return nextBucket.assets[0]?.id || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private emit(recalculate: boolean) {
|
|
|
|
if (recalculate) {
|
|
|
|
this.assets = this.buckets.flatMap(({ assets }) => assets);
|
|
|
|
|
|
|
|
const assetToBucket: Record<string, AssetLookup> = {};
|
|
|
|
for (let i = 0; i < this.buckets.length; i++) {
|
|
|
|
const bucket = this.buckets[i];
|
|
|
|
for (let j = 0; j < bucket.assets.length; j++) {
|
|
|
|
const asset = bucket.assets[j];
|
|
|
|
assetToBucket[asset.id] = { bucket, bucketIndex: i, assetIndex: j };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.assetToBucket = assetToBucket;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.store$.update(() => this);
|
|
|
|
}
|
2022-09-04 15:34:39 +02:00
|
|
|
}
|