moonstream/frontend/pages/dashboard/[dashboardId].js

259 wiersze
7.6 KiB
JavaScript
Czysty Zwykły widok Historia

import React, { useContext, useEffect, useState } from "react";
2021-11-11 11:51:14 +00:00
import { getLayout } from "../../src/layouts/AppLayout";
import {
Spinner,
Flex,
Heading,
Stack,
Text,
Spacer,
IconButton,
2021-11-24 13:41:41 +00:00
Editable,
EditableInput,
EditablePreview,
} from "@chakra-ui/react";
2021-11-11 11:51:14 +00:00
import Scrollable from "../../src/components/Scrollable";
import RangeSelector from "../../src/components/RangeSelector";
import useDashboard from "../../src/core/hooks/useDashboard";
2021-11-11 20:27:41 +00:00
import { useRouter, useSubscriptions } from "../../src/core/hooks";
import { BiTrash } from "react-icons/bi";
import OverlayContext from "../../src/core/providers/OverlayProvider/context";
2021-11-11 20:27:41 +00:00
import SubscriptionReport from "../../src/components/SubscriptionReport";
import { v4 } from "uuid";
2021-11-11 11:36:38 +00:00
const HOUR_KEY = "Hourly";
const DAY_KEY = "Daily";
2021-11-15 22:50:08 +00:00
const MINUTE_KEY = "Minutes";
2021-11-11 11:36:38 +00:00
let timeMap = {};
2021-11-15 22:50:08 +00:00
timeMap[DAY_KEY] = "month";
timeMap[HOUR_KEY] = "week";
timeMap[MINUTE_KEY] = "day";
2021-11-11 11:36:38 +00:00
const Analytics = () => {
const { toggleAlert } = useContext(OverlayContext);
2021-11-11 11:36:38 +00:00
// const [nodesReady, setNodeReady] = useState({
// ntx: false,
// values: false,
// mints: false,
// NFTOwners: false,
// minters: false,
// });
2021-11-11 11:36:38 +00:00
// const nTxRef_ = useRef();
// const valueRef_ = useRef();
// const mintsRef_ = useRef();
// const uniqueNFTOwnersRef_ = useRef();
// const mintersRef_ = useRef();
2021-11-11 11:36:38 +00:00
2021-11-11 11:51:14 +00:00
// const nTxRef = useCallback(
// (node) => {
// if (node !== null && !nodesReady.ntx) {
// setNodeReady({ ...nodesReady, ntx: true });
// nTxRef_.current = node;
// }
// },
// [nodesReady]
// );
// const valueRef = useCallback(
// (node) => {
// if (node !== null && !nodesReady.values) {
// setNodeReady({ ...nodesReady, values: true });
// valueRef_.current = node;
// }
// },
// [nodesReady]
// );
// const mintsRef = useCallback(
// (node) => {
// if (node !== null && !nodesReady.mints) {
// setNodeReady({ ...nodesReady, mints: true });
// mintsRef_.current = node;
// }
// },
// [nodesReady]
// );
2021-11-11 11:36:38 +00:00
2021-11-11 11:51:14 +00:00
// const uniqueNFTOwnersRef = useCallback(
// (node) => {
// if (node !== null && !nodesReady.NFTOwners) {
// setNodeReady({ ...nodesReady, NFTOwners: true });
// uniqueNFTOwnersRef_.current = node;
// }
// },
// [nodesReady]
// );
2021-11-11 11:36:38 +00:00
2021-11-11 11:51:14 +00:00
// const mintersRef = useCallback(
// (node) => {
// if (node !== null && !nodesReady.minters) {
// setNodeReady({ ...nodesReady, minters: true });
// mintersRef_.current = node;
// }
// },
// [nodesReady]
// );
2021-11-11 11:36:38 +00:00
2021-11-15 22:50:08 +00:00
const [timeRange, setTimeRange] = useState(timeMap[MINUTE_KEY]);
const router = useRouter();
const { dashboardId } = router.params;
2021-11-24 13:41:41 +00:00
const {
dashboardCache,
dashboardLinksCache,
deleteDashboard,
updateDashboard,
} = useDashboard(dashboardId);
2021-11-11 20:27:41 +00:00
const { subscriptionsCache } = useSubscriptions();
2021-11-11 11:36:38 +00:00
2021-11-11 20:36:48 +00:00
useEffect(() => {
if (typeof window !== "undefined") {
if (dashboardCache?.data?.data?.resource_data?.name) {
document.title = dashboardCache?.data?.data?.resource_data?.name;
} else {
document.title = `Dashboard`;
}
}
}, [dashboardCache?.data?.data?.resource_data?.name]);
2021-11-11 11:51:14 +00:00
// useLayoutEffect(() => {
// const items = [
// nTxRef_,
// valueRef_,
// mintsRef_,
// uniqueNFTOwnersRef_,
// mintersRef_,
// ];
// console.log("useeffect fired");
// if (items.some((item) => !!item.current)) {
// console.log("brder fun");
// var firstItemInCurrentRow = items[0];
// items.forEach((item) => {
// if (item.current) {
// if (item !== firstItemInCurrentRow) {
// // Check if the current item is at the same
// // height as the first item in the current row.
// if (
// item.current.offsetTop === firstItemInCurrentRow.current.offsetTop
// ) {
// item.current.style.borderLeft =
// "3px dashed var(--chakra-colors-gray-600)";
// } else {
// // This item was lower, it must be
// // the first in a new row.
// firstItemInCurrentRow = item;
// item.current.style.borderLeft = "0px dashed black";
// }
// }
// } else {
// firstItemInCurrentRow = item;
// }
// });
// }
// }, [nodesReady, windowSize]);
2021-11-11 11:36:38 +00:00
2021-11-24 13:41:41 +00:00
const updateCallback = ({ name }) => {
const dashboard = { ...dashboardCache.data.data.resource_data };
dashboard.name = name;
updateDashboard.mutate({ id: dashboardCache.data.data.id, dashboard });
};
2021-11-11 20:27:41 +00:00
if (
dashboardCache.isLoading ||
dashboardLinksCache.isLoading ||
subscriptionsCache.isLoading
)
return <Spinner />;
2021-11-11 11:36:38 +00:00
const plotMinW = "250px";
2021-11-10 16:11:42 +00:00
return (
2021-11-11 11:36:38 +00:00
<Scrollable>
<Flex
h="100%"
w="100%"
m={0}
px={["10px", "20px", "7%", null]}
2021-11-11 11:36:38 +00:00
direction="column"
alignItems="center"
minH="100vh"
>
2021-11-17 16:44:25 +00:00
<Stack direction={["column", "row", null]} w="100%" placeItems="center">
2021-11-24 13:41:41 +00:00
<Editable
as={Heading}
colorScheme="blue"
placeholder="enter note here"
defaultValue={dashboardCache.data.data.resource_data.name}
onSubmit={(nextValue) =>
updateCallback({
name: nextValue,
})
}
>
<EditablePreview maxW="40rem" _placeholder={{ color: "black" }} />
<EditableInput maxW="40rem" />
</Editable>
<Heading as="h1" py={2} fontSize={["md", "xl"]}></Heading>
<IconButton
icon={<BiTrash />}
variant="ghost"
colorScheme="red"
size="sm"
onClick={() => toggleAlert(() => deleteDashboard.mutate())}
/>
2021-11-11 11:36:38 +00:00
<Spacer />
<RangeSelector
2021-11-15 22:50:08 +00:00
initialRange={MINUTE_KEY}
2021-11-11 11:36:38 +00:00
ranges={Object.keys(timeMap)}
size={["sm", "md", null]}
2021-11-15 22:50:08 +00:00
onChange={(e) => {
setTimeRange(timeMap[e]);
}}
2021-11-11 11:36:38 +00:00
/>
</Stack>
2021-11-11 20:27:41 +00:00
<Flex w="100%" direction="row" flexWrap="wrap-reverse" id="container">
{Object.keys(dashboardLinksCache.data.data).map((key) => {
2021-11-12 10:21:53 +00:00
const s3PresignedURLs = dashboardLinksCache.data.data[key];
2021-11-11 20:27:41 +00:00
const name = subscriptionsCache.data.subscriptions.find(
(subscription) => subscription.id === key
).label;
return (
<Flex
key={v4()}
flexBasis={plotMinW}
flexGrow={1}
minW={plotMinW}
minH="320px"
direction="column"
boxShadow="md"
m={["1px", 2]}
2021-11-11 20:27:41 +00:00
>
<Text
w="100%"
py={2}
bgColor="gray.50"
fontWeight="600"
textAlign="center"
>
{name}
</Text>
<SubscriptionReport
2021-11-15 22:50:08 +00:00
timeRange={timeRange}
url={s3PresignedURLs[timeRange]}
2021-11-11 20:27:41 +00:00
id={v4()}
type={v4()}
2021-11-16 00:59:44 +00:00
refetchLinks={dashboardLinksCache.refetch}
2021-11-11 20:27:41 +00:00
/>
</Flex>
);
})}
2021-11-11 11:36:38 +00:00
</Flex>
</Flex>
</Scrollable>
2021-11-10 16:11:42 +00:00
);
};
2021-11-11 11:36:38 +00:00
Analytics.getLayout = getLayout;
export default Analytics;