2022-09-04 15:34:39 +02:00
|
|
|
import { AssetGridState } from '$lib/models/asset-grid-state';
|
|
|
|
import { calculateViewportHeightByNumberOfAsset } from '$lib/utils/viewport-utils';
|
2023-01-27 15:32:26 +01:00
|
|
|
import { api, AssetCountByTimeBucketResponseDto } from '@api';
|
2023-02-13 20:18:11 +01:00
|
|
|
import { sumBy, flatMap } from 'lodash-es';
|
2023-01-27 15:32:26 +01:00
|
|
|
import { writable } from 'svelte/store';
|
2022-09-04 15:34:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The state that holds information about the asset grid
|
|
|
|
*/
|
|
|
|
export const assetGridState = writable<AssetGridState>(new AssetGridState());
|
|
|
|
export const loadingBucketState = writable<{ [key: string]: boolean }>({});
|
|
|
|
|
|
|
|
function createAssetStore() {
|
|
|
|
let _assetGridState = new AssetGridState();
|
|
|
|
assetGridState.subscribe((state) => {
|
|
|
|
_assetGridState = state;
|
|
|
|
});
|
|
|
|
|
|
|
|
let _loadingBucketState: { [key: string]: boolean } = {};
|
|
|
|
loadingBucketState.subscribe((state) => {
|
|
|
|
_loadingBucketState = state;
|
|
|
|
});
|
|
|
|
/**
|
2022-09-08 17:30:49 +02:00
|
|
|
* Set initial state
|
2022-09-04 15:34:39 +02:00
|
|
|
* @param viewportHeight
|
|
|
|
* @param viewportWidth
|
|
|
|
* @param data
|
|
|
|
*/
|
|
|
|
const setInitialState = (
|
|
|
|
viewportHeight: number,
|
|
|
|
viewportWidth: number,
|
2023-05-15 19:30:53 +02:00
|
|
|
data: AssetCountByTimeBucketResponseDto,
|
|
|
|
userId: string | undefined
|
2022-09-04 15:34:39 +02:00
|
|
|
) => {
|
|
|
|
assetGridState.set({
|
|
|
|
viewportHeight,
|
|
|
|
viewportWidth,
|
2022-09-09 22:55:20 +02:00
|
|
|
timelineHeight: 0,
|
2022-09-04 15:34:39 +02:00
|
|
|
buckets: data.buckets.map((d) => ({
|
|
|
|
bucketDate: d.timeBucket,
|
|
|
|
bucketHeight: calculateViewportHeightByNumberOfAsset(d.count, viewportWidth),
|
|
|
|
assets: [],
|
|
|
|
cancelToken: new AbortController()
|
|
|
|
})),
|
2023-05-15 19:30:53 +02:00
|
|
|
assets: [],
|
|
|
|
userId
|
2022-09-04 15:34:39 +02:00
|
|
|
});
|
2022-09-09 22:55:20 +02:00
|
|
|
|
|
|
|
// Update timeline height based on calculated bucket height
|
|
|
|
assetGridState.update((state) => {
|
2023-02-13 20:18:11 +01:00
|
|
|
state.timelineHeight = sumBy(state.buckets, (d) => d.bucketHeight);
|
2022-09-09 22:55:20 +02:00
|
|
|
return state;
|
|
|
|
});
|
2022-09-04 15:34:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const getAssetsByBucket = async (bucket: string) => {
|
|
|
|
try {
|
|
|
|
const currentBucketData = _assetGridState.buckets.find((b) => b.bucketDate === bucket);
|
|
|
|
if (currentBucketData?.assets && currentBucketData.assets.length > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadingBucketState.set({
|
|
|
|
..._loadingBucketState,
|
|
|
|
[bucket]: true
|
|
|
|
});
|
|
|
|
const { data: assets } = await api.assetApi.getAssetByTimeBucket(
|
|
|
|
{
|
2023-05-28 03:52:22 +02:00
|
|
|
getAssetByTimeBucketDto: {
|
|
|
|
timeBucket: [bucket],
|
|
|
|
userId: _assetGridState.userId,
|
|
|
|
withoutThumbs: true
|
|
|
|
}
|
2022-09-04 15:34:39 +02:00
|
|
|
},
|
|
|
|
{ signal: currentBucketData?.cancelToken.signal }
|
|
|
|
);
|
|
|
|
loadingBucketState.set({
|
|
|
|
..._loadingBucketState,
|
|
|
|
[bucket]: false
|
|
|
|
});
|
|
|
|
|
|
|
|
// Update assetGridState with assets by time bucket
|
|
|
|
assetGridState.update((state) => {
|
|
|
|
const bucketIndex = state.buckets.findIndex((b) => b.bucketDate === bucket);
|
|
|
|
state.buckets[bucketIndex].assets = assets;
|
2023-02-13 20:18:11 +01:00
|
|
|
state.assets = flatMap(state.buckets, (b) => b.assets);
|
2022-09-04 15:34:39 +02:00
|
|
|
|
|
|
|
return state;
|
|
|
|
});
|
2022-09-08 17:30:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2022-09-04 15:34:39 +02:00
|
|
|
} catch (e: any) {
|
|
|
|
if (e.name === 'CanceledError') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.error('Failed to get asset for bucket ', bucket);
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeAsset = (assetId: string) => {
|
|
|
|
assetGridState.update((state) => {
|
|
|
|
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-02-13 20:18:11 +01:00
|
|
|
state.assets = flatMap(state.buckets, (b) => b.assets);
|
2022-09-04 15:34:39 +02:00
|
|
|
return state;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const _removeBucket = (bucketDate: string) => {
|
|
|
|
assetGridState.update((state) => {
|
|
|
|
const bucketIndex = state.buckets.findIndex((b) => b.bucketDate === bucketDate);
|
|
|
|
state.buckets.splice(bucketIndex, 1);
|
2023-02-13 20:18:11 +01:00
|
|
|
state.assets = flatMap(state.buckets, (b) => b.assets);
|
2022-09-04 15:34:39 +02:00
|
|
|
return state;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-09-09 22:55:20 +02:00
|
|
|
const updateBucketHeight = (bucket: string, actualBucketHeight: number) => {
|
2022-09-04 15:34:39 +02:00
|
|
|
assetGridState.update((state) => {
|
|
|
|
const bucketIndex = state.buckets.findIndex((b) => b.bucketDate === bucket);
|
2022-09-09 22:55:20 +02:00
|
|
|
// Update timeline height based on the new bucket height
|
|
|
|
const estimateBucketHeight = state.buckets[bucketIndex].bucketHeight;
|
|
|
|
|
|
|
|
if (actualBucketHeight >= estimateBucketHeight) {
|
|
|
|
state.timelineHeight += actualBucketHeight - estimateBucketHeight;
|
|
|
|
} else {
|
|
|
|
state.timelineHeight -= estimateBucketHeight - actualBucketHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.buckets[bucketIndex].bucketHeight = actualBucketHeight;
|
2022-09-04 15:34:39 +02:00
|
|
|
return state;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const cancelBucketRequest = async (token: AbortController, bucketDate: string) => {
|
|
|
|
token.abort();
|
|
|
|
// set new abort controller for bucket
|
|
|
|
assetGridState.update((state) => {
|
|
|
|
const bucketIndex = state.buckets.findIndex((b) => b.bucketDate === bucketDate);
|
|
|
|
state.buckets[bucketIndex].cancelToken = new AbortController();
|
|
|
|
return state;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-01-27 15:32:26 +01:00
|
|
|
const updateAsset = (assetId: string, isFavorite: boolean) => {
|
|
|
|
assetGridState.update((state) => {
|
|
|
|
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-02-13 20:18:11 +01:00
|
|
|
state.assets = flatMap(state.buckets, (b) => b.assets);
|
2023-01-27 15:32:26 +01:00
|
|
|
return state;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-09-04 15:34:39 +02:00
|
|
|
return {
|
|
|
|
setInitialState,
|
|
|
|
getAssetsByBucket,
|
|
|
|
removeAsset,
|
|
|
|
updateBucketHeight,
|
2023-01-27 15:32:26 +01:00
|
|
|
cancelBucketRequest,
|
|
|
|
updateAsset
|
2022-09-04 15:34:39 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const assetStore = createAssetStore();
|