PlayerScores.jsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import React from 'react';
  2. import styled from "styled-components";
  3. import Loading from '../../util/Loading';
  4. import Button from "../../util/Button";
  5. import PlayerScoreTile from "./PlayerScoreTile";
  6. import usePlayerScores from '../../../hooks/usePlayerScores';
  7. import ClickToCopy from '../../util/ClickToCopy';
  8. import useObservable from '../../../hooks/useObservable';
  9. import { gameIdStore, gameJoinedStore, gameStateStore } from '../../../domain/store';
  10. import { PRE_GAME } from '../../../domain/GameState';
  11. import HeaderAndFooter from '../../util/HeaderAndFooter';
  12. const Container = styled.div`
  13. display: flex;
  14. flex-flow: column nowrap;
  15. justify-content: space-around;
  16. align-items: center;
  17. height: 100%;
  18. `
  19. const ScoreBoard = styled.div`
  20. display: flex;
  21. flex-flow: row wrap;
  22. justify-content: space-evenly;
  23. align-items: flex-start;
  24. align-content: space-around;
  25. max-width: 80%;
  26. margin-top: 10%;
  27. `
  28. const LowerContainer = styled.div`
  29. display: flex;
  30. flex-flow: column nowrap;
  31. justify-content: space-between;
  32. align-items: center;
  33. margin: 1em;
  34. `
  35. const Label = styled.span`
  36. padding: 0.2em;
  37. margin-bottom: 0.5em;
  38. `
  39. const StyledButton = styled(Button)`
  40. margin-top: 0.5em;
  41. `
  42. const getUrl = gameId => {
  43. const u = new URL(window.location.href);
  44. u.searchParams.append("summary", gameId);
  45. return u.href;
  46. }
  47. export default () => {
  48. const gameId = useObservable(gameIdStore);
  49. const scores = usePlayerScores();
  50. const onReturnToStart = () => {
  51. gameJoinedStore.set(false);
  52. gameStateStore.set(PRE_GAME);
  53. }
  54. if (!scores) {
  55. return <Loading/>
  56. }
  57. return (
  58. <HeaderAndFooter>
  59. <Container>
  60. <ScoreBoard>
  61. {
  62. scores
  63. .filter(({ currentRound }) => currentRound === null)
  64. .sort((p1, p2) => p1.totalScore > p2.totalScore ? -1 : (p1.totalScore < p2.totalScore ? 1 : 0))
  65. .map((data) => <PlayerScoreTile key={data.name} {...data} />)
  66. }
  67. </ScoreBoard>
  68. <LowerContainer>
  69. <Label>This page can be directly linked with:</Label>
  70. <Label><ClickToCopy text={getUrl(gameId)} /></Label>
  71. <StyledButton onClick={onReturnToStart}>Return to Start</StyledButton>
  72. </LowerContainer>
  73. </Container>
  74. </HeaderAndFooter>
  75. );
  76. }