From fb58de8803473104ad00f638ff100acef6e86b9f Mon Sep 17 00:00:00 2001 From: mariz Date: Wed, 22 Nov 2023 16:02:28 +0800 Subject: [PATCH] chore: make image work locally or live --- web/.env.example | 2 +- web/.env.production | 2 -- web/src/dojo/setupNetwork.ts | 7 +++---- web/src/global/constants.ts | 6 +++++- web/src/global/utils.ts | 12 ++++++++++++ 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/web/.env.example b/web/.env.example index e7e3eaf..4cef722 100644 --- a/web/.env.example +++ b/web/.env.example @@ -6,4 +6,4 @@ VITE_ACCOUNT_CLASS_HASH=0x4d07e40e93398ed3c76981e72dd1fd22557a78ce36c0515f679e27 VITE_PUBLIC_ETH_CONTRACT_ADDRESS=0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7 VITE_PUBLIC_NODE_URL=http://localhost:5050 -VITE_PUBLIC_TORII=http://localhost:8080/graphql +VITE_PUBLIC_TORII=http://localhost:8080 diff --git a/web/.env.production b/web/.env.production index 84dd6c3..8162b75 100644 --- a/web/.env.production +++ b/web/.env.production @@ -1,3 +1 @@ VITE_PUBLIC_ETH_CONTRACT_ADDRESS=0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7 -VITE_PUBLIC_NODE_URL=https://katana.demo.pixelaw.xyz/ -VITE_PUBLIC_TORII=https://torii.demo.pixelaw.xyz/graphql diff --git a/web/src/dojo/setupNetwork.ts b/web/src/dojo/setupNetwork.ts index 53cbad8..3245b33 100644 --- a/web/src/dojo/setupNetwork.ts +++ b/web/src/dojo/setupNetwork.ts @@ -6,13 +6,12 @@ import { GraphQLClient } from 'graphql-request'; import { getSdk } from '@/generated/graphql'; import { Manifest } from '@/global/types' import { streamToString } from '@/global/utils' +import { PUBLIC_NODE_URL, PUBLIC_TORII } from '@/global/constants' export type SetupNetworkResult = Awaited>; const MANIFEST_URL = '/manifests/core' export async function setupNetwork() { - // Extract environment variables for better readability. - const { VITE_PUBLIC_NODE_URL, VITE_PUBLIC_TORII } = import.meta.env; const manifest: Manifest = await (async () => { const result = await fetch(MANIFEST_URL) @@ -24,12 +23,12 @@ export async function setupNetwork() { const worldAddress = manifest.world.address ?? '' // Create a new RPCProvider instance. - const provider = new RPCProvider(worldAddress, manifest, VITE_PUBLIC_NODE_URL); + const provider = new RPCProvider(worldAddress, manifest, PUBLIC_NODE_URL); // Utility function to get the SDK. // Add in new queries or subscriptions in src/graphql/schema.graphql // then generate them using the codegen and fix-codegen commands in package.json - const createGraphSdk = () => getSdk(new GraphQLClient(VITE_PUBLIC_TORII)); + const createGraphSdk = () => getSdk(new GraphQLClient(`${PUBLIC_TORII}/graphql`)); // Return the setup object. return { diff --git a/web/src/global/constants.ts b/web/src/global/constants.ts index 9783da2..57714ae 100644 --- a/web/src/global/constants.ts +++ b/web/src/global/constants.ts @@ -1,4 +1,8 @@ -export const PUBLIC_NODE_URL: string = import.meta.env.VITE_PUBLIC_NODE_URL ?? 'http://localhost:5050' +import { getProductionUrl } from '@/global/utils' + +export const PUBLIC_TORII: string = import.meta.env.VITE_PUBLIC_TORII ?? getProductionUrl('torii') + +export const PUBLIC_NODE_URL: string = import.meta.env.VITE_PUBLIC_NODE_URL ?? getProductionUrl('katana') export const BLOCK_TIME = 1_000 diff --git a/web/src/global/utils.ts b/web/src/global/utils.ts index d0be37a..e27b2e4 100644 --- a/web/src/global/utils.ts +++ b/web/src/global/utils.ts @@ -69,3 +69,15 @@ export const argbToHex = (argb: number) => { const hexCode = convertToHexadecimalAndLeadWithOx(argb) return hexCode.replace("0xff", "#") } + +export const getProductionUrl = (type: 'katana' | 'torii') => { + const protocol = window.location.protocol + const hostname = window.location.hostname.replace('www.', '') + + if (hostname === 'localhost') { + if (type === 'katana') return 'http://localhost:5050' + else return 'http://localhost:8080' + } + + return`${protocol}//${type}.${hostname}` +}