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

214 wiersze
6.2 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,
2021-12-01 15:28:52 +00:00
Button,
} 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-12-01 15:28:52 +00:00
import { DRAWER_TYPES } from "../../src/core/providers/OverlayProvider/constants";
2021-12-15 15:56:46 +00:00
import Page404 from "../../src/components/FourOFour";
2021-12-15 16:00:29 +00:00
import { BsGear } from "react-icons/bs";
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
2021-11-15 22:50:08 +00:00
const [timeRange, setTimeRange] = useState(timeMap[MINUTE_KEY]);
const router = useRouter();
2021-12-01 15:28:52 +00:00
const overlay = useContext(OverlayContext);
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-24 13:41:41 +00:00
const updateCallback = ({ name }) => {
2021-12-08 18:45:53 +00:00
updateDashboard.mutate({
2021-12-15 15:56:46 +00:00
id: dashboardCache.data.id,
2021-12-08 18:45:53 +00:00
dashboard: {
2021-12-15 15:56:46 +00:00
dashboard_id: dashboardCache.data.id,
2021-12-08 18:45:53 +00:00
name: name,
subscription_cache:
2021-12-15 15:56:46 +00:00
dashboardCache.data.resource_data.subscription_setting,
2021-12-08 18:45:53 +00:00
},
});
2021-11-24 13:41:41 +00:00
};
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
2021-12-15 15:56:46 +00:00
if (
dashboardCache.isLoadingError &&
dashboardCache?.error?.response?.status === 404
) {
return <Page404 />;
}
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-12-15 16:00:29 +00:00
<Stack
direction={["column", "row", null]}
w="100%"
placeItems="center"
pt={2}
>
2021-11-24 13:41:41 +00:00
<Editable
as={Heading}
colorScheme="blue"
placeholder="enter note here"
2021-12-15 15:56:46 +00:00
defaultValue={dashboardCache.data.resource_data.name}
2021-11-24 13:41:41 +00:00
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
/>
2021-12-15 16:00:29 +00:00
<IconButton
2021-12-01 15:28:52 +00:00
onClick={() =>
2021-12-02 19:39:59 +00:00
overlay.toggleDrawer({
type: DRAWER_TYPES.NEW_DASHBOARD_ITEM,
2021-12-15 15:56:46 +00:00
props: dashboardCache.data.resource_data,
2021-12-02 19:39:59 +00:00
})
2021-12-01 15:28:52 +00:00
}
2021-12-15 16:00:29 +00:00
size="md"
2021-12-01 15:28:52 +00:00
colorScheme="blue"
2021-12-15 16:00:29 +00:00
variant="outline"
icon={<BsGear />}
/>
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">
2021-12-08 18:45:53 +00:00
<>
{Object.keys(dashboardLinksCache.data.data).map((key) => {
const s3PresignedURLs = dashboardLinksCache.data.data[key];
const name = subscriptionsCache.data.subscriptions.find(
(subscription) => subscription.id === key
2021-12-16 17:54:46 +00:00
)?.label;
2021-12-08 18:45:53 +00:00
return (
<Flex
key={v4()}
flexBasis={plotMinW}
flexGrow={1}
minW={plotMinW}
minH="320px"
direction="column"
boxShadow="md"
m={["1px", 2]}
>
<Text
w="100%"
py={2}
bgColor="gray.50"
fontWeight="600"
textAlign="center"
>
2021-12-16 17:54:46 +00:00
{name ?? ""}
2021-12-08 18:45:53 +00:00
</Text>
<SubscriptionReport
timeRange={timeRange}
url={s3PresignedURLs[timeRange]}
id={dashboardId}
refetchLinks={dashboardLinksCache.refetch}
/>
</Flex>
);
})}
2021-12-15 15:56:46 +00:00
{dashboardCache.data.resource_data.subscription_settings[0] ===
2021-12-08 18:45:53 +00:00
undefined && (
<Flex pt="220px" w="100%" placeContent="center">
<Button
size="lg"
colorScheme="orange"
onClick={() =>
overlay.toggleDrawer({
type: DRAWER_TYPES.NEW_DASHBOARD_ITEM,
2021-12-15 15:56:46 +00:00
props: dashboardCache.data.resource_data,
2021-12-08 18:45:53 +00:00
})
}
2021-11-11 20:27:41 +00:00
>
2021-12-08 18:45:53 +00:00
Populate dashboard
</Button>
2021-11-11 20:27:41 +00:00
</Flex>
2021-12-08 18:45:53 +00:00
)}
</>
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;