GameStudio.jsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 Spinner from "../ui/Spinner";
  9. import { StyledGameList } from "./StyledGameList";
  10. import NewGameItem from "./NewGameItem";
  11. import GameListItem from "./GameListItem";
  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, userAccount } = useAuth();
  32. const isAdmin = userAccount?.isAdmin;
  33. const { isLoading, data: gameList } = useQuery("ownGames", async () =>
  34. (await getGames())
  35. .filter(
  36. ({ owner }) => (userId && (!owner || owner === userId)) || isAdmin
  37. )
  38. .sort((a, b) => {
  39. const [nameA, nameB] = [
  40. a.board.defaultName || a.board.name,
  41. b.board.defaultName || b.board.name,
  42. ];
  43. if (nameA < nameB) {
  44. return -1;
  45. }
  46. if (nameA > nameB) {
  47. return 1;
  48. }
  49. return 0;
  50. })
  51. );
  52. if (!isAuthenticated) {
  53. return <Redirect to="/games/" />;
  54. }
  55. return (
  56. <Content>
  57. <Filter>
  58. <h2 className="incentive">{t("Your games")}</h2>
  59. </Filter>
  60. <StyledGameList>
  61. <NewGameItem />
  62. {!isLoading &&
  63. gameList.map((game) => (
  64. <GameListItem
  65. key={game.id}
  66. game={game}
  67. userId={userId}
  68. isAdmin={isAdmin}
  69. studio={true}
  70. />
  71. ))}
  72. {isLoading && (
  73. <div style={{ paddingTop: "4em" }}>
  74. <Spinner />
  75. </div>
  76. )}
  77. </StyledGameList>
  78. </Content>
  79. );
  80. };
  81. export default GameListView;