import React, { FC, useState, useCallback, useEffect, useRef, useMemo } from 'react'; import { IFlowState } from '~/redux/flow/reducer'; import classNames from 'classnames'; import * as styles from './styles.scss'; import { getURL } from '~/utils/dom'; import { withRouter, RouteComponentProps } from 'react-router'; import { URLS, PRESETS } from '~/constants/urls'; import { Icon } from '~/components/input/Icon'; import { INode } from "~/redux/types"; type IProps = RouteComponentProps & { heroes: IFlowState['heroes']; }; const FlowHeroUnconnected: FC = ({ heroes, history }) => { const preset = useMemo(() => (window.innerWidth <= 768 ? PRESETS.cover : PRESETS.small_hero), []); const [limit, setLimit] = useState(6); const [current, setCurrent] = useState(0); const [loaded, setLoaded] = useState[]>([]); const onLoad = useCallback((i: number) => { setLoaded([...loaded, heroes[i]]) }, [heroes, loaded, setLoaded]) const items = Math.min(heroes.length, limit) const title = useMemo(() => { return loaded[current]?.title || ''; }, [loaded, current, heroes]); const onNext = useCallback(() => setCurrent(current < items - 1 ? current + 1 : 0), [current, items]) const onPrev = useCallback(() => setCurrent(current > 0 ? current - 1 : items - 1), [current, items]) return (
{ heroes.slice(0, items).map((hero, i) => ( onLoad(i)} /> )) }
{loaded.length > 0 && (
{title}
)} {loaded.length > 0 && loaded.slice(0, limit).map((hero, index) => (
{hero.thumbnail}
))}
); }; const FlowHero = withRouter(FlowHeroUnconnected); export { FlowHero };