import { Box, Page, PageProps } from '@wix/design-system';
import React, { ReactNode, useEffect, useState } from 'react';
import { useCollectionPageChild } from '../../hooks/useCollectionPageChild';
import { CollectionPageHeader } from '../CollectionPageHeaderNew';
import { PageWrapperBase } from '../PageWrapper';
import { CollectionPageContent } from '../CollectionPageContent';
import { observer } from 'mobx-react-lite';
import { CollectionPageContextProvider } from '../../providers';
import { usePageHeader } from '../../hooks/usePageHeaderNew';
import { classes, st } from './CollectionPage.st.css.js';
import {
useCollectionPageState,
useWixPatternsContainer,
} from '@wix/bex-core/react';
import { useCollectionPageLoadReporting } from './useCollectionPageLoadReporting';
import { useIsMobile } from '../../hooks/useIsMobile';
import { CollectionPageFooter } from '../CollectionPageFooter';
export interface CollectionPageProps extends PageProps {
/**
* Accepts [CollectionPage.Header](./?path=/story/base-components-pages-collection-page--collectionpage-header) and [CollectionPage.Content](./?path=/story/base-components-pages-collection-page--collectionpage-content) elements
* @external
*/
children?: ReactNode;
}
function _CollectionPage({
children,
scrollableContentRef,
...rest
}: CollectionPageProps) {
const wrappedChildren = useCollectionPageChild(children);
const childrenArr = React.Children.toArray(children);
const {
title: pageTitle,
hideTotal: hideHeaderTotal,
header,
} = usePageHeader(childrenArr);
const container = useWixPatternsContainer();
const applyMinWidthExperiment = container.internalExperiments?.enabled(
'specs.cairo.CollectionPageMinWidth',
);
const minWidth = applyMinWidthExperiment ? 394 : undefined;
const [reportBI] = useState(() =>
container.createBILogger({
componentType: 'Collection page',
pageType: 'Collection',
route: window.location.pathname,
}),
);
const collectionPageState = useCollectionPageState({
reportBI,
pageTitle,
hasHeader: header != null,
hideHeaderTotal,
});
useEffect(() => collectionPageState.init(), []);
useCollectionPageLoadReporting({
startTime: collectionPageState.startTime,
withinRouter: !!collectionPageState.withinRouter,
});
const isMobile = useIsMobile();
let child;
if (container.isPanelLayout) {
child = (
{wrappedChildren}
);
} else {
child = (
{
collectionPageState.pageState.scrollableContentRef.current = el;
scrollableContentRef?.(el);
}}
{...rest}
minWidth={isMobile ? 0 : minWidth}
>
{wrappedChildren}
);
}
return (
{child}
);
}
_CollectionPage.Header = CollectionPageHeader;
_CollectionPage.Content = CollectionPageContent;
_CollectionPage.Footer = CollectionPageFooter;
_CollectionPage.displayName = 'CollectionPage';
export const CollectionPage = observer(_CollectionPage);