import { useCallback, useEffect, useMemo, useState } from 'react'; import Content from './Content'; import { Content as IContent, StackedNavigationContext, useStackedNavigation, } from './context'; import tw from '../../tw'; interface ContentContainerProps { history: StackedNavigationContext['history']; marginTop: number; marginBottom: number; } interface Page { content: IContent; removed: boolean; } export const ContentContainerRenderer = ({ history, marginTop, }: ContentContainerProps) => { const [pages, setPages] = useState([]); useEffect(() => { setPages((pages) => { // Update content case if ( history.length && pages.length && pages[pages.length - 1].content.title === history[history.length - 1].title ) { return pages.map((page, index) => index === pages.length - 1 ? { content: history[history.length - 1], removed: false, } : page ); } // A new page appears if (history.length > pages.length) { return [ ...pages, { content: [...history].pop()!, removed: false, }, ]; } // return pages.map((page, k) => k === pages.length - 1 ? { ...page, removed: true, } : page ); }); }, [history]); const onUnmount = useCallback(() => { const newPages = [...pages]; newPages.pop(); setPages(newPages); }, [pages]); return (
{pages.map(({ content, removed }, index) => ( ))}
); }; export const ContentContainer = () => { const { history, headBox } = useStackedNavigation(); const marginTop = headBox ? +headBox.height.toFixed() : 0; return useMemo( () => ( ), [history, marginTop] ); }; export default ContentContainer;