Skip to content

Commit

Permalink
Slideshow no longer needs assetStore. It is enabled everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
matitalatina committed Jan 4, 2025
1 parent c792f72 commit 67508be
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 41 deletions.
52 changes: 22 additions & 30 deletions web/src/lib/components/asset-viewer/asset-viewer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import { updateNumberOfComments } from '$lib/stores/activity.store';
import { closeEditorCofirm } from '$lib/stores/asset-editor.store';
import { assetViewingStore } from '$lib/stores/asset-viewing.store';
import type { AssetStore } from '$lib/stores/assets.store';
import { isShowDetail } from '$lib/stores/preferences.store';
import { SlideshowNavigation, SlideshowState, slideshowStore } from '$lib/stores/slideshow.store';
import { user } from '$lib/stores/user.store';
Expand Down Expand Up @@ -49,8 +48,9 @@
import VideoViewer from './video-wrapper-viewer.svelte';
import ImagePanoramaViewer from './image-panorama-viewer.svelte';
type HasAsset = boolean;
interface Props {
assetStore?: AssetStore | null;
asset: AssetResponseDto;
preloadAssets?: AssetResponseDto[];
showNavigation?: boolean;
Expand All @@ -61,13 +61,13 @@
onAction?: OnAction | undefined;
reactions?: ActivityResponseDto[];
onClose: (dto: { asset: AssetResponseDto }) => void;
onNext: () => void;
onPrevious: () => void;
onNext: () => Promise<HasAsset>;
onPrevious: () => Promise<HasAsset>;
onRandom: () => Promise<AssetResponseDto | null>;
copyImage?: () => Promise<void>;
}
let {
assetStore = null,
asset = $bindable(),
preloadAssets = $bindable([]),
showNavigation = true,
Expand All @@ -80,6 +80,7 @@
onClose,
onNext,
onPrevious,
onRandom,
copyImage = $bindable(),
}: Props = $props();
Expand Down Expand Up @@ -271,22 +272,6 @@
});
};
const navigateAssetRandom = async () => {
if (!assetStore) {
return;
}
const asset = await assetStore.getRandomAsset();
if (!asset) {
return;
}
slideshowHistory.queue(asset);
setAsset(asset);
$restartSlideshowProgress = true;
};
const navigateAsset = async (order?: 'previous' | 'next', e?: Event) => {
if (!order) {
if ($slideshowState === SlideshowState.PlaySlideshow) {
Expand All @@ -296,23 +281,30 @@
}
}
e?.stopPropagation();
let hasNext = false;
if ($slideshowState === SlideshowState.PlaySlideshow && $slideshowNavigation === SlideshowNavigation.Shuffle) {
return (order === 'previous' ? slideshowHistory.previous() : slideshowHistory.next()) || navigateAssetRandom();
hasNext = (order === 'previous' ? slideshowHistory.previous() : slideshowHistory.next())
if (!hasNext) {
const asset = await onRandom();
if (asset) {
slideshowHistory.queue(asset);
hasNext = true;
}
}
} else {
hasNext = order === 'previous' ? await onPrevious() : await onNext();
}
if ($slideshowState === SlideshowState.PlaySlideshow && assetStore) {
const hasNext =
order === 'previous' ? await assetStore.getPreviousAsset(asset) : await assetStore.getNextAsset(asset);
if ($slideshowState === SlideshowState.PlaySlideshow) {
if (hasNext) {
$restartSlideshowProgress = true;
} else {
await handleStopSlideshow();
}
}
e?.stopPropagation();
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
order === 'previous' ? onPrevious() : onNext();
};
// const showEditorHandler = () => {
Expand Down Expand Up @@ -435,7 +427,7 @@
{person}
{stack}
showDetailButton={enableDetailPanel}
showSlideshow={!!assetStore}
showSlideshow={true}
onZoomImage={zoomToggle}
onCopyImage={copyImage}
onAction={handleAction}
Expand Down
14 changes: 13 additions & 1 deletion web/src/lib/components/photos-page/asset-grid.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,18 @@
return !!nextAsset;
};
const handleRandom = async () => {
const randomAsset = await $assetStore.getRandomAsset();
if (randomAsset) {
const preloadAsset = await $assetStore.getNextAsset(randomAsset);
assetViewingStore.setAsset(randomAsset, preloadAsset ? [preloadAsset] : []);
await navigate({ targetRoute: 'current', assetId: randomAsset.id });
}
return randomAsset;
};
const handleClose = async ({ asset }: { asset: AssetResponseDto }) => {
assetViewingStore.showAssetViewer(false);
showSkeleton = true;
Expand Down Expand Up @@ -911,7 +923,6 @@
{#await import('../asset-viewer/asset-viewer.svelte') then { default: AssetViewer }}
<AssetViewer
{withStacked}
{assetStore}
asset={$viewingAsset}
preloadAssets={$preloadAssets}
{isShared}
Expand All @@ -920,6 +931,7 @@
onAction={handleAction}
onPrevious={handlePrevious}
onNext={handleNext}
onRandom={handleRandom}
onClose={handleClose}
/>
{/await}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
isShowDeleteConfirmation?: boolean;
onPrevious?: (() => Promise<AssetResponseDto | undefined>) | undefined;
onNext?: (() => Promise<AssetResponseDto | undefined>) | undefined;
onRandom?: (() => Promise<AssetResponseDto | undefined>) | undefined;
}
let {
Expand All @@ -47,6 +48,7 @@
isShowDeleteConfirmation = $bindable(false),
onPrevious = undefined,
onNext = undefined,
onRandom = undefined,
}: Props = $props();
let { isViewing: isViewerOpen, asset: viewingAsset, setAsset } = assetViewingStore;
Expand Down Expand Up @@ -202,35 +204,79 @@
})(),
);
const handleNext = async () => {
const handleNext = async (): Promise<boolean> => {
try {
let asset: AssetResponseDto | undefined;
if (onNext) {
asset = await onNext();
} else {
currentViewAssetIndex = Math.min(currentViewAssetIndex + 1, assets.length - 1);
asset = assets[currentViewAssetIndex];
currentViewAssetIndex = currentViewAssetIndex + 1;
if (currentViewAssetIndex >= assets.length) {

Check failure on line 214 in web/src/lib/components/shared-components/gallery-viewer/gallery-viewer.svelte

View workflow job for this annotation

GitHub Actions / Test & Lint Web

This `if` statement can be replaced by a ternary expression
asset = undefined;
} else {
asset = assets[currentViewAssetIndex];
}
}
if (!asset) {
return false;
}
await navigateToAsset(asset);
return true;
} catch (error) {
handleError(error, $t('errors.cannot_navigate_next_asset'));
return false;
}
};
const handleRandom = async (): Promise<AssetResponseDto | null> => {
try {
let asset: AssetResponseDto | undefined;
if (onRandom) {
asset = await onRandom();
} else {
if (assets.length > 0) {
const randomIndex = Math.floor(Math.random() * assets.length);
asset = assets[randomIndex];
}
}
if (!asset) {
return null;
}
await navigateToAsset(asset);
return asset;
} catch (error) {
handleError(error, $t('errors.cannot_navigate_next_asset'));
return null;
}
};
const handlePrevious = async () => {
const handlePrevious = async (): Promise<boolean> => {
try {
let asset: AssetResponseDto | undefined;
if (onPrevious) {
asset = await onPrevious();
} else {
currentViewAssetIndex = Math.max(currentViewAssetIndex - 1, 0);
asset = assets[currentViewAssetIndex];
currentViewAssetIndex = currentViewAssetIndex - 1;
if (currentViewAssetIndex < 0) {

Check failure on line 264 in web/src/lib/components/shared-components/gallery-viewer/gallery-viewer.svelte

View workflow job for this annotation

GitHub Actions / Test & Lint Web

This `if` statement can be replaced by a ternary expression
asset = undefined;
} else {
asset = assets[currentViewAssetIndex];
}
}
if (!asset) {
return false;
}
await navigateToAsset(asset);
return true;
} catch (error) {
handleError(error, $t('errors.cannot_navigate_previous_asset'));
return false;
}
};
Expand Down Expand Up @@ -372,6 +418,7 @@
onAction={handleAction}
onPrevious={handlePrevious}
onNext={handleNext}
onRandom={handleRandom}
onClose={() => {
assetViewingStore.showAssetViewer(false);
handlePromiseError(navigate({ targetRoute: 'current', assetId: null }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,29 @@
<AssetViewer
asset={$viewingAsset}
showNavigation={assets.length > 1}
onNext={() => {
onNext={async () => {

Check failure on line 155 in web/src/lib/components/utilities-page/duplicates/duplicates-compare-control.svelte

View workflow job for this annotation

GitHub Actions / Test & Lint Web

Async arrow function has no 'await' expression
const index = getAssetIndex($viewingAsset.id) + 1;
if (index >= assets.length) {
return false;
}
setAsset(assets[index % assets.length]);
return true;
}}
onPrevious={() => {
const index = getAssetIndex($viewingAsset.id) - 1 + assets.length;
setAsset(assets[index % assets.length]);
onPrevious={async () => {

Check failure on line 163 in web/src/lib/components/utilities-page/duplicates/duplicates-compare-control.svelte

View workflow job for this annotation

GitHub Actions / Test & Lint Web

Async arrow function has no 'await' expression
const index = getAssetIndex($viewingAsset.id) - 1;
if (index < 0) {
return false;
}
setAsset(assets[index]);
return true;
}}
onRandom={async () => {

Check failure on line 171 in web/src/lib/components/utilities-page/duplicates/duplicates-compare-control.svelte

View workflow job for this annotation

GitHub Actions / Test & Lint Web

Async arrow function has no 'await' expression
if (assets.length <= 0) {
return false;
}
const index = Math.floor(Math.random() * assets.length);
setAsset(assets[index]);
return true;
}}
onClose={() => {
assetViewingStore.showAssetViewer(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,28 @@
if (viewingAssetCursor < viewingAssets.length - 1) {
await setAssetId(viewingAssets[++viewingAssetCursor]);
await navigate({ targetRoute: 'current', assetId: $viewingAsset.id });
return true;
}
return false
}
async function navigatePrevious() {
if (viewingAssetCursor > 0) {
await setAssetId(viewingAssets[--viewingAssetCursor]);
await navigate({ targetRoute: 'current', assetId: $viewingAsset.id });
return true;
}
return false;
}
async function navigateRandom() {
if (viewingAssets.length <= 0) {
return false;
}
const index = Math.floor(Math.random() * viewingAssets.length);
await setAssetId(viewingAssets[index]);
await navigate({ targetRoute: 'current', assetId: $viewingAsset.id });
return true;
}
</script>

Expand All @@ -132,6 +146,7 @@
showNavigation={viewingAssets.length > 1}
onNext={navigateNext}
onPrevious={navigatePrevious}
onRandom={navigateRandom}
onClose={() => {
assetViewingStore.showAssetViewer(false);
handlePromiseError(navigate({ targetRoute: 'current', assetId: null }));
Expand Down

0 comments on commit 67508be

Please sign in to comment.