Compare commits

..
Author SHA1 Message Date
Brandon Thomas 68772b3fd2 website dynamically targets development in dev mode 2026-04-16 18:31:39 -04:00
2 changed files with 27 additions and 11 deletions
+10 -1
View File
@@ -3,7 +3,7 @@ import { BrowserRouter } from "react-router-dom";
import * as ReactDOM from "react-dom/client";
import { GoogleOAuthProvider } from "@react-oauth/google";
import App from "./app/app";
import { UsersApi } from "@storyteller/api";
import { StorytellerApiHostStore, UsersApi } from "@storyteller/api";
const GOOGLE_CLIENT_ID = import.meta.env.VITE_GOOGLE_CLIENT_ID;
@@ -11,6 +11,15 @@ const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement,
);
// In development, route API through the Vite dev server origin to avoid CORS
if (import.meta.env.DEV) {
try {
StorytellerApiHostStore.getInstance().setDevelopment();
} catch (e) {
console.warn("Failed to set dev API host override", e);
}
}
// Cache the referrer immediately — it can change or disappear on navigation.
// Only set once so subsequent in-site navigations don't overwrite the original.
if (!(window as any).cached_referrer) {
@@ -2,6 +2,7 @@
const DEFAULT_API_HOST_PRODUCTION = "https://api.storyteller.ai";
const DEFAULT_API_HOST_DEVELOPMENT = "http://localhost:12345";
// NB(bt,2025-09-25): 'nx' is creating multiple copies of the library with name
// mangling, so the singleton pattern fails to resolve to a single instance.
(window as any).STORYTELLER_API_HOST_STORE = undefined;
@@ -33,8 +34,7 @@ export class StorytellerApiHostStore {
if ((window as any).STORYTELLER_API_HOST_STORE !== undefined) {
return (window as any).STORYTELLER_API_HOST_STORE;
}
const defaultValue = StorytellerApiHostStore.getDefaultApiSchemeAndHost();
const instance = new StorytellerApiHostStore(defaultValue);
const instance = new StorytellerApiHostStore(DEFAULT_API_HOST_PRODUCTION);
(window as any).STORYTELLER_API_HOST_STORE = instance;
return instance;
}
@@ -45,6 +45,21 @@ export class StorytellerApiHostStore {
return this.apiSchemeAndHost;
}
/**
* This mutates the singleton instance to target development.
*/
public setDevelopment() {
console.warn("StorytellerApiHostStore.setDevelopment() - all calls will target development");
this.apiSchemeAndHost = DEFAULT_API_HOST_DEVELOPMENT;
}
/**
* This mutates the singleton instance to target production.
*/
public setProduction() {
this.apiSchemeAndHost = DEFAULT_API_HOST_PRODUCTION;
}
/**
* Externally update the API host.
* This is used to sync with Tauri for enabling easier development.
@@ -68,12 +83,4 @@ export class StorytellerApiHostStore {
private constructor(apiSchemeAndHost: string) {
this.apiSchemeAndHost = apiSchemeAndHost;
}
private static getDefaultApiSchemeAndHost(): string {
// NB: hostname can be empty string
if (window.location.hostname === "localhost") {
return DEFAULT_API_HOST_DEVELOPMENT;
}
return DEFAULT_API_HOST_PRODUCTION;
}
}