Utilisation variables d'env

This commit is contained in:
Mathieu Oriol
2025-03-14 18:06:29 +01:00
parent a7e7e076bd
commit 268910c2f3
6 changed files with 26 additions and 8 deletions

View File

@@ -2,6 +2,8 @@ import { Inter } from "next/font/google";
import "./globals.css"; import "./globals.css";
import SocketProvider from "@/context/socketContext"; import SocketProvider from "@/context/socketContext";
import { PublicEnvScript } from 'next-runtime-env';
const inter = Inter({ subsets: ["latin"] }); const inter = Inter({ subsets: ["latin"] });
export const metadata = { export const metadata = {
@@ -11,6 +13,9 @@ export const metadata = {
export default function RootLayout({ children }) { export default function RootLayout({ children }) {
return ( return (
<html lang="en"> <html lang="en">
<head>
<PublicEnvScript />
</head>
<SocketProvider> <SocketProvider>
<body className={inter.className + " h-screen"}>{children}</body> <body className={inter.className + " h-screen"}>{children}</body>
</SocketProvider> </SocketProvider>

View File

@@ -4,6 +4,8 @@ import BlueButton, { RedButton } from '../util/button';
import useAdmin from '@/hook/useAdmin'; import useAdmin from '@/hook/useAdmin';
import dynamic from 'next/dynamic'; import dynamic from 'next/dynamic';
import { env } from 'next-runtime-env';
const CircularAreaPicker = dynamic(() => import('./mapPicker').then((mod) => mod.CircularAreaPicker), { const CircularAreaPicker = dynamic(() => import('./mapPicker').then((mod) => mod.CircularAreaPicker), {
ssr: false ssr: false
}); });
@@ -13,11 +15,12 @@ export default function TeamEdit({ selectedTeamId, setSelectedTeamId }) {
const [newTeamName, setNewTeamName] = React.useState(''); const [newTeamName, setNewTeamName] = React.useState('');
const { updateTeam, getTeamName, removeTeam, getTeam, teams } = useAdmin(); const { updateTeam, getTeamName, removeTeam, getTeam, teams } = useAdmin();
const [team, setTeam] = useState({}); const [team, setTeam] = useState({});
const NEXT_PUBLIC_SOCKET_HOST = env("NEXT_PUBLIC_SOCKET_HOST");
var protocol = "https://"; var protocol = "https://";
if (process.env.NEXT_PUBLIC_SOCKET_HOST == "localhost") { if (NEXT_PUBLIC_SOCKET_HOST == "localhost") {
protocol = "http://"; protocol = "http://";
} }
const SERVER_URL = protocol + process.env.NEXT_PUBLIC_SOCKET_HOST + "/back"; const SERVER_URL = protocol + NEXT_PUBLIC_SOCKET_HOST + "/back";
console.log(SERVER_URL); console.log(SERVER_URL);
useEffect(() => { useEffect(() => {

View File

@@ -3,6 +3,8 @@ import { RedButton } from "../util/button";
import { useEffect, useRef } from "react"; import { useEffect, useRef } from "react";
import Image from "next/image"; import Image from "next/image";
import { env } from 'next-runtime-env';
export function EnemyTeamModal({ visible, onClose }) { export function EnemyTeamModal({ visible, onClose }) {
const { teamId, enemyName } = useGame(); const { teamId, enemyName } = useGame();
const imageRef = useRef(null); const imageRef = useRef(null);
@@ -18,10 +20,11 @@ export function EnemyTeamModal({ visible, onClose }) {
} }
var protocol = "https://"; var protocol = "https://";
if (process.env.NEXT_PUBLIC_SOCKET_HOST == "localhost") { const NEXT_PUBLIC_SOCKET_HOST = env("NEXT_PUBLIC_SOCKET_HOST");
if (NEXT_PUBLIC_SOCKET_HOST == "localhost") {
protocol = "http://"; protocol = "http://";
} }
const SERVER_URL = protocol + process.env.NEXT_PUBLIC_SOCKET_HOST + "/back"; const SERVER_URL = protocol + NEXT_PUBLIC_SOCKET_HOST + "/back";
console.log(SERVER_URL); console.log(SERVER_URL);
return (visible && return (visible &&
<> <>

View File

@@ -4,15 +4,18 @@ import { useRef } from "react";
import Image from "next/image"; import Image from "next/image";
import { useTeamContext } from "@/context/teamContext"; import { useTeamContext } from "@/context/teamContext";
import { env } from 'next-runtime-env';
export function WaitingScreen() { export function WaitingScreen() {
const { name, teamId } = useGame(); const { name, teamId } = useGame();
const { gameSettings } = useTeamContext(); const { gameSettings } = useTeamContext();
const imageRef = useRef(null); const imageRef = useRef(null);
const NEXT_PUBLIC_SOCKET_HOST = env("NEXT_PUBLIC_SOCKET_HOST");
var protocol = "https://"; var protocol = "https://";
if (process.env.NEXT_PUBLIC_SOCKET_HOST == "localhost") { if (NEXT_PUBLIC_SOCKET_HOST == "localhost") {
protocol = "http://"; protocol = "http://";
} }
const SERVER_URL = protocol + process.env.NEXT_PUBLIC_SOCKET_HOST + "/back"; const SERVER_URL = protocol + NEXT_PUBLIC_SOCKET_HOST + "/back";
console.log(SERVER_URL); console.log(SERVER_URL);
function sendImage() { function sendImage() {

View File

@@ -1,13 +1,16 @@
"use client"; "use client";
import { createContext, useContext, useMemo } from "react"; import { createContext, useContext, useMemo } from "react";
import { env } from 'next-runtime-env';
const { io } = require("socket.io-client"); const { io } = require("socket.io-client");
var proto = "wss://"; var proto = "wss://";
if (process.env.NEXT_PUBLIC_SOCKET_HOST == "localhost") { const NEXT_PUBLIC_SOCKET_HOST = env("NEXT_PUBLIC_SOCKET_HOST");
if (NEXT_PUBLIC_SOCKET_HOST == "localhost") {
proto = "ws://"; proto = "ws://";
} }
const SOCKET_URL = proto + process.env.NEXT_PUBLIC_SOCKET_HOST; const SOCKET_URL = proto + NEXT_PUBLIC_SOCKET_HOST;
const USER_SOCKET_URL = SOCKET_URL + "/player"; const USER_SOCKET_URL = SOCKET_URL + "/player";
const ADMIN_SOCKET_URL = SOCKET_URL + "/admin"; const ADMIN_SOCKET_URL = SOCKET_URL + "/admin";

View File

@@ -13,6 +13,7 @@
"@hello-pangea/dnd": "^16.6.0", "@hello-pangea/dnd": "^16.6.0",
"leaflet-defaulticon-compatibility": "^0.1.2", "leaflet-defaulticon-compatibility": "^0.1.2",
"next": "^14.2.9", "next": "^14.2.9",
"next-runtime-env": "^3.2.2",
"react": "^18", "react": "^18",
"react-dom": "^18", "react-dom": "^18",
"react-leaflet": "^4.2.1", "react-leaflet": "^4.2.1",