link-stack/packages/leafcutter-ui/components/Home.tsx

109 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

2023-06-26 10:07:12 +00:00
"use client";
import { useEffect, FC } from "react";
2023-06-28 09:09:45 +00:00
import { useRouter, usePathname } from "next/navigation";
2023-02-13 13:46:56 +00:00
import Link from "next/link";
import { Grid, Button } from "@mui/material";
import { useTranslate } from "react-polyglot";
import { useCookies } from "react-cookie";
2023-08-25 07:11:33 +00:00
import { Welcome } from "./Welcome";
import { WelcomeDialog } from "./WelcomeDialog";
import { VisualizationCard } from "./VisualizationCard";
2024-03-20 17:51:21 +01:00
import { useLeafcutterContext } from "./LeafcutterProvider";
2024-05-09 07:42:44 +02:00
import { getBasePath } from "../lib/utils";
2023-02-13 13:46:56 +00:00
2023-06-26 10:07:12 +00:00
type HomeProps = {
2023-05-24 20:27:57 +00:00
visualizations: any;
2024-03-20 17:51:21 +01:00
showWelcome?: boolean;
2023-05-24 20:27:57 +00:00
};
2024-05-09 07:42:44 +02:00
export const Home: FC<HomeProps> = ({
visualizations = [],
showWelcome = true,
}) => {
2023-02-13 13:46:56 +00:00
const router = useRouter();
2023-07-23 11:21:39 +02:00
const pathname = usePathname() ?? "";
2023-02-13 13:46:56 +00:00
const cookieName = "homeIntroComplete";
const [cookies, setCookie] = useCookies([cookieName]);
const t = useTranslate();
const {
colors: { white, leafcutterElectricBlue },
typography: { h4 },
2024-03-20 17:51:21 +01:00
} = useLeafcutterContext();
2023-02-13 13:46:56 +00:00
const homeIntroComplete = parseInt(cookies[cookieName], 10) || 0;
useEffect(() => {
if (homeIntroComplete === 0) {
setCookie(cookieName, `${1}`, { path: "/" });
2023-06-28 09:09:45 +00:00
router.push(`${pathname}?tooltip=welcome`);
2023-02-13 13:46:56 +00:00
}
}, [homeIntroComplete, router, setCookie]);
return (
2023-06-26 10:07:12 +00:00
<>
2024-03-20 17:51:21 +01:00
{showWelcome && <Welcome />}
2023-02-13 13:46:56 +00:00
<Grid
container
spacing={3}
sx={{ pt: "22px", pb: "22px" }}
direction="row-reverse"
>
2024-05-09 07:42:44 +02:00
<Link href={`${getBasePath()}/create`} passHref>
2023-02-13 13:46:56 +00:00
<Button
sx={{
fontSize: 14,
borderRadius: 500,
color: leafcutterElectricBlue,
border: `2px solid ${leafcutterElectricBlue}`,
fontWeight: "bold",
textTransform: "uppercase",
pl: 6,
pr: 5,
":hover": {
backgroundColor: leafcutterElectricBlue,
color: white,
opacity: 0.8,
},
}}
>
{t("createVisualization")}
</Button>
</Link>
</Grid>
<Grid
container
direction="row"
wrap="wrap"
spacing={3}
justifyContent="space-between"
>
{visualizations.length === 0 ? (
<Grid
container
sx={{ height: 300, width: "100%", pt: 10 }}
justifyContent="center"
>
<Grid item sx={{ ...h4, width: 450, textAlign: "center" }}>
2024-05-09 07:42:44 +02:00
{"You dont have any saved visualizations. Go to "}
<Link href={`${getBasePath()}/create`}>Search and Create</Link>
{" or "}
<Link href={`${getBasePath()}/trends`}>Trends</Link>
{" to get started."}
2023-02-13 13:46:56 +00:00
</Grid>
</Grid>
) : null}
2023-05-24 20:27:57 +00:00
{visualizations.map((visualization: any, index: number) => (
2023-02-13 13:46:56 +00:00
<VisualizationCard
id={visualization.id}
key={index}
title={visualization.title}
description={visualization.description}
url={visualization.url}
/>
))}
</Grid>
<WelcomeDialog />
2023-06-26 10:07:12 +00:00
</>
2023-02-13 13:46:56 +00:00
);
};