File size: 968 Bytes
83003f5
 
 
 
 
 
 
 
 
 
 
 
 
a61ba58
83003f5
 
 
 
 
 
 
 
a61ba58
83003f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState, useEffect } from "react";
import { universeApi } from "../utils/api";

export const useGameSession = () => {
  const [sessionId, setSessionId] = useState(null);
  const [universe, setUniverse] = useState(null);
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    const initializeGame = async () => {
      try {
        setIsLoading(true);
        const { session_id, base_story, style, genre, epoch, macguffin } =
          await universeApi.generate();

        setSessionId(session_id);
        setUniverse({
          base_story,
          style,
          genre,
          epoch,
          macguffin,
        });
      } catch (err) {
        setError(err.message || "Failed to initialize game session");
      } finally {
        setIsLoading(false);
      }
    };

    initializeGame();
  }, []);

  return {
    sessionId,
    universe,
    isLoading,
    error,
  };
};