GameStudio.jsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React from "react";
  2. import { Redirect } from "react-router-dom";
  3. import { useTranslation } from "react-i18next";
  4. import styled from "styled-components";
  5. import { useQuery } from "react-query";
  6. import { getGames } from "../utils/api";
  7. import useAuth from "../hooks/useAuth";
  8. import { StyledGameList } from "./StyledGameList";
  9. import NewGameItem from "./NewGameItem";
  10. import GameListItem from "./GameListItem";
  11. import Spinner from "./Spinner";
  12. const Filter = styled.div`
  13. & .incentive {
  14. width: 100%;
  15. text-align: center;
  16. font-size: 3.5vw;
  17. padding: 0.5em;
  18. margin: 0;
  19. }
  20. @media screen and (max-width: 1024px) {
  21. & .incentive {
  22. font-size: 32px;
  23. }
  24. }
  25. `;
  26. const Content = styled.div`
  27. background-color: var(--bg-secondary-color);
  28. `;
  29. const GameListView = () => {
  30. const { t } = useTranslation();
  31. const { isAuthenticated, userId } = useAuth();
  32. const { isLoading, data: gameList } = useQuery("ownGames", async () =>
  33. (await getGames())
  34. .filter(({ owner }) => userId && (!owner || owner === userId))
  35. .sort((a, b) => {
  36. const [nameA, nameB] = [
  37. a.board.defaultName || a.board.name,
  38. b.board.defaultName || b.board.name,
  39. ];
  40. if (nameA < nameB) {
  41. return -1;
  42. }
  43. if (nameA > nameB) {
  44. return 1;
  45. }
  46. return 0;
  47. })
  48. );
  49. if (!isAuthenticated) {
  50. return <Redirect to="/games/" />;
  51. }
  52. return (
  53. <Content>
  54. <Filter>
  55. <h2 className="incentive">{t("Your games")}</h2>
  56. </Filter>
  57. <StyledGameList>
  58. <NewGameItem />
  59. {!isLoading &&
  60. gameList.map((game) => (
  61. <GameListItem key={game.id} game={game} userId={userId} />
  62. ))}
  63. {isLoading && (
  64. <div style={{ paddingTop: "4em" }}>
  65. <Spinner />
  66. </div>
  67. )}
  68. </StyledGameList>
  69. </Content>
  70. );
  71. };
  72. export default GameListView;