File size: 12,099 Bytes
81702ec
bf58a54
 
 
 
 
 
 
 
 
 
 
5470817
 
953a39e
81702ec
 
5470817
 
 
81702ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5470817
81702ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00c453b
 
 
 
 
81702ec
 
5470817
7154898
00c453b
7154898
00c453b
81702ec
 
00c453b
81702ec
 
 
 
 
00c453b
5470817
953a39e
81702ec
 
 
 
 
 
 
 
 
953a39e
 
5470817
 
 
 
 
 
 
81702ec
5470817
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00c453b
5470817
 
81702ec
5470817
953a39e
 
81702ec
5470817
 
 
 
81702ec
5470817
 
81702ec
 
 
 
 
 
5470817
 
953a39e
 
 
81702ec
953a39e
 
 
 
5470817
81702ec
5470817
 
 
81702ec
 
bf58a54
 
 
 
00c453b
bf58a54
 
 
 
 
 
 
 
81702ec
bf58a54
 
 
 
 
 
 
 
 
 
81702ec
 
bf58a54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81702ec
bf58a54
 
 
 
 
 
 
 
 
81702ec
 
 
 
 
 
bf58a54
 
 
 
 
 
 
 
 
 
 
81702ec
 
 
bf58a54
81702ec
 
bf58a54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81702ec
 
00c453b
81702ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd164b2
81702ec
 
 
 
 
 
 
953a39e
81702ec
 
 
5470817
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import React, { useState, useMemo } from "react";
import {
  Typography,
  Grid,
  Box,
  Button,
  Collapse,
  Stack,
  Accordion,
  AccordionSummary,
  AccordionDetails,
} from "@mui/material";
import { alpha } from "@mui/material/styles";
import LeaderboardCard from "./LeaderboardCard";
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
import { useLeaderboard } from "../context/LeaderboardContext";
import SearchOffIcon from "@mui/icons-material/SearchOff";

const ITEMS_PER_PAGE = 3;

const LeaderboardSection = ({
  title,
  leaderboards,
  filteredLeaderboards,
  id,
}) => {
  const {
    expandedSections,
    setExpandedSections,
    selectedLanguage,
    setSelectedLanguage,
    searchQuery,
    selectedCategory,
  } = useLeaderboard();
  const isExpanded = expandedSections.has(id);

  // Extraire la liste des langues si c'est la section Language Specific
  // Cette liste ne doit JAMAIS changer, peu importe les filtres
  const languages = useMemo(() => {
    if (id !== "language") return null;
    const langSet = new Set();
    leaderboards.forEach((board) => {
      board.tags?.forEach((tag) => {
        if (tag.startsWith("language:")) {
          const language = tag.split(":")[1];
          const capitalizedLang =
            language.charAt(0).toUpperCase() + language.slice(1);
          langSet.add(capitalizedLang);
        }
      });
    });
    return Array.from(langSet).sort();
  }, [id, leaderboards]);

  // Calculer le nombre de leaderboards par langue
  // On utilise les leaderboards bruts pour avoir toutes les langues
  const languageStats = useMemo(() => {
    if (!languages) return null;
    const stats = new Map();

    // Compter les leaderboards pour chaque langue
    languages.forEach((lang) => {
      const count = leaderboards.filter((board) =>
        board.tags?.some(
          (tag) => tag.toLowerCase() === `language:${lang.toLowerCase()}`
        )
      ).length;
      stats.set(lang, count);
    });

    return stats;
  }, [languages, leaderboards]);

  // Filtrer pour n'avoir que les leaderboards approuvés
  const approvedLeaderboards = filteredLeaderboards.filter(
    (leaderboard) => leaderboard.approval_status === "approved"
  );

  // On ne retourne null que si on n'a pas de leaderboards bruts
  if (!leaderboards) return null;

  // On affiche toujours les 3 premiers
  const displayedLeaderboards = approvedLeaderboards.slice(0, ITEMS_PER_PAGE);
  // Le reste sera dans le Collapse
  const remainingLeaderboards = approvedLeaderboards.slice(ITEMS_PER_PAGE);

  // Calculate how many skeletons we need
  const skeletonsNeeded = Math.max(0, 3 - approvedLeaderboards.length);

  // On affiche le bouton seulement si aucune catégorie n'est sélectionnée
  const showExpandButton = !selectedCategory;

  // Le bouton est actif seulement s'il y a plus de 3 leaderboards
  const isExpandButtonEnabled = approvedLeaderboards.length > ITEMS_PER_PAGE;

  const toggleExpanded = () => {
    setExpandedSections((prev) => {
      const newSet = new Set(prev);
      if (isExpanded) {
        newSet.delete(id);
      } else {
        newSet.add(id);
      }
      return newSet;
    });
  };

  return (
    <Box sx={{ mb: 6 }}>
      <Box
        sx={{
          display: "flex",
          alignItems: "center",
          justifyContent: "space-between",
          mb: languageStats ? 2 : 4,
        }}
      >
        <Box sx={{ display: "flex", alignItems: "center", gap: 2 }}>
          <Typography
            variant="h4"
            sx={{
              color: "text.primary",
              fontWeight: 600,
              fontSize: { xs: "1.5rem", md: "2rem" },
            }}
          >
            {title}
          </Typography>
          <Box
            sx={(theme) => ({
              width: "4px",
              height: "4px",
              borderRadius: "100%",
              backgroundColor: alpha(
                theme.palette.text.primary,
                theme.palette.mode === "dark" ? 0.2 : 0.15
              ),
            })}
          />
          <Typography
            variant="h4"
            sx={{
              color: "text.secondary",
              fontWeight: 400,
              fontSize: { xs: "1.25rem", md: "1.5rem" },
            }}
          >
            {approvedLeaderboards.length}
          </Typography>
        </Box>
        {showExpandButton && (
          <Button
            onClick={toggleExpanded}
            size="small"
            disabled={!isExpandButtonEnabled}
            sx={{
              color: "text.secondary",
              fontSize: "0.875rem",
              textTransform: "none",
              opacity: isExpandButtonEnabled ? 1 : 0.5,
              "&:hover": {
                backgroundColor: (theme) =>
                  isExpandButtonEnabled
                    ? alpha(
                        theme.palette.text.primary,
                        theme.palette.mode === "dark" ? 0.1 : 0.06
                      )
                    : "transparent",
              },
            }}
            endIcon={
              <ExpandMoreIcon
                sx={{
                  transform: isExpanded ? "rotate(180deg)" : "rotate(0deg)",
                  transition: "transform 300ms",
                }}
              />
            }
          >
            {isExpanded ? "Show less" : "Show more"}
          </Button>
        )}
      </Box>

      {languages && selectedCategory === "language" && (
        <Accordion
          defaultExpanded={false}
          elevation={0}
          sx={{
            mb: approvedLeaderboards.length > 0 ? 4 : 2,
            "&:before": {
              display: "none",
            },
            backgroundColor: "transparent",
          }}
        >
          <AccordionSummary
            expandIcon={<ExpandMoreIcon sx={{ fontSize: 20 }} />}
            sx={{
              padding: 0,
              "& .MuiAccordionSummary-content": {
                margin: 0,
              },
              "& .MuiAccordionSummary-expandIconWrapper": {
                position: "relative",
                right: "unset",
                marginLeft: 1,
                transform: "none",
              },
            }}
          >
            <Box sx={{ display: "flex", alignItems: "center" }}>
              <Typography
                variant="body2"
                color="text.secondary"
                sx={{ fontWeight: 500 }}
              >
                Languages represented in this category
              </Typography>
            </Box>
          </AccordionSummary>
          <AccordionDetails sx={{ padding: 0 }}>
            <Box
              sx={{
                display: "flex",
                flexWrap: "wrap",
                gap: 1,
                mx: -0.5,
              }}
            >
              {languages.map((lang) => {
                const isActive = selectedLanguage === lang;
                const count = languageStats?.get(lang) || 0;

                return (
                  <Button
                    key={lang}
                    onClick={() => setSelectedLanguage(isActive ? null : lang)}
                    variant={isActive ? "contained" : "outlined"}
                    size="small"
                    sx={{
                      textTransform: "none",
                      m: 0.125,
                      backgroundColor: (theme) =>
                        isActive
                          ? undefined
                          : theme.palette.mode === "dark"
                          ? "background.paper"
                          : "white",
                      "&:hover": {
                        backgroundColor: (theme) =>
                          isActive
                            ? undefined
                            : theme.palette.mode === "dark"
                            ? "background.paper"
                            : "white",
                      },
                      "& .MuiTouchRipple-root": {
                        transition: "none",
                      },
                      transition: "none",
                    }}
                  >
                    {lang}
                    <Box
                      component="span"
                      sx={{
                        display: "inline-flex",
                        alignItems: "center",
                        gap: 0.75,
                        color: isActive ? "inherit" : "text.secondary",
                        ml: 0.75,
                      }}
                    >
                      <Box
                        component="span"
                        sx={(theme) => ({
                          width: "4px",
                          height: "4px",
                          borderRadius: "100%",
                          backgroundColor: alpha(
                            theme.palette.text.primary,
                            theme.palette.mode === "dark" ? 0.2 : 0.15
                          ),
                        })}
                      />
                      {count}
                    </Box>
                  </Button>
                );
              })}
            </Box>
          </AccordionDetails>
        </Accordion>
      )}

      {approvedLeaderboards.length === 0 ? (
        <Box
          sx={{
            display: "flex",
            flexDirection: "column",
            alignItems: "center",
            gap: 2,
            py: 7,
            bgcolor: (theme) =>
              theme.palette.mode === "dark"
                ? "background.paper"
                : "background.default",
            borderRadius: 2,
          }}
        >
          <SearchOffIcon
            sx={{
              fontSize: 64,
              color: "text.secondary",
              opacity: 0.5,
            }}
          />
          <Typography variant="h5" color="text.secondary" align="center">
            {searchQuery ? (
              <>
                No {title.toLowerCase()} leaderboard matches{" "}
                <Box
                  component="span"
                  sx={{
                    bgcolor: "primary.main",
                    color: "primary.contrastText",
                    px: 1,
                    borderRadius: 1,
                  }}
                >
                  {searchQuery}
                </Box>
              </>
            ) : (
              `No ${title.toLowerCase()} leaderboard matches your criteria`
            )}
          </Typography>
          <Typography variant="body1" color="text.secondary" align="center">
            Try adjusting your search filters
          </Typography>
        </Box>
      ) : (
        <>
          <Grid container spacing={3}>
            {displayedLeaderboards.map((leaderboard, index) => (
              <Grid item xs={12} sm={6} md={4} key={index}>
                <LeaderboardCard leaderboard={leaderboard} />
              </Grid>
            ))}
            {/* Add skeletons if needed */}
            {Array.from({ length: skeletonsNeeded }).map((_, index) => (
              <Grid item xs={12} sm={6} md={4} key={`skeleton-${index}`}>
                <Box
                  sx={{
                    height: "180px",
                    borderRadius: 2,
                    bgcolor: (theme) => alpha(theme.palette.primary.main, 0.15),
                    opacity: 1,
                    transition: "opacity 0.3s ease-in-out",
                    "&:hover": {
                      opacity: 0.8,
                    },
                  }}
                />
              </Grid>
            ))}
          </Grid>
          <Collapse in={isExpanded} timeout={300} unmountOnExit>
            <Grid container spacing={3} sx={{ mt: 0 }}>
              {remainingLeaderboards.map((leaderboard, index) => (
                <Grid item xs={12} sm={6} md={4} key={index + ITEMS_PER_PAGE}>
                  <LeaderboardCard leaderboard={leaderboard} />
                </Grid>
              ))}
            </Grid>
          </Collapse>
        </>
      )}
    </Box>
  );
};

export default LeaderboardSection;