1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
import { useHydrateAtoms } from "jotai/utils";
import type { LoaderFunctionArgs, MetaFunction } from "react-router";
import { useLoaderData } from "react-router";
import { ensureUserLoggedIn } from "../.server/auth";
import {
ApiAuthTokenContext,
apiGetGame,
apiGetGamePlayLatestState,
} from "../api/client";
import GolfPlayApp from "../components/GolfPlayApp";
import {
setCurrentTimestampAtom,
setDurationSecondsAtom,
setGameStartedAtAtom,
setLatestGameStateAtom,
} from "../states/play";
export const meta: MetaFunction<typeof loader> = ({ data }) => [
{
title: data
? `Golf Playing ${data.game.display_name} | PHPerKaigi 2025 Albatross`
: "Golf Playing | PHPerKaigi 2025 Albatross",
},
];
export async function loader({ params, request }: LoaderFunctionArgs) {
const { token, user } = await ensureUserLoggedIn(request);
const gameId = Number(params.gameId);
const fetchGame = async () => {
return (await apiGetGame(token, gameId)).game;
};
const fetchGameState = async () => {
return (await apiGetGamePlayLatestState(token, gameId)).state;
};
const [game, state] = await Promise.all([fetchGame(), fetchGameState()]);
return {
apiAuthToken: token,
game,
player: user,
gameState: state,
};
}
export default function GolfPlay() {
const { apiAuthToken, game, player, gameState } =
useLoaderData<typeof loader>();
useHydrateAtoms([
[setCurrentTimestampAtom, undefined],
[setDurationSecondsAtom, game.duration_seconds],
[setGameStartedAtAtom, game.started_at ?? null],
[setLatestGameStateAtom, gameState],
]);
return (
<ApiAuthTokenContext.Provider value={apiAuthToken}>
<GolfPlayApp game={game} player={player} initialCode={gameState.code} />
</ApiAuthTokenContext.Provider>
);
}
|