mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 23:15:36 +00:00
- Changement de structure de fichier - Ajout des favicons des sites - Suppression et mise à jour de dépendances - Ajout React-Icons pour gérer les icons - Amélioration du l'UI
70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
import { useSession } from "next-auth/react";
|
|
import { useState } from "react";
|
|
|
|
import Links from "../components/Links/Links";
|
|
import SideMenu from "../components/SideMenu/SideMenu";
|
|
|
|
import { Category, Link } from "../types";
|
|
|
|
import { prisma } from "../utils/back";
|
|
import { BuildCategory } from "../utils/front";
|
|
|
|
interface HomeProps {
|
|
categories: Category[];
|
|
favorites: Link[];
|
|
}
|
|
|
|
function Home({ categories, favorites }: HomeProps) {
|
|
const { data } = useSession({ required: true });
|
|
const [categoryActive, setCategoryActive] = useState<Category | null>(
|
|
categories?.[0]
|
|
);
|
|
|
|
const handleSelectCategory = (category: Category) =>
|
|
setCategoryActive(category);
|
|
|
|
return (
|
|
<div className="App">
|
|
<SideMenu
|
|
categories={categories}
|
|
favorites={favorites}
|
|
handleSelectCategory={handleSelectCategory}
|
|
categoryActive={categoryActive}
|
|
session={data}
|
|
/>
|
|
<Links category={categoryActive} />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export async function getServerSideProps() {
|
|
const categoriesDB = await prisma.category.findMany({
|
|
include: { links: true },
|
|
});
|
|
|
|
const favorites = [] as Link[];
|
|
const categories = categoriesDB.map((categoryDB) => {
|
|
const category = BuildCategory(categoryDB);
|
|
category.links.map((link) => (link.favorite ? favorites.push(link) : null));
|
|
return category;
|
|
});
|
|
|
|
if (categories.length === 0) {
|
|
return {
|
|
redirect: {
|
|
destination: "/category/create",
|
|
},
|
|
};
|
|
}
|
|
|
|
return {
|
|
props: {
|
|
categories: JSON.parse(JSON.stringify(categories)),
|
|
favorites: JSON.parse(JSON.stringify(favorites)),
|
|
},
|
|
};
|
|
}
|
|
|
|
Home.authRequired = true;
|
|
export default Home;
|