Skip to content

Commit 90742a4

Browse files
committed
feat: improved distribution and auto complete
1 parent 8a6e266 commit 90742a4

5 files changed

Lines changed: 92 additions & 40 deletions

File tree

app/components/HomeSearch/SearchResults.tsx

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export interface SearchResultSummoner extends RecentSummoner {
1919

2020
export type SearchResult = SearchResultChallenge | SearchResultSummoner;
2121

22+
const MAX_VISIBLE_RESULTS = 5;
23+
2224
export default function SearchResults({
2325
results,
2426
visible,
@@ -41,8 +43,7 @@ export default function SearchResults({
4143
function handleSubmit() {
4244
if (pointer !== 0) {
4345
const result = document.querySelectorAll(`.${css.result}`)[pointer - 1] as
44-
| HTMLAnchorElement
45-
| undefined;
46+
HTMLAnchorElement | undefined;
4647
if (!result) return;
4748

4849
result.click();
@@ -107,21 +108,27 @@ export default function SearchResults({
107108
{results.recent.length > 0 && (
108109
<div className={css.group}>
109110
<span className={css.namespace}>Recent Pages</span>
110-
{results.recent.map((result, i) => (
111-
<Result key={i} result={result} active={pointer === i + 1} />
112-
))}
111+
{results.recent.map(
112+
(result, i) =>
113+
i < MAX_VISIBLE_RESULTS && (
114+
<Result key={i} result={result} active={pointer === i + 1} />
115+
),
116+
)}
113117
</div>
114118
)}
115119
{results.challenges.length > 0 && (
116120
<div className={css.group}>
117121
<span className={css.namespace}>Challenges</span>
118-
{results.challenges.map((result, i) => (
119-
<Result
120-
key={i}
121-
result={result}
122-
active={pointer === i + 1 + results.recent.length}
123-
/>
124-
))}
122+
{results.challenges.map(
123+
(result, i) =>
124+
i < MAX_VISIBLE_RESULTS && (
125+
<Result
126+
key={i}
127+
result={result}
128+
active={pointer === i + 1 + results.recent.length}
129+
/>
130+
),
131+
)}
125132
</div>
126133
)}
127134
{results.summoner.length > 0 && (

app/components/User/Statistics/Distribution.tsx

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { LineChart } from "@mui/x-charts";
1+
import { BarChart } from "@mui/x-charts";
22
import { useStaticData } from "@cgg/hooks/useStaticData";
3+
import cssVariables from "@cgg/styles/variables.module.scss";
34
import { capitalize } from "@cgg/utils/capitalize";
45
import type { IApiChallengeResponse } from "@cgg/utils/endpoints/types";
56
import { getChallenge } from "@cgg/utils/getChallenge";
@@ -8,15 +9,75 @@ import type { Tier } from "@cgg/utils/tier";
89

910
const tiers = ["UNRANKED" as Tier, ...tierList];
1011

12+
const fallbackTierColors: Record<Tier, string> = {
13+
NONE: "hsl(0, 0%, 41%)",
14+
UNRANKED: "hsl(0, 0%, 41%)",
15+
IRON: "hsl(0, 0%, 41%)",
16+
BRONZE: "hsl(17, 42%, 39%)",
17+
SILVER: "hsl(189, 12%, 56%)",
18+
GOLD: "hsl(31, 60%, 52%)",
19+
PLATINUM: "hsl(171, 65%, 36%)",
20+
DIAMOND: "hsl(230, 55%, 57%)",
21+
MASTER: "hsl(286, 64%, 55%)",
22+
GRANDMASTER: "hsl(351, 65%, 66%)",
23+
CHALLENGER: "hsl(40, 85%, 70%)",
24+
NONCHALLENGE: "hsl(0, 0%, 41%)",
25+
};
26+
27+
function getTierColor(tier: Tier): string {
28+
if (typeof document === "undefined") {
29+
return fallbackTierColors[tier];
30+
}
31+
32+
const colorNode = document.createElement("div");
33+
colorNode.className = cssVariables[tier];
34+
colorNode.style.position = "absolute";
35+
colorNode.style.visibility = "hidden";
36+
colorNode.style.pointerEvents = "none";
37+
colorNode.style.opacity = "0";
38+
document.body.appendChild(colorNode);
39+
40+
const color = getComputedStyle(colorNode).getPropertyValue("--tier").trim();
41+
colorNode.remove();
42+
43+
return color || fallbackTierColors[tier];
44+
}
45+
1146
export default function Distribution({
1247
playerData,
1348
}: {
1449
playerData: IApiChallengeResponse;
1550
}) {
1651
const data = useStaticData();
52+
const tierColors = tiers.reduce<Record<Tier, string>>(
53+
(acc, tier) => {
54+
acc[tier] = getTierColor(tier);
55+
return acc;
56+
},
57+
{} as Record<Tier, string>,
58+
);
59+
60+
const values = tiers.map((t) => {
61+
let amount = playerData.challenges.filter((c) => {
62+
const challenge = getChallenge(c.challengeId, data);
63+
if (!challenge) return false;
64+
if (challenge.retired) return false;
65+
66+
if (t === "UNRANKED") {
67+
return (["UNRANKED", "NONCHALLENGE", "NONE"] as Tier[]).includes(c.tier);
68+
}
69+
return c.tier === t;
70+
}).length;
71+
72+
if (t === "UNRANKED") {
73+
amount += Object.keys(data.challenges).length - playerData.challenges.length;
74+
}
75+
76+
return amount;
77+
});
1778

1879
return (
19-
<LineChart
80+
<BarChart
2081
xAxis={[
2182
{
2283
scaleType: "band",
@@ -25,33 +86,14 @@ export default function Distribution({
2586
]}
2687
series={[
2788
{
28-
data: tiers.map((t) => {
29-
let amount = playerData.challenges.filter((c) => {
30-
const challenge = getChallenge(c.challengeId, data);
31-
if (!challenge) return false;
32-
if (challenge.retired) return false;
33-
34-
if (t === "UNRANKED") {
35-
return (["UNRANKED", "NONCHALLENGE", "NONE"] as Tier[]).includes(
36-
c.tier,
37-
);
38-
}
39-
return c.tier === t;
40-
}).length;
41-
if (t === "UNRANKED") {
42-
amount +=
43-
Object.keys(data.challenges).length -
44-
playerData.challenges.length;
45-
}
46-
47-
return amount;
48-
}),
89+
data: values,
4990
label: "Challenges in tier",
50-
color: "#0dbdff",
91+
colorGetter: ({ dataIndex }) => tierColors[tiers[dataIndex]],
5192
},
5293
]}
5394
hideLegend={true}
5495
height={300}
96+
borderRadius={4}
5597
grid={{ vertical: true, horizontal: true }}
5698
/>
5799
);

app/components/User/Statistics/UserStatistics.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ export default function UserStatistics({
1515
<ThemeProvider theme={darkTheme}>
1616
<div className={css.wrapper}>
1717
<div className={css.section} style={{ gridArea: "1 / 1 / 2 / 3" }}>
18-
<Heading level={2}>Categories</Heading>
18+
<Heading level={2}>categories</Heading>
1919
<Categories playerData={playerData} />
2020
</div>
2121
<div className={css.section} style={{ gridArea: "2 / 2 / 3 / 3" }}>
22-
<Heading level={2}>Distribution</Heading>
22+
<Heading level={2}>tier distribution</Heading>
2323
<Distribution playerData={playerData} />
2424
</div>
2525
</div>

app/components/User/Statistics/userStatistics.module.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
h2 {
1919
margin-bottom: 0.5rem;
20+
text-transform: capitalize;
2021
}
2122
}
2223
}

app/utils/recents.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export function getRecentSearches(noEmptyResults = true): Recent[] {
5454
}
5555
}
5656

57+
const MAX_RECENTS = 25;
58+
5759
export function addRecentSearch(recent: Recent) {
5860
if (serverSide) return;
5961
const recents = getRecentSearches(false);
@@ -64,8 +66,8 @@ export function addRecentSearch(recent: Recent) {
6466
// add new recent to the front
6567
filteredRecents.unshift(recent);
6668

67-
if (filteredRecents.length > 5) {
68-
filteredRecents.length = 5;
69+
if (filteredRecents.length > 25) {
70+
filteredRecents.length = 25;
6971
}
7072
localStorage.setItem(storageNames.recents, JSON.stringify(filteredRecents));
7173
}

0 commit comments

Comments
 (0)