Skip to content

Commit

Permalink
refactor(web): asset grid state (immich-app#3513)
Browse files Browse the repository at this point in the history
* refactor(web): asset grid state

* fix: multi-select across time buckets

---------

Co-authored-by: Alex Tran <[email protected]>
  • Loading branch information
jrasm91 and alextran1502 authored Aug 3, 2023
1 parent 050ee91 commit 28ab1d4
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
const assetGridState = get(assetStore);
for (const bucket of assetGridState.buckets) {
await assetStore.getAssetsByBucket(bucket.bucketDate, BucketPosition.Unknown);
await assetStore.loadBucket(bucket.bucketDate, BucketPosition.Unknown);
for (const asset of bucket.assets) {
assetInteractionStore.addAssetToMultiselectGroup(asset);
}
Expand Down
2 changes: 1 addition & 1 deletion web/src/lib/components/photos-page/asset-date-group.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
$: {
if (actualBucketHeight && actualBucketHeight != 0 && actualBucketHeight != bucketHeight) {
const heightDelta = assetStore.updateBucketHeight(bucketDate, actualBucketHeight);
const heightDelta = assetStore.updateBucket(bucketDate, actualBucketHeight);
if (heightDelta !== 0) {
scrollTimeline(heightDelta);
}
Expand Down
58 changes: 20 additions & 38 deletions web/src/lib/components/photos-page/asset-grid.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { locale } from '$lib/stores/preferences.store';
import { formatGroupTitle, splitBucketIntoDateGroups } from '$lib/utils/timeline-util';
import type { UserResponseDto } from '@api';
import { api, AssetCountByTimeBucketResponseDto, AssetResponseDto, TimeGroupEnum } from '@api';
import { AssetResponseDto, TimeGroupEnum, api } from '@api';
import { DateTime } from 'luxon';
import { onDestroy, onMount } from 'svelte';
import AssetViewer from '../asset-viewer/asset-viewer.svelte';
Expand All @@ -17,13 +17,13 @@
import AssetDateGroup from './asset-date-group.svelte';
import MemoryLane from './memory-lane.svelte';
import { AppRoute } from '$lib/constants';
import { goto } from '$app/navigation';
import { browser } from '$app/environment';
import { goto } from '$app/navigation';
import { AppRoute } from '$lib/constants';
import type { AssetInteractionStore } from '$lib/stores/asset-interaction.store';
import type { AssetStore } from '$lib/stores/assets.store';
import { isSearchEnabled } from '$lib/stores/search.store';
import ShowShortcuts from '../shared-components/show-shortcuts.svelte';
import type { AssetStore } from '$lib/stores/assets.store';
import type { AssetInteractionStore } from '$lib/stores/asset-interaction.store';
export let user: UserResponseDto | undefined = undefined;
export let isAlbumSelectionMode = false;
Expand All @@ -39,49 +39,29 @@
let viewportHeight = 0;
let viewportWidth = 0;
let assetGridElement: HTMLElement;
let bucketInfo: AssetCountByTimeBucketResponseDto;
let showShortcuts = false;
const onKeyboardPress = (event: KeyboardEvent) => handleKeyboardPress(event);
onMount(async () => {
document.addEventListener('keydown', onKeyboardPress);
const { data: assetCountByTimebucket } = await api.assetApi.getAssetCountByTimeBucket({
const { data: timeBuckets } = await api.assetApi.getAssetCountByTimeBucket({
getAssetCountByTimeBucketDto: {
timeGroup: TimeGroupEnum.Month,
userId: user?.id,
withoutThumbs: true,
},
});
bucketInfo = assetCountByTimebucket;
assetStore.setInitialState(viewportHeight, viewportWidth, assetCountByTimebucket, user?.id);
// Get asset bucket if bucket height is smaller than viewport height
let bucketsToFetchInitially: string[] = [];
let initialBucketsHeight = 0;
$assetStore.buckets.every((bucket) => {
if (initialBucketsHeight < viewportHeight) {
initialBucketsHeight += bucket.bucketHeight;
bucketsToFetchInitially.push(bucket.bucketDate);
return true;
} else {
return false;
}
});
bucketsToFetchInitially.forEach((bucketDate) => {
assetStore.getAssetsByBucket(bucketDate, BucketPosition.Visible);
});
assetStore.init({ width: viewportHeight, height: viewportWidth }, timeBuckets.buckets, user?.id);
});
onDestroy(() => {
if (browser) {
document.removeEventListener('keydown', onKeyboardPress);
}
assetStore.setInitialState(0, 0, { totalCount: 0, buckets: [] }, undefined);
assetStore.init({ width: 0, height: 0 }, [], undefined);
});
const handleKeyboardPress = (event: KeyboardEvent) => {
Expand Down Expand Up @@ -113,7 +93,7 @@
const target = el.firstChild as HTMLElement;
if (target) {
const bucketDate = target.id.split('_')[1];
assetStore.getAssetsByBucket(bucketDate, event.detail.position);
assetStore.loadBucket(bucketDate, event.detail.position);
}
}
Expand All @@ -122,14 +102,14 @@
}
const navigateToPreviousAsset = async () => {
const prevAsset = await assetStore.getAdjacentAsset($viewingAsset.id, 'previous');
const prevAsset = await assetStore.getPreviousAssetId($viewingAsset.id);
if (prevAsset) {
assetViewingStore.setAssetId(prevAsset);
}
};
const navigateToNextAsset = async () => {
const nextAsset = await assetStore.getAdjacentAsset($viewingAsset.id, 'next');
const nextAsset = await assetStore.getNextAssetId($viewingAsset.id);
if (nextAsset) {
assetViewingStore.setAssetId(nextAsset);
}
Expand Down Expand Up @@ -234,8 +214,12 @@
assetInteractionStore.clearAssetSelectionCandidates();
if ($assetSelectionStart && rangeSelection) {
let startBucketIndex = $assetStore.loadedAssets[$assetSelectionStart.id];
let endBucketIndex = $assetStore.loadedAssets[asset.id];
let startBucketIndex = $assetStore.getBucketIndexByAssetId($assetSelectionStart.id);
let endBucketIndex = $assetStore.getBucketIndexByAssetId(asset.id);
if (startBucketIndex === null || endBucketIndex === null) {
return;
}
if (endBucketIndex < startBucketIndex) {
[startBucketIndex, endBucketIndex] = [endBucketIndex, startBucketIndex];
Expand All @@ -244,7 +228,7 @@
// Select/deselect assets in all intermediate buckets
for (let bucketIndex = startBucketIndex + 1; bucketIndex < endBucketIndex; bucketIndex++) {
const bucket = $assetStore.buckets[bucketIndex];
await assetStore.getAssetsByBucket(bucket.bucketDate, BucketPosition.Unknown);
await assetStore.loadBucket(bucket.bucketDate, BucketPosition.Unknown);
for (const asset of bucket.assets) {
if (deselect) {
assetInteractionStore.removeAssetFromMultiselectGroup(asset);
Expand Down Expand Up @@ -308,7 +292,7 @@
<ShowShortcuts on:close={() => (showShortcuts = !showShortcuts)} />
{/if}

{#if bucketInfo && viewportHeight && $assetStore.timelineHeight > viewportHeight}
{#if viewportHeight && $assetStore.initialized && $assetStore.timelineHeight > viewportHeight}
<Scrollbar
{assetStore}
scrollbarHeight={viewportHeight}
Expand All @@ -335,9 +319,7 @@
{#each $assetStore.buckets as bucket, bucketIndex (bucketIndex)}
<IntersectionObserver
on:intersected={intersectedHandler}
on:hidden={async () => {
await assetStore.cancelBucketRequest(bucket.cancelToken, bucket.bucketDate);
}}
on:hidden={() => assetStore.cancelBucket(bucket)}
let:intersecting
top={750}
bottom={750}
Expand Down
Loading

0 comments on commit 28ab1d4

Please sign in to comment.