Skip to content

Commit

Permalink
Extract HistoryPage ui components into @ui
Browse files Browse the repository at this point in the history
  • Loading branch information
dongzoolee committed Sep 19, 2024
1 parent c831e66 commit aa5ee0c
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React, { createContext, useContext, useEffect, useState } from "react";

import { type THistoryData } from "../../../contexts/history-data-context";
import {
useHistoryDataContext,
type THistoryData,
} from "../../../contexts/history-data-context";

export type TSelectedHistoryContext = {
year: number;
Expand All @@ -15,14 +18,14 @@ export const useSelectedHistoryContext = () =>
useContext(SelectedHistoryContext);

export const SelectedHistoryContextProvider = ({
historyDataset,
initialValue,
children,
}: {
historyDataset: THistoryData["all"];
initialValue: Pick<TSelectedHistoryContext, "year">;
children: React.ReactNode;
}) => {
const { all: historyDataset } = useHistoryDataContext();

const [year, setYear] = useState(initialValue.year);
const [data, setData] = useState(historyDataset.find((d) => d.year === year));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { styled } from "styled-components";

import { Section } from "@ui/section/section";
import { Table } from "@ui/table/table";
import { AwardBadge, TAwardBadgeVariant } from "@ui/award-badge/award-badge";

import { useSelectedHistoryContext } from "../contexts/selected-history-context";
import { LinksAndReviews } from "./links-and-reviews";
import { AwardTable } from "@ui/award/award-table";
import { LinksAndReviews } from "@ui/award/links-and-reviews";
import { TAwardBadgeVariant } from "@ui/award-badge/award-badge";

const _HistoryDisplay = ({ className }: { className?: string }) => {
const selectedHistory = useSelectedHistoryContext();
Expand All @@ -17,41 +17,11 @@ const _HistoryDisplay = ({ className }: { className?: string }) => {
<Section key={title}>
<Section.Title>{title}</Section.Title>
<Section.Body>
<Table.Wrapper>
<Table>
<Table.Header>
<Table.Row>
{columns.map((h, i) => (
<Table.Head key={i}>{h}</Table.Head>
))}
</Table.Row>
</Table.Header>
<Table.Body>
{data.map((r, rowIndex) => (
<Table.Row key={rowIndex}>
{r.map((c, colIndex) => {
// FIXME: data 형식을 string[] -> json으로 일괄 변경해야 함
if (colIndex === 1) {
return (
<Table.Cell key={colIndex}>
{c}
{award[rowIndex] ? (
<AwardBadge
variant={
award[rowIndex] as TAwardBadgeVariant
}
/>
) : null}
</Table.Cell>
);
}
return <Table.Cell key={colIndex}>{c}</Table.Cell>;
})}
</Table.Row>
))}
</Table.Body>
</Table>
</Table.Wrapper>
<AwardTable
columns={columns}
data={data}
award={award as unknown as TAwardBadgeVariant[]}
/>
<LinksAndReviews links={links} review={review} />
</Section.Body>
</Section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@ import styled from "styled-components";

import { Page } from "@ui/page/page";
import { Section } from "@ui/section/section";
import { YearSelectorDropdown } from "@ui/dropdown/year-selector-dropdown";
import { MedalIconDescriptions } from "@ui/award/medal-icon-descriptions";

import ScoreboardImage from "./assets/scoreboard.jpg";
import { HistoryTab } from "./history-tab";
import { useHistoryDataContext } from "../../contexts/history-data-context";
import { SelectedHistoryContextProvider } from "./contexts/selected-history-context";
import {
SelectedHistoryContextProvider,
useSelectedHistoryContext,
} from "./contexts/selected-history-context";
import { HistoryDisplay } from "./history-display/history-display";
import constants from "../../contexts/assets/constants";
import { MedalIconDescriptions } from "./medal-icon-descriptions";

const YearDropdown = () => {
const { year, setYear } = useSelectedHistoryContext();
const historyData = useHistoryDataContext();
return (
<YearSelectorDropdown
year={year}
setYear={setYear}
items={historyData.years.reverse().map((y) => ({ label: y, value: y }))}
/>
);
};

const GrayText = styled.div`
font-size: 0.9rem;
Expand All @@ -22,26 +37,18 @@ const HeroImage = styled.img`
margin: 32px 0;
`;
const _HistoryPage = ({ className }: { className?: string }) => {
const historyData = useHistoryDataContext();
return (
<Page className={className}>
<Page.Title description="매년 여러 대회에 참가해 우수한 성적을 거두고 있습니다.">
기록
</Page.Title>
<Page.Body>
<HeroImage src={ScoreboardImage} alt="scoreboard" />
<SelectedHistoryContextProvider
historyDataset={historyData.all}
initialValue={{ year: 2024 }}
>
<SelectedHistoryContextProvider initialValue={{ year: 2024 }}>
<Section>
<Section.Title>
<span>연도별 대회 기록</span>
<HistoryTab
items={historyData.years
.reverse()
.map((y) => ({ label: y, value: y }))}
/>
<YearDropdown />
</Section.Title>
<Section.Body>
<HistoryDisplay />
Expand All @@ -65,7 +72,5 @@ export const HistoryPage = styled(_HistoryPage)`
display: flex;
justify-content: space-between;
align-items: center;
gap: 18px;
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Table } from "@ui/table/table";
import { AwardBadge, TAwardBadgeVariant } from "@ui/award-badge/award-badge";

export const AwardTable = ({
columns,
data,
award,
}: {
columns: string[];
data: string[][];
award: TAwardBadgeVariant[];
}) => {
return (
<Table.Wrapper>
<Table>
<Table.Header>
<Table.Row>
{columns.map((h, i) => (
<Table.Head key={i}>{h}</Table.Head>
))}
</Table.Row>
</Table.Header>
<Table.Body>
{data.map((r, rowIndex) => (
<Table.Row key={rowIndex}>
{r.map((c, colIndex) => {
// FIXME: data 형식을 string[] -> json으로 일괄 변경해야 함
if (colIndex === 1) {
return (
<Table.Cell key={colIndex}>
{c}
{award[rowIndex] ? (
<AwardBadge variant={award[rowIndex]} />
) : null}
</Table.Cell>
);
}
return <Table.Cell key={colIndex}>{c}</Table.Cell>;
})}
</Table.Row>
))}
</Table.Body>
</Table>
</Table.Wrapper>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Table } from "@ui/table/table";
import { OpenInANewTabButton } from "@ui/button/open-in-a-new-tab-button";
import { Button } from "@ui/button/button";

import { TSelectedHistoryContext } from "../contexts/selected-history-context";
import { TSelectedHistoryContext } from "../../../app/history-page/contexts/selected-history-context";
const ReviewTable = styled(Table)`
min-width: unset;
margin-top: 12px;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import styled, { createGlobalStyle, css } from "styled-components";

import { EmptyButton } from "@ui/button/empty-button";
import { Dropdown } from "@ui/dropdown/dropdown";

const _Item = ({
className,
label,
}: {
className?: string;
label: React.ReactNode;
selected: boolean;
}) => {
return <li className={className}>{label}</li>;
};
const Item = styled(_Item)`
width: fit-content;
font-size: 1.6rem;
letter-spacing: -1px;
color: #ddd;
cursor: pointer;
transition: 0.3s color ease;
${({ selected }) => {
if (selected) {
return css`
color: #000;
font-weight: bold;
`;
}
}}
`;

const DropdownTriggerButton = styled(EmptyButton)`
align-items: center;
gap: 0.4rem;
padding: 0.2rem 1rem;
border-radius: 12px;
background-color: #0000000f;
color: #535353;
font-size: 1.6rem;
letter-spacing: -1px;
cursor: pointer;
font-weight: bold;
transition: 0.3s color ease;
`;

const DROPDOWN_CLASSNAME = "history-tab__year__dropdown";
const DropdownStyles = createGlobalStyle`
.${DROPDOWN_CLASSNAME} ul {
display: grid;
grid-template-columns: repeat(4, 1fr);
li {
justify-content: center;
}
}
`;
const TriangleDownIcon = styled.span`
font-size: 12px;
`;

const _YearSelectorDropdown = ({
className,
items,
year,
setYear,
}: {
className?: string;
items: { label: React.ReactNode; value: number }[];
year: number;
setYear: React.Dispatch<React.SetStateAction<number>>;
}) => {
return (
<div className={className}>
<DropdownStyles />
<Dropdown
overlayClassName={DROPDOWN_CLASSNAME}
placement="bottomLeft"
trigger={["click"]}
menu={{
items: items.map((item) => ({
key: item.value,
onClick: () => setYear(item.value),
label: <Item selected={year === item.value} label={item.label} />,
})),
}}
children={
<DropdownTriggerButton
onClick={(e) => {
e.preventDefault();
}}
>
<span>{year}</span>
<TriangleDownIcon></TriangleDownIcon>
</DropdownTriggerButton>
}
/>
</div>
);
};
export const YearSelectorDropdown = styled(_YearSelectorDropdown)`
overflow-x: auto;
overflow-y: hidden;
white-space: nowrap;
&::-webkit-scrollbar {
height: 5px;
}
&::-webkit-scrollbar-track {
background: #fff;
}
&::-webkit-scrollbar-thumb {
background: #eee;
&:hover {
background: #eee;
}
}
`;

0 comments on commit aa5ee0c

Please sign in to comment.