PlayerScores.jsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. const Container = styled.div`
  9. display: flex;
  10. flex-flow: column nowrap;
  11. justify-content: space-around;
  12. align-items: center;
  13. `
  14. const ScoreBoard = styled.div`
  15. display: flex;
  16. flex-flow: row wrap;
  17. justify-content: space-around;
  18. align-items: center;
  19. align-content: space-around;
  20. `
  21. const Label = styled.span`
  22. padding: 0.2em;
  23. `
  24. const getUrl = gameId => {
  25. const u = new URL(window.location.href);
  26. u.searchParams.append("summary", gameId);
  27. return u.href;
  28. }
  29. export default ({ gameId, onReturnToStart }) => {
  30. const scores = usePlayerScores(gameId);
  31. if (!scores) {
  32. return <Loading/>
  33. }
  34. return (
  35. <Container>
  36. <ScoreBoard>
  37. {
  38. scores
  39. .filter(({ currentRound }) => currentRound === null)
  40. .sort((p1, p2) => p1.totalScore > p2.totalScore ? -1 : (p1.totalScore < p2.totalScore ? 1 : 0))
  41. .map(({ name, guesses, totalScore }) => <PlayerScoreTile key={name} {...{ name, guesses, totalScore }} />)
  42. }
  43. </ScoreBoard>
  44. <Label>This page can be directly linked with:</Label>
  45. <Label><ClickToCopy text={getUrl(gameId)} /></Label>
  46. <Button onClick={onReturnToStart}>Return to Start</Button>
  47. </Container>
  48. );
  49. }