Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions webApp/src/components/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type AidRequest = {

type AdminSettings = {
fundWallet: string;
streamEndpoint: string;
apiToken: string;
};

Expand Down Expand Up @@ -80,10 +79,27 @@ function normalizeEndpoint(value: string): string {
return value.trim();
}

function detectStreamEndpoint(): string {
const configuredEndpoint = normalizeEndpoint(import.meta.env.VITE_STREAM_SENDER_ENDPOINT ?? '');
if (configuredEndpoint) {
return configuredEndpoint;
}

if (typeof window !== 'undefined') {
const { hostname, origin, port, protocol } = window.location;
if (port === '3000') {
return `${origin}/stream/send`;
}
return `${protocol}//${hostname}:3000/stream/send`;
}

return 'http://localhost:3000/stream/send';
}

function createInterledgerClient(): InterledgerClient {
let selfAccount = '';
let targetAccount = '';
let streamEndpoint = normalizeEndpoint(import.meta.env.VITE_STREAM_SENDER_ENDPOINT ?? '');
let streamEndpoint = detectStreamEndpoint();
let authToken = (import.meta.env.VITE_INTERLEDGER_API_TOKEN ?? '').trim();

return {
Expand Down Expand Up @@ -167,11 +183,12 @@ export function App() {
const [adminSettings, setAdminSettings] = useState<AdminSettings>(() =>
readStoredJson<AdminSettings>(SETTINGS_STORAGE_KEY, {
fundWallet: '',
streamEndpoint: import.meta.env.VITE_STREAM_SENDER_ENDPOINT ?? '',
apiToken: import.meta.env.VITE_INTERLEDGER_API_TOKEN ?? ''
})
);

const streamEndpoint = useMemo(() => detectStreamEndpoint(), []);

const [requestForm, setRequestForm] = useState<RequestFormState>({
requesterName: '',
contact: '',
Expand Down Expand Up @@ -311,16 +328,11 @@ export function App() {
return;
}

if (!adminSettings.streamEndpoint.trim()) {
setAdminNotice({ kind: 'error', message: 'Set the STREAM sender endpoint in Admin Settings first.' });
return;
}

setPayingRequestId(request.id);
setAdminNotice({ kind: 'idle', message: `Sending payout for ${request.id}...` });

try {
interledgerClient.setStreamEndpoint(adminSettings.streamEndpoint);
interledgerClient.setStreamEndpoint(streamEndpoint);
interledgerClient.setAuthToken(adminSettings.apiToken);
interledgerClient.setSelfAccount(adminSettings.fundWallet);
interledgerClient.setTargetAccount(request.wallet);
Expand Down Expand Up @@ -565,20 +577,7 @@ export function App() {
/>
</label>

<label>
STREAM Sender Endpoint
<input
type="url"
value={adminSettings.streamEndpoint}
onChange={(event) =>
saveAdminSettings({
...adminSettings,
streamEndpoint: event.target.value
})
}
placeholder="http://localhost:3000/stream/send"
/>
</label>
<p className="subtitle">STREAM sender endpoint is auto-detected: <code>{streamEndpoint}</code></p>

<label>
Bearer Token (optional)
Expand Down