Small Website to play Web-based games.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

131 line
3.2 KiB

  1. import type { NextPage } from 'next'
  2. import {GetServerSideProps} from 'next';
  3. import {Game} from '../../utils/games';
  4. import Link from 'next/link';
  5. import Head from 'next/head';
  6. import Image from 'next/image';
  7. type HomeProps = {
  8. games: Game[],
  9. }
  10. const GamesPage: NextPage<HomeProps> = ({
  11. games,
  12. }) => {
  13. return (
  14. <>
  15. <Head>
  16. <title>Flash Games</title>
  17. <style>{`
  18. body {
  19. background-color: #eee;
  20. }
  21. @media (min-width: 720px) {
  22. .games {
  23. grid-template-columns: 1fr 1fr;
  24. }
  25. }
  26. .foo {
  27. object-fit: cover;
  28. object-position: center;
  29. }
  30. `}</style>
  31. </Head>
  32. <div
  33. style={{
  34. width: '100%',
  35. maxWidth: 720,
  36. margin: '0 auto',
  37. padding: '0 2rem',
  38. boxSizing: 'border-box',
  39. }}
  40. >
  41. <ul
  42. className="games"
  43. style={{
  44. padding: 0,
  45. margin: '2rem 0',
  46. display: 'grid',
  47. gap: '2rem',
  48. gridGap: '2rem',
  49. }}
  50. >
  51. {
  52. games.map((g) => (
  53. <li
  54. key={g.id}
  55. style={{
  56. display: 'block',
  57. }}
  58. >
  59. <Link
  60. href={{
  61. pathname: '/games/[id]',
  62. query: {
  63. id: g.id,
  64. },
  65. }}
  66. passHref
  67. >
  68. <a
  69. href={`/games/${g.id}`}
  70. style={{
  71. display: 'block',
  72. height: '12rem',
  73. position: 'relative',
  74. overflow: 'hidden',
  75. borderRadius: '0.25rem',
  76. border: '1px solid #ddd',
  77. boxShadow: '0.125rem 0.25rem 0.25rem rgba(0,0,0,0.0625)'
  78. }}
  79. >
  80. <Image
  81. alt={g.name}
  82. src={`/games/${g.id}/${g.thumbnail}`}
  83. layout="fill"
  84. className="foo"
  85. />
  86. <div
  87. style={{
  88. position: 'absolute',
  89. bottom: 0,
  90. left: 0,
  91. width: '100%',
  92. textAlign: 'center',
  93. backgroundColor: 'white',
  94. padding: '0.5rem 0',
  95. boxSizing: 'border-box',
  96. }}
  97. >
  98. {g.name}
  99. </div>
  100. </a>
  101. </Link>
  102. </li>
  103. ))
  104. }
  105. </ul>
  106. </div>
  107. </>
  108. )
  109. }
  110. export const getServerSideProps: GetServerSideProps = async (ctx) => {
  111. const response = await fetch(`http://localhost:3000/api/games`);
  112. if (response.status !== 200) {
  113. return {
  114. notFound: true,
  115. }
  116. }
  117. const games = await response.json();
  118. return {
  119. props: {
  120. games,
  121. }
  122. }
  123. }
  124. export default GamesPage