Skip to content

Commit

Permalink
fix(web): ensure current asset index stays within bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
michelheusschen committed Nov 8, 2024
1 parent c8b4680 commit ddf027d
Showing 1 changed file with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,43 @@
const handleNext = async () => {
try {
const asset = onNext ? await onNext() : assets[++currentViewAssetIndex];
if (asset) {
setAsset(asset);
await navigate({ targetRoute: 'current', assetId: $viewingAsset.id });
let asset: AssetResponseDto | undefined;
if (onNext) {
asset = await onNext();
} else {
currentViewAssetIndex = Math.min(currentViewAssetIndex + 1, assets.length - 1);
asset = assets[currentViewAssetIndex];
}
await navigateToAsset(asset);
} catch (error) {
handleError(error, $t('errors.cannot_navigate_next_asset'));
}
};
const handlePrevious = async () => {
try {
const asset = onPrevious ? await onPrevious() : assets[--currentViewAssetIndex];
if (asset) {
setAsset(asset);
await navigate({ targetRoute: 'current', assetId: $viewingAsset.id });
let asset: AssetResponseDto | undefined;
if (onPrevious) {
asset = await onPrevious();
} else {
currentViewAssetIndex = Math.max(currentViewAssetIndex - 1, 0);
asset = assets[currentViewAssetIndex];
}
await navigateToAsset(asset);
} catch (error) {
handleError(error, $t('errors.cannot_navigate_previous_asset'));
}
};
const navigateToAsset = async (asset?: AssetResponseDto) => {
if (asset && asset.id !== $viewingAsset.id) {
setAsset(asset);
await navigate({ targetRoute: 'current', assetId: $viewingAsset.id });
}
};
const handleAction = async (action: Action) => {
switch (action.type) {
case AssetAction.ARCHIVE:
Expand Down

0 comments on commit ddf027d

Please sign in to comment.