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
|
|
|
|
*/
|
|
|
|
bucketHeight!: number;
|
|
|
|
bucketDate!: string;
|
2023-11-12 02:23:15 +01:00
|
|
|
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';
|
|
|
|
value: AssetResponseDto;
|
|
|
|
}
|
|
|
|
|
2024-02-29 18:44:30 +01:00
|
|
|
interface UpdateAsset {
|
|
|
|
type: 'update';
|
|
|
|
value: AssetResponseDto;
|
|
|
|
}
|
|
|
|
|
2023-10-06 22:48:11 +02:00
|
|
|
interface DeleteAsset {
|
|
|
|
type: 'delete';
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
2024-03-02 01:49:31 +01:00
|
|
|
interface TrashAssets {
|
2023-10-06 22:48:11 +02:00
|
|
|
type: 'trash';
|
2024-03-02 01:49:31 +01:00
|
|
|
value: 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) => {
|
|
|
|
this.addPendingChanges({ type: 'add', value: 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-02 01:49:31 +01:00
|
|
|
this.addPendingChanges({ type: 'trash', value: ids });
|
2023-10-06 22:48:11 +02:00
|
|
|
}),
|
2024-02-29 18:44:30 +01:00
|
|
|
websocketEvents.on('on_asset_update', (asset) => {
|
|
|
|
this.addPendingChanges({ type: 'update', value: asset });
|
|
|
|
}),
|
2024-02-16 21:43:40 +01:00
|
|
|
websocketEvents.on('on_asset_delete', (id: string) => {
|
|
|
|
this.addPendingChanges({ type: 'delete', value: id });
|
2023-10-06 22:48:11 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
for (const unsubscribe of this.unsubscribers) {
|
|
|
|
unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
processPendingChanges = throttle(() => {
|
|
|
|
for (const { type, value } of this.pendingChanges) {
|
|
|
|
switch (type) {
|
2024-02-02 04:18:00 +01:00
|
|
|
case 'add': {
|
2023-10-06 22:48:11 +02:00
|
|
|
this.addAsset(value);
|
|
|
|
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': {
|
|
|
|
this.updateAsset(value);
|
|
|
|
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-02 01:49:31 +01:00
|
|
|
this.removeAssets(value);
|
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-02 01:49:31 +01:00
|
|
|
this.removeAssets([value]);
|
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);
|
|
|
|
}, 10_000);
|
|
|
|
|
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-02-14 15:38:57 +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;
|
|
|
|
|
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,
|
2023-11-12 02:23:15 +01:00
|
|
|
bucketCount: bucket.count,
|
2023-08-03 17:44:12 +02:00
|
|
|
assets: [],
|
|
|
|
cancelToken: null,
|
|
|
|
position: BucketPosition.Unknown,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
this.timelineHeight = this.buckets.reduce((accumulator, b) => accumulator + b.bucketHeight, 0);
|
2023-08-03 17:44:12 +02:00
|
|
|
|
|
|
|
this.emit(false);
|
|
|
|
|
|
|
|
let height = 0;
|
2024-02-27 17:37:37 +01:00
|
|
|
const loaders = [];
|
2023-08-03 17:44:12 +02:00
|
|
|
for (const bucket of this.buckets) {
|
|
|
|
if (height < viewport.height) {
|
|
|
|
height += bucket.bucketHeight;
|
2024-02-27 17:37:37 +01:00
|
|
|
loaders.push(this.loadBucket(bucket.bucketDate, BucketPosition.Visible));
|
2023-08-03 17:44:12 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2024-02-27 17:37:37 +01:00
|
|
|
await Promise.all(loaders);
|
2023-08-03 17:44:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async loadBucket(bucketDate: string, position: BucketPosition): Promise<void> {
|
|
|
|
try {
|
|
|
|
const bucket = this.getBucketByDate(bucketDate);
|
|
|
|
if (!bucket) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket.position = position;
|
|
|
|
|
2024-03-01 20:16:07 +01:00
|
|
|
if (bucket.assets.length > 0 || bucket.cancelToken) {
|
2023-08-03 17:44:12 +02:00
|
|
|
this.emit(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket.cancelToken = new AbortController();
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-03 17:44:12 +02:00
|
|
|
bucket.assets = assets;
|
2023-10-27 22:34:01 +02:00
|
|
|
|
2023-08-03 17:44:12 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-10-27 22:34:01 +02:00
|
|
|
addAsset(asset: AssetResponseDto): void {
|
2023-10-06 22:48:11 +02:00
|
|
|
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)
|
|
|
|
) {
|
2023-11-04 13:59:21 +01:00
|
|
|
// If asset is already in the bucket we don't need to recalculate
|
|
|
|
// asset store containers
|
|
|
|
this.updateAsset(asset);
|
2023-10-06 22:48:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-29 18:44:30 +01:00
|
|
|
this.addAssetToBucket(asset);
|
|
|
|
}
|
|
|
|
|
|
|
|
private addAssetToBucket(asset: AssetResponseDto) {
|
2023-10-06 22:48:11 +02:00
|
|
|
const timeBucket = DateTime.fromISO(asset.fileCreatedAt).toUTC().startOf('month').toString();
|
|
|
|
let bucket = this.getBucketByDate(timeBucket);
|
|
|
|
|
|
|
|
if (!bucket) {
|
|
|
|
bucket = {
|
|
|
|
bucketDate: timeBucket,
|
|
|
|
bucketHeight: THUMBNAIL_HEIGHT,
|
2023-11-12 02:23:15 +01:00
|
|
|
bucketCount: 0,
|
2023-10-06 22:48:11 +02:00
|
|
|
assets: [],
|
|
|
|
cancelToken: null,
|
|
|
|
position: BucketPosition.Unknown,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.buckets.push(bucket);
|
|
|
|
this.buckets = this.buckets.sort((a, b) => {
|
|
|
|
const aDate = DateTime.fromISO(a.bucketDate).toUTC();
|
|
|
|
const bDate = DateTime.fromISO(b.bucketDate).toUTC();
|
|
|
|
return bDate.diff(aDate).milliseconds;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket.assets.push(asset);
|
|
|
|
bucket.assets.sort((a, b) => {
|
|
|
|
const aDate = DateTime.fromISO(a.fileCreatedAt).toUTC();
|
|
|
|
const bDate = DateTime.fromISO(b.fileCreatedAt).toUTC();
|
|
|
|
return bDate.diff(aDate).milliseconds;
|
|
|
|
});
|
2023-11-04 13:59:21 +01:00
|
|
|
|
|
|
|
// If we added an asset to the store, we need to recalculate
|
|
|
|
// asset store containers
|
|
|
|
this.assets.push(asset);
|
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 {
|
|
|
|
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-11-02 02:34:30 +01:00
|
|
|
async getRandomAsset(): Promise<AssetResponseDto | null> {
|
2024-02-02 04:18:00 +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) {
|
|
|
|
if (index < bucket.bucketCount) {
|
|
|
|
await this.loadBucket(bucket.bucketDate, BucketPosition.Unknown);
|
|
|
|
return bucket.assets[index] || null;
|
|
|
|
}
|
2023-11-02 02:34:30 +01:00
|
|
|
|
2023-11-12 02:23:15 +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-02-29 18:44:30 +01:00
|
|
|
updateAsset(_asset: AssetResponseDto) {
|
2023-08-05 05:26:28 +02:00
|
|
|
const asset = this.assets.find((asset) => asset.id === _asset.id);
|
2023-08-03 17:44:12 +02:00
|
|
|
if (!asset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-29 18:44:30 +01:00
|
|
|
const recalculate = asset.fileCreatedAt !== _asset.fileCreatedAt;
|
|
|
|
if (recalculate) {
|
2024-03-02 01:49:31 +01:00
|
|
|
this.removeAssets([asset.id]);
|
2024-02-29 18:44:30 +01:00
|
|
|
this.addAssetToBucket(_asset);
|
|
|
|
return;
|
|
|
|
}
|
2023-08-05 05:26:28 +02:00
|
|
|
|
2024-02-29 18:44:30 +01:00
|
|
|
Object.assign(asset, _asset);
|
2023-11-03 15:01:48 +01:00
|
|
|
this.emit(recalculate);
|
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);
|
|
|
|
this.assets = this.assets.filter((asset) => !idSet.has(asset.id));
|
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-02 01:49:31 +01:00
|
|
|
bucket.bucketCount = bucket.assets.length;
|
|
|
|
if (bucket.bucketCount === 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
|
|
|
delete this.assetToBucket[asset.id];
|
2023-08-03 17:44:12 +02:00
|
|
|
}
|
|
|
|
}
|
2024-03-02 01:49:31 +01:00
|
|
|
|
|
|
|
this.emit(false);
|
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];
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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) {
|
2023-11-12 02:23:15 +01:00
|
|
|
bucket.bucketCount = bucket.assets.length;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
export const isSelectAllCancelled = writable(false);
|