import React, { useState, useEffect } from "react"; import { GetServerSidePropsContext } from "next"; import { OpenSourceHeatmapProps } from "../../types/heatmap"; import { generateCalendarData } from "../../utils/calendar"; import Heatmap from "../../components/Heatmap"; import { fetchUserData, fetchAuthorData } from "../../utils/authors"; const DEFAULT_COLOR = "#FF9D00"; const OpenSourceHeatmap: React.FC = ({ calendarData, providers, }) => { const [isLoading, setIsLoading] = useState(true); useEffect(() => { if (calendarData && Object.keys(calendarData).length > 0) { setIsLoading(false); } }, [calendarData]); return (
{isLoading ? (

Loading...

) : (
{Object.entries(providers) .sort( ([keyA], [keyB]) => calendarData[keyB].reduce((sum, day) => sum + day.count, 0) - calendarData[keyA].reduce((sum, day) => sum + day.count, 0) ) .map(([providerName, { color, fullName, avatarUrl }]) => ( ))}
)}
); }; export async function getServerSideProps(context: GetServerSidePropsContext) { const { author, color } = context.query; const authorColor = color || DEFAULT_COLOR; try { const { fullName, avatarUrl } = await fetchUserData([author as string]); const providers = { [author as string]: { color: authorColor as string, authors: [author as string], fullName, avatarUrl, }, }; const flatData = await fetchAuthorData(author as string); const calendarData = generateCalendarData(flatData, [providers[author as string]]); return { props: { calendarData, color: authorColor, providers, }, }; } catch (error) { console.error("Error fetching data:", error); return { props: { calendarData: {}, color: authorColor, providers: { [author as string]: { color: authorColor as string, authors: [author as string], fullName: author, avatarUrl: null, }, }, }, }; } } export default OpenSourceHeatmap;