slate/scenes/SceneActivity/index.js

162 lines
4.8 KiB
JavaScript
Raw Normal View History

2021-05-27 11:20:34 +03:00
import * as React from "react";
import * as Constants from "~/common/constants";
import * as Styles from "~/common/styles";
import { css } from "@emotion/react";
// import { SecondaryTabGroup } from "~/components/core/TabGroup";
2021-05-27 11:20:34 +03:00
import { LoaderSpinner } from "~/components/system/components/Loaders";
import { useIntersection } from "common/hooks";
import { useActivity } from "~/scenes/SceneActivity/hooks";
2021-07-28 16:56:05 +03:00
import { GlobalCarousel } from "~/components/system/components/GlobalCarousel";
2021-05-27 11:20:34 +03:00
import ScenePage from "~/components/core/ScenePage";
import WebsitePrototypeWrapper from "~/components/core/WebsitePrototypeWrapper";
import ActivityGroup from "~/components/core/ActivityGroup";
const STYLES_GROUPS_CONTAINER = css`
margin-top: 32px;
& > * + * {
margin-top: 32px;
}
`;
const STYLES_LOADING_CONTAINER = css`
height: 48px;
margin-top: 32px;
${Styles.CONTAINER_CENTERED}
`;
const STYLES_LOADER = css`
display: flex;
align-items: center;
justify-content: center;
height: calc(100vh - 400px);
width: 100%;
`;
2021-07-28 16:56:05 +03:00
export default function SceneActivity({ page, viewer, external, onAction, ...props }) {
2021-05-27 11:20:34 +03:00
const { feed, tab, isLoading, updateFeed } = useActivity({
page,
viewer,
onAction,
});
const divRef = React.useRef();
const nbrOfCardsInRow = useNbrOfCardsPerRow(divRef);
2021-07-28 16:56:05 +03:00
const [globalCarouselState, setGlobalCarouselState] = React.useState({
2021-09-18 04:35:21 +03:00
currentCarousel: -1,
2021-07-28 16:56:05 +03:00
currentObjects: [],
});
const handleFileClick = (fileIdx, groupFiles) =>
2021-09-18 04:35:21 +03:00
setGlobalCarouselState({ currentCarousel: fileIdx, currentObjects: groupFiles });
2021-07-28 16:56:05 +03:00
2021-05-27 11:20:34 +03:00
useIntersection({
ref: divRef,
onIntersect: () => {
if (feed?.length === 0 || isLoading[tab]) return;
updateFeed();
},
});
return (
<WebsitePrototypeWrapper
title={`${page.pageTitle} • Slate`}
url={`${Constants.hostname}${page.pathname}`}
>
<ScenePage>
{/* {viewer && (
2021-05-27 11:20:34 +03:00
<SecondaryTabGroup
tabs={[
{ title: "My network", value: { tab: "activity" } },
{ title: "Explore", value: { tab: "explore" } },
]}
value={tab}
onAction={onAction}
style={{ marginTop: 0 }}
/>
)} */}
2021-05-27 11:20:34 +03:00
<div css={STYLES_GROUPS_CONTAINER}>
{feed?.map((group) => (
<ActivityGroup
nbrOfCardsPerRow={nbrOfCardsInRow}
2021-05-27 11:20:34 +03:00
key={group.id}
viewer={viewer}
external={external}
onAction={onAction}
group={group}
2021-07-28 16:56:05 +03:00
onFileClick={handleFileClick}
2021-05-27 11:20:34 +03:00
/>
))}
</div>
<div ref={divRef} css={feed?.length ? STYLES_LOADING_CONTAINER : STYLES_LOADER}>
{isLoading[tab] && <LoaderSpinner style={{ height: 32, width: 32 }} />}
</div>
</ScenePage>
2021-07-28 16:56:05 +03:00
<GlobalCarousel
viewer={viewer}
objects={globalCarouselState.currentObjects}
2021-09-18 04:35:21 +03:00
index={globalCarouselState.currentCarousel}
2021-07-28 16:56:05 +03:00
isMobile={props.isMobile}
2021-09-18 04:35:21 +03:00
onChange={(idx) => setGlobalCarouselState((prev) => ({ ...prev, currentCarousel: idx }))}
2021-07-28 16:56:05 +03:00
isOwner={false}
onAction={() => {}}
/>
2021-05-27 11:20:34 +03:00
</WebsitePrototypeWrapper>
);
}
let NbrOfCardsInRow = {};
function useNbrOfCardsPerRow(ref) {
const calculateNbrOfCards = (card) => {
const isMobile = window.matchMedia(`(max-width: ${Constants.sizes.mobile}px)`).matches;
const profileInfoWidth = isMobile ? 0 : Constants.grids.activity.profileInfo.width;
const containerWidth = ref.current.offsetWidth - profileInfoWidth;
const nbrOfCardsWithoutGap = Math.floor(containerWidth / card.width);
const gapsWidth = (nbrOfCardsWithoutGap - 1) * card.gap;
return Math.floor((containerWidth - gapsWidth) / card.width) || 1;
};
React.useEffect(() => {
if (JSON.stringify(NbrOfCardsInRow) !== "{}") return;
const isMobile = window.matchMedia(`(max-width: ${Constants.sizes.mobile}px)`).matches;
const responsiveKey = isMobile ? "mobile" : "desktop";
2021-09-18 04:35:21 +03:00
const { width: objectPreviewWidth, rowGap: objectPreviewGridRowGap } = Constants.grids.object[
responsiveKey
];
NbrOfCardsInRow.object = calculateNbrOfCards({
width: objectPreviewWidth,
gap: objectPreviewGridRowGap,
});
2021-09-18 04:35:21 +03:00
const {
width: collectionPreviewWidth,
rowGap: collectionPreviewGridRowGap,
} = Constants.grids.collection[responsiveKey];
NbrOfCardsInRow.collection = calculateNbrOfCards({
width: collectionPreviewWidth,
gap: collectionPreviewGridRowGap,
});
2021-09-18 04:35:21 +03:00
const {
width: profilePreviewWidth,
rowGap: profilePreviewGridRowGap,
} = Constants.grids.profile[responsiveKey];
NbrOfCardsInRow.profile = calculateNbrOfCards({
width: profilePreviewWidth,
gap: profilePreviewGridRowGap,
});
}, []);
return NbrOfCardsInRow;
}