2024-02-14 14:09:49 +01:00
|
|
|
import { getKey } from '$lib/utils';
|
2024-02-14 15:38:57 +01:00
|
|
|
import { TimeBucketSize, getTimeBucket, getTimeBuckets, type AssetResponseDto } from '@immich/sdk';
|
2023-10-06 22:48:11 +02:00
|
|
|
import { throttle } from 'lodash-es';
|
|
|
|
import { DateTime } from 'luxon';
|
2024-02-14 14:09:49 +01:00
|
|
|
import { writable, type Unsubscriber } from 'svelte/store';
|
2023-08-03 17:44:12 +02:00
|
|
|
import { handleError } from '../utils/handle-error';
|
2024-02-16 21:43:40 +01:00
|
|
|
import { websocketEvents } from './websocket';
|
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',
|
|
|
|
}
|
2024-02-14 15:38:57 +01:00
|
|
|
type AssetApiGetTimeBucketsRequest = Parameters<typeof getTimeBuckets>[0];
|
2023-10-26 20:55:10 +02:00
|
|
|
export type AssetStoreOptions = Omit<AssetApiGetTimeBucketsRequest, 'size'>;
|
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
|
|
|
|
*/
|
2024-03-05 16:43:24 +01:00
|
|
|
bucketHeight!: number;
|
|
|
|
bucketDate!: string;
|
|
|
|
bucketCount!: number;
|
2023-08-03 17:44:12 +02:00
|
|
|
assets!: AssetResponseDto[];
|
|
|
|
cancelToken!: AbortController | null;
|
|
|
|
position!: BucketPosition;
|
2023-08-01 03:27:56 +02:00
|
|
|
}
|
2022-09-04 15:34:39 +02:00
|
|
|
|
2023-10-06 22:48:11 +02:00
|
|
|
const isMismatched = (option: boolean | undefined, value: boolean): boolean =>
|
|
|
|
option === undefined ? false : option !== value;
|
|
|
|
|
2023-08-03 17:44:12 +02:00
|
|
|
const THUMBNAIL_HEIGHT = 235;
|
|
|
|
|
2023-10-06 22:48:11 +02:00
|
|
|
interface AddAsset {
|
|
|
|
type: 'add';
|
2024-03-15 17:11:29 +01:00
|
|
|
values: AssetResponseDto[];
|
2023-10-06 22:48:11 +02:00
|
|
|
}
|
|
|
|
|
2024-02-29 18:44:30 +01:00
|
|
|
interface UpdateAsset {
|
|
|
|
type: 'update';
|
2024-03-15 17:11:29 +01:00
|
|
|
values: AssetResponseDto[];
|
2024-02-29 18:44:30 +01:00
|
|
|
}
|
|
|
|
|
2023-10-06 22:48:11 +02:00
|
|
|
interface DeleteAsset {
|
|
|
|
type: 'delete';
|
2024-03-15 17:11:29 +01:00
|
|
|
values: string[];
|
2023-10-06 22:48:11 +02:00
|
|
|
}
|
|
|
|
|
2024-03-02 01:49:31 +01:00
|
|
|
interface TrashAssets {
|
2023-10-06 22:48:11 +02:00
|
|
|
type: 'trash';
|
2024-03-15 17:11:29 +01:00
|
|
|
values: string[];
|
2023-10-06 22:48:11 +02:00
|
|
|
}
|
|
|
|
|
2023-12-05 16:43:15 +01:00
|
|
|
export const photoViewer = writable<HTMLImageElement | null>(null);
|
|
|
|
|
2024-03-02 01:49:31 +01:00
|
|
|
type PendingChange = AddAsset | UpdateAsset | DeleteAsset | TrashAssets;
|
2023-10-06 22:48:11 +02:00
|
|
|
|
2023-08-03 17:44:12 +02:00
|
|
|
export class AssetStore {
|
|
|
|
private store$ = writable(this);
|
|
|
|
private assetToBucket: Record<string, AssetLookup> = {};
|
2023-10-06 22:48:11 +02:00
|
|
|
private pendingChanges: PendingChange[] = [];
|
|
|
|
private unsubscribers: Unsubscriber[] = [];
|
2023-10-26 20:55:10 +02:00
|
|
|
private options: AssetApiGetTimeBucketsRequest;
|
2023-08-03 17:44:12 +02:00
|
|
|
|
2023-09-01 19:12:09 +02:00
|
|
|
initialized = false;
|
2023-08-03 17:44:12 +02:00
|
|
|
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-11-27 17:42:04 +01:00
|
|
|
constructor(
|
|
|
|
options: AssetStoreOptions,
|
|
|
|
private albumId?: string,
|
|
|
|
) {
|
2023-10-26 20:55:10 +02:00
|
|
|
this.options = { ...options, size: TimeBucketSize.Month };
|
2023-08-03 17:44:12 +02:00
|
|
|
this.store$.set(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribe = this.store$.subscribe;
|
|
|
|
|
2023-12-05 02:29:35 +01:00
|
|
|
private addPendingChanges(...changes: PendingChange[]) {
|
|
|
|
// prevent websocket events from happening before local client events
|
|
|
|
setTimeout(() => {
|
|
|
|
this.pendingChanges.push(...changes);
|
|
|
|
this.processPendingChanges();
|
2024-02-02 04:18:00 +01:00
|
|
|
}, 1000);
|
2023-12-05 02:29:35 +01:00
|
|
|
}
|
|
|
|
|
2023-10-06 22:48:11 +02:00
|
|
|
connect() {
|
|
|
|
this.unsubscribers.push(
|
2024-02-16 21:43:40 +01:00
|
|
|
websocketEvents.on('on_upload_success', (asset) => {
|
2024-03-15 17:11:29 +01:00
|
|
|
this.addPendingChanges({ type: 'add', values: [asset] });
|
2023-10-06 22:48:11 +02:00
|
|
|
}),
|
2024-02-16 21:43:40 +01:00
|
|
|
websocketEvents.on('on_asset_trash', (ids) => {
|
2024-03-15 17:11:29 +01:00
|
|
|
this.addPendingChanges({ type: 'trash', values: ids });
|
2023-10-06 22:48:11 +02:00
|
|
|
}),
|
2024-02-29 18:44:30 +01:00
|
|
|
websocketEvents.on('on_asset_update', (asset) => {
|
2024-03-15 17:11:29 +01:00
|
|
|
this.addPendingChanges({ type: 'update', values: [asset] });
|
2024-02-29 18:44:30 +01:00
|
|
|
}),
|
2024-02-16 21:43:40 +01:00
|
|
|
websocketEvents.on('on_asset_delete', (id: string) => {
|
2024-03-15 17:11:29 +01:00
|
|
|
this.addPendingChanges({ type: 'delete', values: [id] });
|
2023-10-06 22:48:11 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
for (const unsubscribe of this.unsubscribers) {
|
|
|
|
unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-15 17:11:29 +01:00
|
|
|
private getPendingChangeBatches() {
|
|
|
|
const batches: PendingChange[] = [];
|
|
|
|
let batch: PendingChange | undefined;
|
|
|
|
|
|
|
|
for (const { type, values: _values } of this.pendingChanges) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const values = _values as any[];
|
|
|
|
|
|
|
|
if (batch && batch.type !== type) {
|
|
|
|
batches.push(batch);
|
|
|
|
batch = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (batch) {
|
|
|
|
batch.values.push(...values);
|
|
|
|
} else {
|
|
|
|
batch = { type, values };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (batch) {
|
|
|
|
batches.push(batch);
|
|
|
|
}
|
|
|
|
|
|
|
|
return batches;
|
|
|
|
}
|
|
|
|
|
2023-10-06 22:48:11 +02:00
|
|
|
processPendingChanges = throttle(() => {
|
2024-03-15 17:11:29 +01:00
|
|
|
for (const { type, values } of this.getPendingChangeBatches()) {
|
2023-10-06 22:48:11 +02:00
|
|
|
switch (type) {
|
2024-02-02 04:18:00 +01:00
|
|
|
case 'add': {
|
2024-03-15 17:11:29 +01:00
|
|
|
this.addAssets(values);
|
2023-10-06 22:48:11 +02:00
|
|
|
break;
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
2023-10-06 22:48:11 +02:00
|
|
|
|
2024-02-29 18:44:30 +01:00
|
|
|
case 'update': {
|
2024-03-15 17:11:29 +01:00
|
|
|
this.updateAssets(values);
|
2024-02-29 18:44:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
case 'trash': {
|
2023-10-06 22:48:11 +02:00
|
|
|
if (!this.options.isTrashed) {
|
2024-03-15 17:11:29 +01:00
|
|
|
this.removeAssets(values);
|
2023-10-06 22:48:11 +02:00
|
|
|
}
|
|
|
|
break;
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
2023-10-06 22:48:11 +02:00
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
case 'delete': {
|
2024-03-15 17:11:29 +01:00
|
|
|
this.removeAssets(values);
|
2023-10-06 22:48:11 +02:00
|
|
|
break;
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
2023-10-06 22:48:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.pendingChanges = [];
|
|
|
|
this.emit(true);
|
2024-03-15 17:11:29 +01:00
|
|
|
}, 2500);
|
2023-10-06 22:48:11 +02:00
|
|
|
|
2023-08-03 17:44:12 +02:00
|
|
|
async init(viewport: Viewport) {
|
2023-09-01 19:12:09 +02:00
|
|
|
this.initialized = false;
|
2023-08-11 18:00:51 +02:00
|
|
|
this.timelineHeight = 0;
|
|
|
|
this.buckets = [];
|
|
|
|
this.assets = [];
|
|
|
|
this.assetToBucket = {};
|
|
|
|
this.albumAssets = new Set();
|
|
|
|
|
2024-03-14 17:45:03 +01:00
|
|
|
const buckets = await getTimeBuckets({
|
|
|
|
...this.options,
|
|
|
|
key: getKey(),
|
|
|
|
});
|
2023-08-03 17:44:12 +02:00
|
|
|
|
2023-09-01 19:12:09 +02:00
|
|
|
this.initialized = true;
|
|
|
|
|
2024-03-05 16:43:24 +01:00
|
|
|
this.buckets = buckets.map((bucket) => {
|
|
|
|
const unwrappedWidth = (3 / 2) * bucket.count * THUMBNAIL_HEIGHT * (7 / 10);
|
2023-08-03 17:44:12 +02:00
|
|
|
const rows = Math.ceil(unwrappedWidth / viewport.width);
|
2024-03-05 16:43:24 +01:00
|
|
|
const height = rows * THUMBNAIL_HEIGHT;
|
2023-08-03 17:44:12 +02:00
|
|
|
|
2024-03-05 16:43:24 +01:00
|
|
|
return {
|
|
|
|
bucketDate: bucket.timeBucket,
|
|
|
|
bucketHeight: height,
|
|
|
|
bucketCount: bucket.count,
|
|
|
|
assets: [],
|
|
|
|
cancelToken: null,
|
|
|
|
position: BucketPosition.Unknown,
|
|
|
|
};
|
|
|
|
});
|
2024-03-03 23:12:52 +01:00
|
|
|
|
2024-03-05 16:43:24 +01:00
|
|
|
this.timelineHeight = this.buckets.reduce((accumulator, b) => accumulator + b.bucketHeight, 0);
|
2024-03-03 23:12:52 +01:00
|
|
|
|
|
|
|
this.emit(false);
|
2024-03-05 16:43:24 +01:00
|
|
|
|
|
|
|
let height = 0;
|
|
|
|
const loaders = [];
|
|
|
|
for (const bucket of this.buckets) {
|
|
|
|
if (height < viewport.height) {
|
|
|
|
height += bucket.bucketHeight;
|
|
|
|
loaders.push(this.loadBucket(bucket.bucketDate, BucketPosition.Visible));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
await Promise.all(loaders);
|
2023-08-03 17:44:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async loadBucket(bucketDate: string, position: BucketPosition): Promise<void> {
|
2024-03-20 20:40:41 +01:00
|
|
|
const bucket = this.getBucketByDate(bucketDate);
|
|
|
|
if (!bucket) {
|
|
|
|
return;
|
|
|
|
}
|
2023-08-03 17:44:12 +02:00
|
|
|
|
2024-03-20 20:40:41 +01:00
|
|
|
bucket.position = position;
|
2023-08-03 17:44:12 +02:00
|
|
|
|
2024-03-20 20:40:41 +01:00
|
|
|
if (bucket.cancelToken || bucket.assets.length > 0) {
|
|
|
|
this.emit(false);
|
|
|
|
return;
|
|
|
|
}
|
2023-08-03 17:44:12 +02:00
|
|
|
|
2024-03-20 20:40:41 +01:00
|
|
|
bucket.cancelToken = new AbortController();
|
2023-08-03 17:44:12 +02:00
|
|
|
|
2024-03-20 20:40:41 +01:00
|
|
|
try {
|
2024-02-14 14:09:49 +01:00
|
|
|
const assets = await getTimeBucket(
|
2023-08-25 06:03:28 +02:00
|
|
|
{
|
|
|
|
...this.options,
|
|
|
|
timeBucket: bucketDate,
|
2024-02-14 14:09:49 +01:00
|
|
|
key: getKey(),
|
2023-08-25 06:03:28 +02:00
|
|
|
},
|
2023-08-03 17:44:12 +02:00
|
|
|
{ signal: bucket.cancelToken.signal },
|
|
|
|
);
|
|
|
|
|
2023-08-11 18:00:51 +02:00
|
|
|
if (this.albumId) {
|
2024-02-14 14:09:49 +01:00
|
|
|
const albumAssets = await getTimeBucket(
|
2023-08-11 18:00:51 +02:00
|
|
|
{
|
|
|
|
albumId: this.albumId,
|
|
|
|
timeBucket: bucketDate,
|
|
|
|
size: this.options.size,
|
2024-02-14 14:09:49 +01:00
|
|
|
key: getKey(),
|
2023-08-11 18:00:51 +02:00
|
|
|
},
|
|
|
|
{ signal: bucket.cancelToken.signal },
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const asset of albumAssets) {
|
|
|
|
this.albumAssets.add(asset.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-14 17:24:18 +01:00
|
|
|
if (bucket.cancelToken.signal.aborted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-05 16:43:24 +01:00
|
|
|
bucket.assets = assets;
|
|
|
|
|
2023-08-03 17:44:12 +02:00
|
|
|
this.emit(true);
|
|
|
|
} catch (error) {
|
|
|
|
handleError(error, 'Failed to load assets');
|
2024-03-20 20:40:41 +01:00
|
|
|
} finally {
|
|
|
|
bucket.cancelToken = null;
|
2023-08-03 17:44:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelBucket(bucket: AssetBucket) {
|
|
|
|
bucket.cancelToken?.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateBucket(bucketDate: string, height: number) {
|
|
|
|
const bucket = this.getBucketByDate(bucketDate);
|
|
|
|
if (!bucket) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-05 16:43:24 +01:00
|
|
|
const delta = height - bucket.bucketHeight;
|
2023-08-03 17:44:12 +02:00
|
|
|
const scrollTimeline = bucket.position == BucketPosition.Above;
|
|
|
|
|
2024-03-05 16:43:24 +01:00
|
|
|
bucket.bucketHeight = height;
|
2023-08-03 17:44:12 +02:00
|
|
|
bucket.position = BucketPosition.Unknown;
|
|
|
|
|
|
|
|
this.timelineHeight += delta;
|
|
|
|
|
|
|
|
this.emit(false);
|
|
|
|
|
|
|
|
return scrollTimeline ? delta : 0;
|
|
|
|
}
|
|
|
|
|
2024-03-15 17:11:29 +01:00
|
|
|
addAssets(assets: AssetResponseDto[]) {
|
|
|
|
const assetsToUpdate: AssetResponseDto[] = [];
|
|
|
|
const assetsToAdd: AssetResponseDto[] = [];
|
|
|
|
|
|
|
|
for (const asset of assets) {
|
|
|
|
if (
|
|
|
|
this.assetToBucket[asset.id] ||
|
|
|
|
this.options.userId ||
|
|
|
|
this.options.personId ||
|
|
|
|
this.options.albumId ||
|
|
|
|
isMismatched(this.options.isArchived, asset.isArchived) ||
|
|
|
|
isMismatched(this.options.isFavorite, asset.isFavorite)
|
|
|
|
) {
|
|
|
|
// If asset is already in the bucket we don't need to recalculate
|
|
|
|
// asset store containers
|
|
|
|
assetsToUpdate.push(asset);
|
|
|
|
} else {
|
|
|
|
assetsToAdd.push(asset);
|
|
|
|
}
|
2023-10-06 22:48:11 +02:00
|
|
|
}
|
|
|
|
|
2024-03-15 17:11:29 +01:00
|
|
|
this.updateAssets(assetsToUpdate);
|
|
|
|
this.addAssetsToBuckets(assetsToAdd);
|
2024-02-29 18:44:30 +01:00
|
|
|
}
|
|
|
|
|
2024-03-15 17:11:29 +01:00
|
|
|
private addAssetsToBuckets(assets: AssetResponseDto[]) {
|
|
|
|
if (assets.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const updatedBuckets = new Set<AssetBucket>();
|
2023-10-06 22:48:11 +02:00
|
|
|
|
2024-03-15 17:11:29 +01:00
|
|
|
for (const asset of assets) {
|
|
|
|
const timeBucket = DateTime.fromISO(asset.fileCreatedAt).toUTC().startOf('month').toString();
|
|
|
|
let bucket = this.getBucketByDate(timeBucket);
|
2023-10-06 22:48:11 +02:00
|
|
|
|
2024-03-15 17:11:29 +01:00
|
|
|
if (!bucket) {
|
|
|
|
bucket = {
|
|
|
|
bucketDate: timeBucket,
|
|
|
|
bucketHeight: THUMBNAIL_HEIGHT,
|
|
|
|
bucketCount: 0,
|
|
|
|
assets: [],
|
|
|
|
cancelToken: null,
|
|
|
|
position: BucketPosition.Unknown,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.buckets.push(bucket);
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket.assets.push(asset);
|
|
|
|
this.assets.push(asset);
|
|
|
|
updatedBuckets.add(bucket);
|
2023-10-06 22:48:11 +02:00
|
|
|
}
|
|
|
|
|
2024-03-15 17:11:29 +01:00
|
|
|
this.buckets = this.buckets.sort((a, b) => {
|
|
|
|
const aDate = DateTime.fromISO(a.bucketDate).toUTC();
|
|
|
|
const bDate = DateTime.fromISO(b.bucketDate).toUTC();
|
2024-03-05 16:43:24 +01:00
|
|
|
return bDate.diff(aDate).milliseconds;
|
|
|
|
});
|
2023-11-04 13:59:21 +01:00
|
|
|
|
2024-03-15 17:11:29 +01:00
|
|
|
for (const bucket of updatedBuckets) {
|
|
|
|
bucket.assets.sort((a, b) => {
|
|
|
|
const aDate = DateTime.fromISO(a.fileCreatedAt).toUTC();
|
|
|
|
const bDate = DateTime.fromISO(b.fileCreatedAt).toUTC();
|
|
|
|
return bDate.diff(aDate).milliseconds;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-29 18:44:30 +01:00
|
|
|
this.emit(true);
|
2023-10-06 22:48:11 +02:00
|
|
|
}
|
|
|
|
|
2023-08-03 17:44:12 +02:00
|
|
|
getBucketByDate(bucketDate: string): AssetBucket | null {
|
2024-03-05 16:43:24 +01:00
|
|
|
return this.buckets.find((bucket) => bucket.bucketDate === bucketDate) || null;
|
2023-08-03 17:44:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getBucketInfoForAssetId(assetId: string) {
|
|
|
|
return this.assetToBucket[assetId] || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
getBucketIndexByAssetId(assetId: string) {
|
|
|
|
return this.assetToBucket[assetId]?.bucketIndex ?? null;
|
|
|
|
}
|
|
|
|
|
2023-11-02 02:34:30 +01:00
|
|
|
async getRandomAsset(): Promise<AssetResponseDto | null> {
|
2024-03-05 16:43:24 +01:00
|
|
|
let index = Math.floor(
|
|
|
|
Math.random() * this.buckets.reduce((accumulator, bucket) => accumulator + bucket.bucketCount, 0),
|
|
|
|
);
|
2023-11-12 02:23:15 +01:00
|
|
|
for (const bucket of this.buckets) {
|
2024-03-05 16:43:24 +01:00
|
|
|
if (index < bucket.bucketCount) {
|
|
|
|
await this.loadBucket(bucket.bucketDate, BucketPosition.Unknown);
|
2023-11-12 02:23:15 +01:00
|
|
|
return bucket.assets[index] || null;
|
|
|
|
}
|
2023-11-02 02:34:30 +01:00
|
|
|
|
2024-03-05 16:43:24 +01:00
|
|
|
index -= bucket.bucketCount;
|
2023-11-02 02:34:30 +01:00
|
|
|
}
|
|
|
|
|
2023-11-12 02:23:15 +01:00
|
|
|
return null;
|
2023-11-02 02:34:30 +01:00
|
|
|
}
|
|
|
|
|
2024-03-15 17:11:29 +01:00
|
|
|
updateAssets(assets: AssetResponseDto[]) {
|
|
|
|
if (assets.length === 0) {
|
2023-08-03 17:44:12 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-03-15 17:11:29 +01:00
|
|
|
const assetsToReculculate: AssetResponseDto[] = [];
|
2023-08-03 17:44:12 +02:00
|
|
|
|
2024-03-15 17:11:29 +01:00
|
|
|
for (const _asset of assets) {
|
|
|
|
const asset = this.assets.find((asset) => asset.id === _asset.id);
|
|
|
|
if (!asset) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const recalculate = asset.fileCreatedAt !== _asset.fileCreatedAt;
|
|
|
|
Object.assign(asset, _asset);
|
|
|
|
|
|
|
|
if (recalculate) {
|
|
|
|
assetsToReculculate.push(asset);
|
|
|
|
}
|
2024-02-29 18:44:30 +01:00
|
|
|
}
|
2023-08-05 05:26:28 +02:00
|
|
|
|
2024-03-15 17:11:29 +01:00
|
|
|
this.removeAssets(assetsToReculculate.map((asset) => asset.id));
|
|
|
|
this.addAssetsToBuckets(assetsToReculculate);
|
|
|
|
this.emit(assetsToReculculate.length > 0);
|
2023-08-03 17:44:12 +02:00
|
|
|
}
|
|
|
|
|
2023-08-16 22:04:55 +02:00
|
|
|
removeAssets(ids: string[]) {
|
2024-03-02 01:49:31 +01:00
|
|
|
const idSet = new Set(ids);
|
2024-02-29 18:44:30 +01:00
|
|
|
|
2024-03-02 01:49:31 +01:00
|
|
|
// Iterate in reverse to allow array splicing.
|
|
|
|
for (let index = this.buckets.length - 1; index >= 0; index--) {
|
2024-02-02 04:18:00 +01:00
|
|
|
const bucket = this.buckets[index];
|
2024-03-02 01:49:31 +01:00
|
|
|
for (let index_ = bucket.assets.length - 1; index_ >= 0; index_--) {
|
2024-02-02 04:18:00 +01:00
|
|
|
const asset = bucket.assets[index_];
|
2024-03-02 01:49:31 +01:00
|
|
|
if (!idSet.has(asset.id)) {
|
2023-08-03 17:44:12 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
bucket.assets.splice(index_, 1);
|
2024-03-16 14:22:15 +01:00
|
|
|
if (bucket.assets.length === 0) {
|
2024-02-02 04:18:00 +01:00
|
|
|
this.buckets.splice(index, 1);
|
2023-08-03 17:44:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-02 01:49:31 +01:00
|
|
|
|
2024-03-16 14:22:15 +01:00
|
|
|
this.emit(true);
|
2023-08-03 17:44:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
2024-03-05 16:43:24 +01:00
|
|
|
await this.loadBucket(previousBucket.bucketDate, BucketPosition.Unknown);
|
2023-08-03 17:44:12 +02:00
|
|
|
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];
|
2024-03-05 16:43:24 +01:00
|
|
|
await this.loadBucket(nextBucket.bucketDate, BucketPosition.Unknown);
|
2023-08-03 17:44:12 +02:00
|
|
|
return nextBucket.assets[0]?.id || null;
|
|
|
|
}
|
|
|
|
|
2024-02-20 17:01:52 +01:00
|
|
|
triggerUpdate() {
|
|
|
|
this.emit(false);
|
|
|
|
}
|
|
|
|
|
2023-08-03 17:44:12 +02:00
|
|
|
private emit(recalculate: boolean) {
|
|
|
|
if (recalculate) {
|
|
|
|
this.assets = this.buckets.flatMap(({ assets }) => assets);
|
|
|
|
|
|
|
|
const assetToBucket: Record<string, AssetLookup> = {};
|
2024-02-02 04:18:00 +01:00
|
|
|
for (let index = 0; index < this.buckets.length; index++) {
|
|
|
|
const bucket = this.buckets[index];
|
|
|
|
if (bucket.assets.length > 0) {
|
2024-03-05 16:43:24 +01:00
|
|
|
bucket.bucketCount = bucket.assets.length;
|
2023-11-12 02:23:15 +01:00
|
|
|
}
|
2024-02-02 04:18:00 +01:00
|
|
|
for (let index_ = 0; index_ < bucket.assets.length; index_++) {
|
|
|
|
const asset = bucket.assets[index_];
|
|
|
|
assetToBucket[asset.id] = { bucket, bucketIndex: index, assetIndex: index_ };
|
2023-08-03 17:44:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.assetToBucket = assetToBucket;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.store$.update(() => this);
|
|
|
|
}
|
2022-09-04 15:34:39 +02:00
|
|
|
}
|
2024-01-01 17:18:22 +01:00
|
|
|
|
2024-03-21 13:14:13 +01:00
|
|
|
export const isSelectingAllAssets = writable(false);
|