Skip to content

Commit

Permalink
RouteList: Switch photos automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
jvaclavik committed Sep 17, 2024
1 parent 212d72c commit da68787
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
27 changes: 26 additions & 1 deletion src/components/FeaturePanel/Climbing/ClimbingView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { getCommonsImageUrl } from '../../../services/images/getCommonsImageUrl'
import { useUserSettingsContext } from '../../utils/UserSettingsContext';
import { CLIMBING_ROUTE_ROW_HEIGHT, SPLIT_PANE_DEFAULT_HEIGHT } from './config';
import { ClimbingViewContent } from './ClimbingViewContent';
import { getOsmappLink } from '../../../services/helpers';
import { useRouter } from 'next/router';
import { GalleryControls } from './Editor/GalleryControls';

const Container = styled.div`
position: relative;
Expand Down Expand Up @@ -191,6 +194,8 @@ export const ClimbingView = ({ photo }: { photo?: string }) => {
loadedPhotos,
routeListTopOffsets,
setRouteSelectedIndex,
routes,
setPhotoPath,
} = useClimbingContext();
const { feature } = useFeatureContext();

Expand Down Expand Up @@ -295,6 +300,9 @@ export const ClimbingView = ({ photo }: { photo?: string }) => {
ShadowBottom,
} = useScrollShadow([areRoutesLoading]);
const theme = useTheme();
const router = useRouter();
const [isPhotoLoading, setIsPhotoLoading] = useState(false);

const { userSettings } = useUserSettingsContext();

const isResolutionLoaded =
Expand All @@ -305,6 +313,16 @@ export const ClimbingView = ({ photo }: { photo?: string }) => {
Object.keys(resolutions).filter((key) => resolutions[key] === true).length >
0;

const replacePhotoIfNeeded = (photos: string[], selectedIndex: number) => {
if (!photos.includes(photoPath) && selectedIndex > -1 && !isPhotoLoading) {
if (photos.length > 0) {
const featureLink = getOsmappLink(feature);
router.replace(`${featureLink}/climbing/photo/${photos[0]}`);
setIsPhotoLoading(true);
}
}
};

const selectRouteByScroll = (e) => {
const { scrollTop } = e.target;
const scrollTopWithOffset = scrollTop + 20 + CLIMBING_ROUTE_ROW_HEIGHT;
Expand All @@ -315,6 +333,10 @@ export const ClimbingView = ({ photo }: { photo?: string }) => {
offset + CLIMBING_ROUTE_ROW_HEIGHT >= scrollTopWithOffset,
);

const selectedRoute = routes[selectedIndex];
const photos = selectedRoute?.paths ? Object.keys(selectedRoute.paths) : [];

replacePhotoIfNeeded(photos, selectedIndex);
if (selectedIndex !== -1) setRouteSelectedIndex(selectedIndex);
};

Expand Down Expand Up @@ -359,7 +381,7 @@ export const ClimbingView = ({ photo }: { photo?: string }) => {
$imageUrl={backgroundImageUrl}
>
<>
{!isResolutionLoaded && (
{(!isResolutionLoaded || isPhotoLoading) && (
<MiniLoadingContainer>
<CircularProgress color="primary" size={14} thickness={6} />
</MiniLoadingContainer>
Expand All @@ -377,6 +399,8 @@ export const ClimbingView = ({ photo }: { photo?: string }) => {
>
<>
<RoutesEditor
setIsPhotoLoading={setIsPhotoLoading}
isPhotoLoading={isPhotoLoading}
isRoutesLayerVisible={
!isSplitViewDragging && !areRoutesLoading
}
Expand All @@ -385,6 +409,7 @@ export const ClimbingView = ({ photo }: { photo?: string }) => {
/>
</>
</TransformComponent>
{photoZoom.scale < 1.2 && <GalleryControls />}
</TransformWrapper>
{isEditMode && (
<>
Expand Down
21 changes: 12 additions & 9 deletions src/components/FeaturePanel/Climbing/Editor/RoutesEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useClimbingContext } from '../contexts/ClimbingContext';
import { updateElementOnIndex } from '../utils/array';
import { PositionPx } from '../types';
import { getPositionInImageFromMouse } from '../utils/mousePositionUtils';
import { GalleryControls } from './GalleryControls';
import { getCommonsImageUrl } from '../../../../services/images/getCommonsImageUrl';

const EditorContainer = styled.div<{ imageHeight: number }>`
Expand Down Expand Up @@ -43,6 +42,8 @@ export const RoutesEditor = ({
isRoutesLayerVisible = true,
photoResolution,
imageUrl,
isPhotoLoading,
setIsPhotoLoading,
}) => {
const {
imageSize,
Expand Down Expand Up @@ -154,21 +155,23 @@ export const RoutesEditor = ({
});
loadPhotoRelatedData();
preloadOtherPhotos();
setIsPhotoLoading(false);
};

return (
<EditorContainer imageHeight={imageSize.height}>
<ImageContainer>
<ImageElement src={imageUrl} onLoad={onPhotoLoad} ref={photoRef} />
</ImageContainer>
<RoutesLayer
isVisible={isRoutesLayerVisible}
onClick={onCanvasClick}
onEditorMouseMove={onMouseMove}
onEditorTouchMove={onTouchMove}
transformOrigin={transformOrigin}
/>
<GalleryControls />
{!isPhotoLoading && (
<RoutesLayer
isVisible={isRoutesLayerVisible}
onClick={onCanvasClick}
onEditorMouseMove={onMouseMove}
onEditorTouchMove={onTouchMove}
transformOrigin={transformOrigin}
/>
)}
</EditorContainer>
);
};

0 comments on commit da68787

Please sign in to comment.