diff --git a/src/common/useNavigateWithOrigin.ts b/src/common/useNavigateWithOrigin.ts index 08eceb174..7039adf21 100644 --- a/src/common/useNavigateWithOrigin.ts +++ b/src/common/useNavigateWithOrigin.ts @@ -1,6 +1,9 @@ import { useLocation, useNavigate, To, Location } from 'react-router-dom'; +import toPath from './toPath'; -const ORIGIN_KEY = 'originPath'; +const getLocationPath = (location: Location): string => location.pathname + (location.search || ''); +const getOriginPath = (origin: Location | string): string => typeof origin === 'string' ? origin : getLocationPath(origin); +const normalizeTarget = (target: To): To => typeof target === 'string' ? toPath(target) : target; export function useNavigateWithOrigin() { const navigate = useNavigate(); @@ -8,32 +11,20 @@ export function useNavigateWithOrigin() { function navigateWithOrigin(target: To) { const origin: Location = location.state?.from || location; - - // Save origin in sessionStorage - sessionStorage.setItem(ORIGIN_KEY, origin.pathname + origin.search); - - // Navigate and propagate origin - navigate(target, { + navigate(normalizeTarget(target), { state: { from: origin }, }); } - function setOriginPath(path?: string) { - const finalPath = path ?? location.pathname + location.search; - sessionStorage.setItem(ORIGIN_KEY, finalPath); - } - function getStoredOrigin(fallback?: string): string | undefined { if (location.state?.from) { - const from = location.state.from as Location; - return from.pathname + (from.search || ''); + return getOriginPath(location.state.from as Location | string); } - return sessionStorage.getItem(ORIGIN_KEY) || fallback; + return fallback; } return { navigateWithOrigin, getStoredOrigin, - setOriginPath, }; } diff --git a/src/components/MetaItem/MetaItem.js b/src/components/MetaItem/MetaItem.js index bd61ef964..6567da8d2 100644 --- a/src/components/MetaItem/MetaItem.js +++ b/src/components/MetaItem/MetaItem.js @@ -16,7 +16,7 @@ const styles = require('./styles'); const MetaItem = React.memo(({ className, type, name, poster, posterShape, posterChangeCursor, progress, newVideos, options, deepLinks, dataset, optionOnSelect, onDismissClick, onPlayClick, watched, ...props }) => { const { t } = useTranslation(); - const { setOriginPath } = useNavigateWithOrigin(); + const { navigateWithOrigin } = useNavigateWithOrigin(); const [menuOpen, onMenuOpen, onMenuClose] = useBinaryState(false); const href = React.useMemo(() => { return deepLinks ? @@ -34,13 +34,15 @@ const MetaItem = React.memo(({ className, type, name, poster, posterShape, poste null; }, [deepLinks]); const metaItemOnClick = React.useCallback((event) => { - setOriginPath(); if (event.nativeEvent.selectPrevented) { event.preventDefault(); + } else if (typeof href === 'string') { + event.preventDefault(); + navigateWithOrigin(href); } else if (typeof props.onClick === 'function') { props.onClick(event); } - }, [props.onClick]); + }, [href, navigateWithOrigin, props.onClick]); const menuOnClick = React.useCallback((event) => { event.nativeEvent.selectPrevented = true; }, []); diff --git a/src/routes/Calendar/List/Item/Item.tsx b/src/routes/Calendar/List/Item/Item.tsx index 9525a4bc8..9641a438c 100644 --- a/src/routes/Calendar/List/Item/Item.tsx +++ b/src/routes/Calendar/List/Item/Item.tsx @@ -19,7 +19,7 @@ type Props = { const Item = ({ selected, monthInfo, date, items, profile, onClick }: Props) => { const ref = useRef(null); - const { setOriginPath } = useNavigateWithOrigin(); + const { navigateWithOrigin } = useNavigateWithOrigin(); const { toDayMonth } = useCalendarDate(profile); const [active, today] = useMemo(() => [ @@ -28,10 +28,15 @@ const Item = ({ selected, monthInfo, date, items, profile, onClick }: Props) => ], [selected, monthInfo, date]); const onItemClick = () => { - setOriginPath(); onClick && onClick(date); }; + const onVideoClick = (event: React.MouseEvent, target: string) => { + event.preventDefault(); + event.stopPropagation(); + navigateWithOrigin(target); + }; + useEffect(() => { active && ref.current?.scrollIntoView({ block: 'start', @@ -52,7 +57,7 @@ const Item = ({ selected, monthInfo, date, items, profile, onClick }: Props) =>
{ items.map(({ id, name, season, episode, deepLinks }) => ( -