Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

refactor(cloudcp): centralize Pulse Account frontend state

rcourtman6d4f47bf8c89
diff --git a/internal/cloudcp/portal/dist/build_manifest.json b/internal/cloudcp/portal/dist/build_manifest.json
index bbe2dc0004..e17a3b9372 100644
--- a/internal/cloudcp/portal/dist/build_manifest.json
+++ b/internal/cloudcp/portal/dist/build_manifest.json
@@ -1,5 +1,5 @@
{
- "source_hash": "33ad099dc74b695667a57e6d0ee611f46bb9f70d4b875e5eb84d192ea0173742",
+ "source_hash": "b84590f1de8004d9b2b91daf3e2c6080698c6ebdac90203874902c066d96d09d",
"build_inputs": [
"package.json",
"tsconfig.json",
@@ -7,6 +7,7 @@
"src/index.ts",
"src/account_controller.ts",
"src/auth_controller.ts",
+ "src/state.ts",
"src/shell.ts",
"src/shell_view.ts",
"src/services.ts",
diff --git a/internal/cloudcp/portal/dist/portal_app.js b/internal/cloudcp/portal/dist/portal_app.js
index eb818fe2e8..b9598bb57e 100644
--- a/internal/cloudcp/portal/dist/portal_app.js
+++ b/internal/cloudcp/portal/dist/portal_app.js
@@ -418,6 +418,129 @@
});
}
+ // src/state.ts
+ function emptyStatus() {
+ return {
+ visible: false,
+ message: "",
+ error: false
+ };
+ }
+ function newVerificationFlowState() {
+ return {
+ pendingEmail: "",
+ requesting: false,
+ confirming: false,
+ step2Visible: false,
+ status: emptyStatus(),
+ result: null,
+ emailValue: "",
+ codeValue: "",
+ checkboxChecked: false
+ };
+ }
+ function createPortalLoginState() {
+ return {
+ emailValue: "",
+ sending: false,
+ success: false,
+ error: ""
+ };
+ }
+ function createPortalServiceState() {
+ return {
+ openPanelID: "",
+ flows: {
+ manage: newVerificationFlowState(),
+ retrieve: newVerificationFlowState(),
+ export: newVerificationFlowState(),
+ delete: newVerificationFlowState()
+ },
+ refund: {
+ emailValue: "",
+ tokenValue: "",
+ submitting: false,
+ status: emptyStatus()
+ }
+ };
+ }
+ function syncLoginStateBootstrapEmail(loginState, email) {
+ if (!loginState.emailValue) {
+ loginState.emailValue = email || "";
+ }
+ }
+ function syncServiceStateBootstrapEmail(serviceState2, email) {
+ if (!serviceState2.flows.manage.emailValue) serviceState2.flows.manage.emailValue = email || "";
+ if (!serviceState2.flows.retrieve.emailValue) serviceState2.flows.retrieve.emailValue = email || "";
+ if (!serviceState2.flows.export.emailValue) serviceState2.flows.export.emailValue = email || "";
+ if (!serviceState2.flows.delete.emailValue) serviceState2.flows.delete.emailValue = email || "";
+ if (!serviceState2.refund.emailValue) serviceState2.refund.emailValue = email || "";
+ }
+ function setFlowStatus(serviceState2, flowID, message, isError) {
+ serviceState2.flows[flowID].status = {
+ visible: true,
+ message,
+ error: !!isError
+ };
+ }
+ function clearFlowStatus(serviceState2, flowID) {
+ serviceState2.flows[flowID].status = emptyStatus();
+ }
+ function setRefundStatus(serviceState2, message, isError) {
+ serviceState2.refund.status = {
+ visible: true,
+ message,
+ error: !!isError
+ };
+ }
+ function toggleServicePanelState(serviceState2, panelID) {
+ serviceState2.openPanelID = serviceState2.openPanelID === panelID ? "" : panelID;
+ }
+ function resetVerificationFlowState(serviceState2, flowID) {
+ var previous = serviceState2.flows[flowID];
+ serviceState2.flows[flowID] = newVerificationFlowState();
+ serviceState2.flows[flowID].emailValue = previous.emailValue;
+ }
+ function updateServiceInputValue(serviceState2, inputKind, value) {
+ switch (inputKind) {
+ case "manage-email":
+ serviceState2.flows.manage.emailValue = value;
+ return;
+ case "manage-code":
+ serviceState2.flows.manage.codeValue = value;
+ return;
+ case "retrieve-email":
+ serviceState2.flows.retrieve.emailValue = value;
+ return;
+ case "retrieve-code":
+ serviceState2.flows.retrieve.codeValue = value;
+ return;
+ case "refund-email":
+ serviceState2.refund.emailValue = value;
+ return;
+ case "refund-token":
+ serviceState2.refund.tokenValue = value;
+ return;
+ case "data-export-email":
+ serviceState2.flows.export.emailValue = value;
+ return;
+ case "data-export-code":
+ serviceState2.flows.export.codeValue = value;
+ return;
+ case "data-delete-email":
+ serviceState2.flows.delete.emailValue = value;
+ return;
+ case "data-delete-code":
+ serviceState2.flows.delete.codeValue = value;
+ return;
+ default:
+ return;
+ }
+ }
+ function updateDeleteConfirmation(serviceState2, checked) {
+ serviceState2.flows.delete.checkboxChecked = checked;
+ }
+
// src/auth_controller.ts
function asHTMLElement2(target) {
return target instanceof HTMLElement ? target : null;
@@ -426,16 +549,9 @@
return document.getElementById(id);
}
function installAuthController(deps) {
- var loginState = {
- emailValue: "",
- sending: false,
- success: false,
- error: ""
- };
+ var loginState = createPortalLoginState();
function syncBootstrapEmail(email) {
- if (!loginState.emailValue) {
- loginState.emailValue = email || "";
- }
+ syncLoginStateBootstrapEmail(loginState, email);
}
async function sendMagicLink() {
var email = String(loginState.emailValue || "").trim();
@@ -891,41 +1007,7 @@
}
// src/services.ts
- var serviceState = {
- openPanelID: "",
- flows: {
- manage: newVerificationFlowState(),
- retrieve: newVerificationFlowState(),
- export: newVerificationFlowState(),
- delete: newVerificationFlowState()
- },
- refund: {
- emailValue: "",
- tokenValue: "",
- submitting: false,
- status: emptyStatus()
- }
- };
- function newVerificationFlowState() {
- return {
- pendingEmail: "",
- requesting: false,
- confirming: false,
- step2Visible: false,
- status: emptyStatus(),
- result: null,
- emailValue: "",
- codeValue: "",
- checkboxChecked: false
- };
- }
- function emptyStatus() {
- return {
- visible: false,
- message: "",
- error: false
- };
- }
+ var serviceState = createPortalServiceState();
function getCommercialAPIBaseURL2() {
return getCommercialAPIBaseURL();
}
@@ -936,25 +1018,8 @@
body: JSON.stringify(body)
});
}
- function setFlowStatus(flowID, message, isError) {
- serviceState.flows[flowID].status = {
- visible: true,
- message,
- error: !!isError
- };
- }
- function clearFlowStatus(flowID) {
- serviceState.flows[flowID].status = emptyStatus();
- }
- function setRefundStatus(message, isError) {
- serviceState.refund.status = {
- visible: true,
- message,
- error: !!isError
- };
- }
function toggleServicePanel(panelID) {
- serviceState.openPanelID = serviceState.openPanelID === panelID ? "" : panelID;
+ toggleServicePanelState(serviceState, panelID);
renderOpenPanels(serviceState.openPanelID);
}
function renderFlow(flowID) {
@@ -989,9 +1054,7 @@
function resetVerificationFlow(flowID) {
var flow = verificationFlows[flowID];
if (!flow) return;
- var previous = serviceState.flows[flowID];
- serviceState.flows[flowID] = newVerificationFlowState();
- serviceState.flows[flowID].emailValue = previous.emailValue;
+ resetVerificationFlowState(serviceState, flowID);
if (flow.codeInputID) {
setValue(flow.codeInputID, "");
}
@@ -1058,7 +1121,7 @@
onConfirmSuccess: function(data) {
serviceState.flows.retrieve.result = data.license;
serviceState.flows.retrieve.codeValue = "";
- setFlowStatus("retrieve", "License retrieved successfully.", false);
+ setFlowStatus(serviceState, "retrieve", "License retrieved successfully.", false);
},
renderPanel: renderRetrievePanel
},
@@ -1092,7 +1155,7 @@
onConfirmSuccess: function(data) {
serviceState.flows.export.result = data;
serviceState.flows.export.codeValue = "";
- setFlowStatus("export", "Data export retrieved successfully.", false);
+ setFlowStatus(serviceState, "export", "Data export retrieved successfully.", false);
resetVerificationFlow("export");
serviceState.flows.export.result = data;
},
@@ -1125,7 +1188,7 @@
},
beforeConfirm: function() {
if (!getElement3("data-delete-confirm-check")?.checked) {
- setFlowStatus("delete", "You must confirm that you understand this action is permanent.", true);
+ setFlowStatus(serviceState, "delete", "You must confirm that you understand this action is permanent.", true);
renderFlow("delete");
return false;
}
@@ -1137,7 +1200,7 @@
checkbox.checked = false;
}
resetVerificationFlow("delete");
- setFlowStatus("delete", data.deleted_count > 0 && data.stripe_reminder ? data.message + " " + data.stripe_reminder : data.message, false);
+ setFlowStatus(serviceState, "delete", data.deleted_count > 0 && data.stripe_reminder ? data.message + " " + data.stripe_reminder : data.message, false);
},
renderPanel: renderDeletePanel
}
@@ -1154,7 +1217,7 @@
flow.onRequestStart();
}
serviceState.flows[flowID].requesting = true;
- clearFlowStatus(flowID);
+ clearFlowStatus(serviceState, flowID);
renderFlow(flowID);
try {
var res = await serviceFetch(flow.requestPath, { email });
@@ -1162,9 +1225,9 @@
if (!res.ok) throw new Error(data.error || flow.requestErrorMessage);
serviceState.flows[flowID].pendingEmail = email;
serviceState.flows[flowID].step2Visible = !!flow.step2ID;
- setFlowStatus(flowID, flow.requestSuccessMessage, false);
+ setFlowStatus(serviceState, flowID, flow.requestSuccessMessage, false);
} catch (err) {
- setFlowStatus(flowID, err.message, true);
+ setFlowStatus(serviceState, flowID, err.message, true);
} finally {
serviceState.flows[flowID].requesting = false;
renderFlow(flowID);
@@ -1180,9 +1243,9 @@
var res = await serviceFetch(flow.requestPath, { email });
var data = await res.json();
if (!res.ok) throw new Error(data.error || flow.requestErrorMessage);
- setFlowStatus(flowID, flow.resendSuccessMessage, false);
+ setFlowStatus(serviceState, flowID, flow.resendSuccessMessage, false);
} catch (err) {
- setFlowStatus(flowID, err.message, true);
+ setFlowStatus(serviceState, flowID, err.message, true);
}
renderFlow(flowID);
}
@@ -1203,7 +1266,7 @@
if (!res.ok) throw new Error(data.error || flow.confirmErrorMessage);
flow.onConfirmSuccess(data, email);
} catch (err) {
- setFlowStatus(flowID, err.message, true);
+ setFlowStatus(serviceState, flowID, err.message, true);
} finally {
serviceState.flows[flowID].confirming = false;
renderFlow(flowID);
@@ -1215,9 +1278,9 @@
if (!token) return;
try {
await navigator.clipboard.writeText(token);
- setFlowStatus("retrieve", "License key copied to clipboard.", false);
+ setFlowStatus(serviceState, "retrieve", "License key copied to clipboard.", false);
} catch (_) {
- setFlowStatus("retrieve", "Failed to copy automatically. Please copy the key manually.", true);
+ setFlowStatus(serviceState, "retrieve", "Failed to copy automatically. Please copy the key manually.", true);
}
renderFlow("retrieve");
}
@@ -1234,9 +1297,9 @@
var data = await res.json();
if (!res.ok) throw new Error(data.error || "Refund failed");
serviceState.refund.tokenValue = "";
- setRefundStatus("Success! Your refund has been processed. Stripe will follow up by email.", false);
+ setRefundStatus(serviceState, "Success! Your refund has been processed. Stripe will follow up by email.", false);
} catch (err) {
- setRefundStatus(err.message, true);
+ setRefundStatus(serviceState, err.message, true);
} finally {
serviceState.refund.submitting = false;
renderRefund();
@@ -1247,11 +1310,7 @@
if (!bootstrap.authenticated) {
return;
}
- if (!serviceState.flows.manage.emailValue) serviceState.flows.manage.emailValue = bootstrap.email || "";
- if (!serviceState.flows.retrieve.emailValue) serviceState.flows.retrieve.emailValue = bootstrap.email || "";
- if (!serviceState.flows.export.emailValue) serviceState.flows.export.emailValue = bootstrap.email || "";
- if (!serviceState.flows.delete.emailValue) serviceState.flows.delete.emailValue = bootstrap.email || "";
- if (!serviceState.refund.emailValue) serviceState.refund.emailValue = bootstrap.email || "";
+ syncServiceStateBootstrapEmail(serviceState, bootstrap.email || "");
}
function renderServiceRuntime() {
syncServiceStateFromBootstrap();
@@ -1260,42 +1319,6 @@
}
renderServiceRuntime();
subscribePortalRender(renderServiceRuntime);
- function updateInputValue(inputKind, value) {
- switch (inputKind) {
- case "manage-email":
- serviceState.flows.manage.emailValue = value;
- return;
- case "manage-code":
- serviceState.flows.manage.codeValue = value;
- return;
- case "retrieve-email":
- serviceState.flows.retrieve.emailValue = value;
- return;
- case "retrieve-code":
- serviceState.flows.retrieve.codeValue = value;
- return;
- case "refund-email":
- serviceState.refund.emailValue = value;
- return;
- case "refund-token":
- serviceState.refund.tokenValue = value;
- return;
- case "data-export-email":
- serviceState.flows.export.emailValue = value;
- return;
- case "data-export-code":
- serviceState.flows.export.codeValue = value;
- return;
- case "data-delete-email":
- serviceState.flows.delete.emailValue = value;
- return;
- case "data-delete-code":
- serviceState.flows.delete.codeValue = value;
- return;
- default:
- return;
- }
- }
installServicesController({
toggleServicePanel,
focusElement,
@@ -1314,9 +1337,11 @@
submitRefund: function() {
void submitRefund();
},
- updateInputValue,
+ updateInputValue: function(inputKind, value) {
+ updateServiceInputValue(serviceState, inputKind, value);
+ },
updateDeleteConfirmation: function(checked) {
- serviceState.flows.delete.checkboxChecked = checked;
+ updateDeleteConfirmation(serviceState, checked);
}
});
})();
diff --git a/internal/cloudcp/portal/frontend/build.mjs b/internal/cloudcp/portal/frontend/build.mjs
index bdf7ba320f..26d0766e77 100644
--- a/internal/cloudcp/portal/frontend/build.mjs
+++ b/internal/cloudcp/portal/frontend/build.mjs
@@ -20,6 +20,7 @@ const buildInputs = [
'src/index.ts',
'src/account_controller.ts',
'src/auth_controller.ts',
+ 'src/state.ts',
'src/shell.ts',
'src/shell_view.ts',
'src/services.ts',
diff --git a/internal/cloudcp/portal/frontend/src/auth_controller.ts b/internal/cloudcp/portal/frontend/src/auth_controller.ts
index c5da8f65bf..ee89ec54f3 100644
--- a/internal/cloudcp/portal/frontend/src/auth_controller.ts
+++ b/internal/cloudcp/portal/frontend/src/auth_controller.ts
@@ -1,3 +1,4 @@
+import { createPortalLoginState, syncLoginStateBootstrapEmail } from './state';
import type { PortalLoginState } from './types';
type FormValueElement = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
@@ -23,17 +24,10 @@ function getElement<T extends HTMLElement = HTMLElement>(id: string): T | null {
}
export function installAuthController(deps: AuthControllerDeps): AuthController {
- var loginState: PortalLoginState = {
- emailValue: '',
- sending: false,
- success: false,
- error: '',
- };
+ var loginState: PortalLoginState = createPortalLoginState();
function syncBootstrapEmail(email: string) {
- if (!loginState.emailValue) {
- loginState.emailValue = email || '';
- }
+ syncLoginStateBootstrapEmail(loginState, email);
}
async function sendMagicLink() {
diff --git a/internal/cloudcp/portal/frontend/src/services.ts b/internal/cloudcp/portal/frontend/src/services.ts
index 909f6eec82..5ceb8e6007 100644
--- a/internal/cloudcp/portal/frontend/src/services.ts
+++ b/internal/cloudcp/portal/frontend/src/services.ts
@@ -1,5 +1,17 @@
import { getBootstrap, getCommercialAPIBaseURL as readCommercialAPIBaseURL, subscribePortalRender } from './runtime';
import { installServicesController } from './services_controller';
+import {
+ clearFlowStatus,
+ createPortalServiceState,
+ emptyStatus,
+ resetVerificationFlowState,
+ setFlowStatus,
+ setRefundStatus,
+ syncServiceStateBootstrapEmail,
+ toggleServicePanelState,
+ updateDeleteConfirmation,
+ updateServiceInputValue,
+} from './state';
import {
focusElement,
getElement,
@@ -16,9 +28,7 @@ import {
setValue,
setVisible,
} from './services_view';
-import type { RefundState, ServiceStatus, VerificationFlowState } from './types';
-
-type FlowID = 'manage' | 'retrieve' | 'export' | 'delete';
+import type { PortalServiceFlowID, VerificationFlowState } from './types';
interface VerificationFlowDefinition {
requestPath: string;
@@ -47,43 +57,7 @@ interface VerificationFlowDefinition {
renderResult?: (result: unknown) => void;
}
- var serviceState = {
- openPanelID: '',
- flows: {
- manage: newVerificationFlowState(),
- retrieve: newVerificationFlowState(),
- export: newVerificationFlowState(),
- delete: newVerificationFlowState(),
- },
- refund: {
- emailValue: '',
- tokenValue: '',
- submitting: false,
- status: emptyStatus(),
- },
- } as { openPanelID: string; flows: Record<FlowID, VerificationFlowState>; refund: RefundState };
-
- function newVerificationFlowState(): VerificationFlowState {
- return {
- pendingEmail: '',
- requesting: false,
- confirming: false,
- step2Visible: false,
- status: emptyStatus(),
- result: null,
- emailValue: '',
- codeValue: '',
- checkboxChecked: false,
- };
- }
-
- function emptyStatus(): ServiceStatus {
- return {
- visible: false,
- message: '',
- error: false,
- };
- }
+ var serviceState = createPortalServiceState();
function getCommercialAPIBaseURL() {
return readCommercialAPIBaseURL();
@@ -97,32 +71,12 @@ interface VerificationFlowDefinition {
});
}
- function setFlowStatus(flowID: FlowID, message, isError) {
- serviceState.flows[flowID].status = {
- visible: true,
- message: message,
- error: !!isError,
- };
- }
-
- function clearFlowStatus(flowID: FlowID) {
- serviceState.flows[flowID].status = emptyStatus();
- }
-
- function setRefundStatus(message, isError) {
- serviceState.refund.status = {
- visible: true,
- message: message,
- error: !!isError,
- };
- }
-
function toggleServicePanel(panelID) {
- serviceState.openPanelID = serviceState.openPanelID === panelID ? '' : panelID;
+ toggleServicePanelState(serviceState, panelID);
renderOpenPanels(serviceState.openPanelID);
}
- function renderFlow(flowID) {
+ function renderFlow(flowID: PortalServiceFlowID) {
var flow = verificationFlows[flowID];
if (!flow) return;
var flowState = serviceState.flows[flowID];
@@ -154,18 +108,16 @@ interface VerificationFlowDefinition {
renderStatus('refund-inline-status', serviceState.refund.status);
}
- function resetVerificationFlow(flowID: FlowID) {
+ function resetVerificationFlow(flowID: PortalServiceFlowID) {
var flow = verificationFlows[flowID];
if (!flow) return;
- var previous = serviceState.flows[flowID];
- serviceState.flows[flowID] = newVerificationFlowState();
- serviceState.flows[flowID].emailValue = previous.emailValue;
+ resetVerificationFlowState(serviceState, flowID);
if (flow.codeInputID) {
setValue(flow.codeInputID, '');
}
}
- var verificationFlows: Record<FlowID, VerificationFlowDefinition> = {
+ var verificationFlows: Record<PortalServiceFlowID, VerificationFlowDefinition> = {
manage: {
requestPath: '/v1/manage/request',
confirmPath: '/v1/manage',
@@ -226,7 +178,7 @@ interface VerificationFlowDefinition {
onConfirmSuccess: function(data) {
serviceState.flows.retrieve.result = data.license;
serviceState.flows.retrieve.codeValue = '';
- setFlowStatus('retrieve', 'License retrieved successfully.', false);
+ setFlowStatus(serviceState, 'retrieve', 'License retrieved successfully.', false);
},
renderPanel: renderRetrievePanel
},
@@ -260,7 +212,7 @@ interface VerificationFlowDefinition {
onConfirmSuccess: function(data) {
serviceState.flows.export.result = data;
serviceState.flows.export.codeValue = '';
- setFlowStatus('export', 'Data export retrieved successfully.', false);
+ setFlowStatus(serviceState, 'export', 'Data export retrieved successfully.', false);
resetVerificationFlow('export');
serviceState.flows.export.result = data;
},
@@ -293,7 +245,7 @@ interface VerificationFlowDefinition {
},
beforeConfirm: function() {
if (!getElement<HTMLInputElement>('data-delete-confirm-check')?.checked) {
- setFlowStatus('delete', 'You must confirm that you understand this action is permanent.', true);
+ setFlowStatus(serviceState, 'delete', 'You must confirm that you understand this action is permanent.', true);
renderFlow('delete');
return false;
}
@@ -305,13 +257,13 @@ interface VerificationFlowDefinition {
checkbox.checked = false;
}
resetVerificationFlow('delete');
- setFlowStatus('delete', data.deleted_count > 0 && data.stripe_reminder ? data.message + ' ' + data.stripe_reminder : data.message, false);
+ setFlowStatus(serviceState, 'delete', data.deleted_count > 0 && data.stripe_reminder ? data.message + ' ' + data.stripe_reminder : data.message, false);
},
renderPanel: renderDeletePanel
}
};
- async function requestVerificationCode(flowID: FlowID) {
+ async function requestVerificationCode(flowID: PortalServiceFlowID) {
var flow = verificationFlows[flowID];
if (!flow) return;
var email = flow.readEmailValue ? flow.readEmailValue() : readValue(flow.emailInputID);
@@ -323,7 +275,7 @@ interface VerificationFlowDefinition {
flow.onRequestStart();
}
serviceState.flows[flowID].requesting = true;
- clearFlowStatus(flowID);
+ clearFlowStatus(serviceState, flowID);
renderFlow(flowID);
try {
var res = await serviceFetch(flow.requestPath, { email: email });
@@ -331,16 +283,16 @@ interface VerificationFlowDefinition {
if (!res.ok) throw new Error(data.error || flow.requestErrorMessage);
serviceState.flows[flowID].pendingEmail = email;
serviceState.flows[flowID].step2Visible = !!flow.step2ID;
- setFlowStatus(flowID, flow.requestSuccessMessage, false);
+ setFlowStatus(serviceState, flowID, flow.requestSuccessMessage, false);
} catch (err) {
- setFlowStatus(flowID, err.message, true);
+ setFlowStatus(serviceState, flowID, err.message, true);
} finally {
serviceState.flows[flowID].requesting = false;
renderFlow(flowID);
}
}
- async function resendVerificationCode(flowID: FlowID, event) {
+ async function resendVerificationCode(flowID: PortalServiceFlowID, event?: Event) {
if (event) event.preventDefault();
var flow = verificationFlows[flowID];
if (!flow) return;
@@ -350,14 +302,14 @@ interface VerificationFlowDefinition {
var res = await serviceFetch(flow.requestPath, { email: email });
var data = await res.json();
if (!res.ok) throw new Error(data.error || flow.requestErrorMessage);
- setFlowStatus(flowID, flow.resendSuccessMessage, false);
+ setFlowStatus(serviceState, flowID, flow.resendSuccessMessage, false);
} catch (err) {
- setFlowStatus(flowID, err.message, true);
+ setFlowStatus(serviceState, flowID, err.message, true);
}
renderFlow(flowID);
}
- async function confirmVerificationCode(flowID: FlowID) {
+ async function confirmVerificationCode(flowID: PortalServiceFlowID) {
var flow = verificationFlows[flowID];
if (!flow) return;
var email = serviceState.flows[flowID].pendingEmail;
@@ -374,7 +326,7 @@ interface VerificationFlowDefinition {
if (!res.ok) throw new Error(data.error || flow.confirmErrorMessage);
flow.onConfirmSuccess(data, email);
} catch (err) {
- setFlowStatus(flowID, err.message, true);
+ setFlowStatus(serviceState, flowID, err.message, true);
} finally {
serviceState.flows[flowID].confirming = false;
renderFlow(flowID);
@@ -387,9 +339,9 @@ interface VerificationFlowDefinition {
if (!token) return;
try {
await navigator.clipboard.writeText(token);
- setFlowStatus('retrieve', 'License key copied to clipboard.', false);
+ setFlowStatus(serviceState, 'retrieve', 'License key copied to clipboard.', false);
} catch (_) {
- setFlowStatus('retrieve', 'Failed to copy automatically. Please copy the key manually.', true);
+ setFlowStatus(serviceState, 'retrieve', 'Failed to copy automatically. Please copy the key manually.', true);
}
renderFlow('retrieve');
}
@@ -407,9 +359,9 @@ interface VerificationFlowDefinition {
var data = await res.json();
if (!res.ok) throw new Error(data.error || 'Refund failed');
serviceState.refund.tokenValue = '';
- setRefundStatus('Success! Your refund has been processed. Stripe will follow up by email.', false);
+ setRefundStatus(serviceState, 'Success! Your refund has been processed. Stripe will follow up by email.', false);
} catch (err) {
- setRefundStatus(err.message, true);
+ setRefundStatus(serviceState, err.message, true);
} finally {
serviceState.refund.submitting = false;
renderRefund();
@@ -421,11 +373,7 @@ interface VerificationFlowDefinition {
if (!bootstrap.authenticated) {
return;
}
- if (!serviceState.flows.manage.emailValue) serviceState.flows.manage.emailValue = bootstrap.email || '';
- if (!serviceState.flows.retrieve.emailValue) serviceState.flows.retrieve.emailValue = bootstrap.email || '';
- if (!serviceState.flows.export.emailValue) serviceState.flows.export.emailValue = bootstrap.email || '';
- if (!serviceState.flows.delete.emailValue) serviceState.flows.delete.emailValue = bootstrap.email || '';
- if (!serviceState.refund.emailValue) serviceState.refund.emailValue = bootstrap.email || '';
+ syncServiceStateBootstrapEmail(serviceState, bootstrap.email || '');
}
function renderServiceRuntime() {
@@ -437,43 +385,6 @@ interface VerificationFlowDefinition {
renderServiceRuntime();
subscribePortalRender(renderServiceRuntime);
- function updateInputValue(inputKind: string, value: string) {
- switch (inputKind) {
- case 'manage-email':
- serviceState.flows.manage.emailValue = value;
- return;
- case 'manage-code':
- serviceState.flows.manage.codeValue = value;
- return;
- case 'retrieve-email':
- serviceState.flows.retrieve.emailValue = value;
- return;
- case 'retrieve-code':
- serviceState.flows.retrieve.codeValue = value;
- return;
- case 'refund-email':
- serviceState.refund.emailValue = value;
- return;
- case 'refund-token':
- serviceState.refund.tokenValue = value;
- return;
- case 'data-export-email':
- serviceState.flows.export.emailValue = value;
- return;
- case 'data-export-code':
- serviceState.flows.export.codeValue = value;
- return;
- case 'data-delete-email':
- serviceState.flows.delete.emailValue = value;
- return;
- case 'data-delete-code':
- serviceState.flows.delete.codeValue = value;
- return;
- default:
- return;
- }
- }
-
installServicesController({
toggleServicePanel,
focusElement,
@@ -492,8 +403,10 @@ interface VerificationFlowDefinition {
submitRefund: function() {
void submitRefund();
},
- updateInputValue,
+ updateInputValue: function(inputKind, value) {
+ updateServiceInputValue(serviceState, inputKind, value);
+ },
updateDeleteConfirmation: function(checked) {
- serviceState.flows.delete.checkboxChecked = checked;
+ updateDeleteConfirmation(serviceState, checked);
},
});
diff --git a/internal/cloudcp/portal/frontend/src/state.ts b/internal/cloudcp/portal/frontend/src/state.ts
new file mode 100644
index 0000000000..621758b7e9
--- /dev/null
+++ b/internal/cloudcp/portal/frontend/src/state.ts
@@ -0,0 +1,142 @@
+import type {
+ PortalLoginState,
+ PortalServiceFlowID,
+ PortalServiceState,
+ RefundState,
+ ServiceStatus,
+ VerificationFlowState,
+} from './types';
+
+export function emptyStatus(): ServiceStatus {
+ return {
+ visible: false,
+ message: '',
+ error: false,
+ };
+}
+
+export function newVerificationFlowState(): VerificationFlowState {
+ return {
+ pendingEmail: '',
+ requesting: false,
+ confirming: false,
+ step2Visible: false,
+ status: emptyStatus(),
+ result: null,
+ emailValue: '',
+ codeValue: '',
+ checkboxChecked: false,
+ };
+}
+
+export function createPortalLoginState(): PortalLoginState {
+ return {
+ emailValue: '',
+ sending: false,
+ success: false,
+ error: '',
+ };
+}
+
+export function createPortalServiceState(): PortalServiceState {
+ return {
+ openPanelID: '',
+ flows: {
+ manage: newVerificationFlowState(),
+ retrieve: newVerificationFlowState(),
+ export: newVerificationFlowState(),
+ delete: newVerificationFlowState(),
+ },
+ refund: {
+ emailValue: '',
+ tokenValue: '',
+ submitting: false,
+ status: emptyStatus(),
+ },
+ };
+}
+
+export function syncLoginStateBootstrapEmail(loginState: PortalLoginState, email: string): void {
+ if (!loginState.emailValue) {
+ loginState.emailValue = email || '';
+ }
+}
+
+export function syncServiceStateBootstrapEmail(serviceState: PortalServiceState, email: string): void {
+ if (!serviceState.flows.manage.emailValue) serviceState.flows.manage.emailValue = email || '';
+ if (!serviceState.flows.retrieve.emailValue) serviceState.flows.retrieve.emailValue = email || '';
+ if (!serviceState.flows.export.emailValue) serviceState.flows.export.emailValue = email || '';
+ if (!serviceState.flows.delete.emailValue) serviceState.flows.delete.emailValue = email || '';
+ if (!serviceState.refund.emailValue) serviceState.refund.emailValue = email || '';
+}
+
+export function setFlowStatus(serviceState: PortalServiceState, flowID: PortalServiceFlowID, message: string, isError: boolean): void {
+ serviceState.flows[flowID].status = {
+ visible: true,
+ message,
+ error: !!isError,
+ };
+}
+
+export function clearFlowStatus(serviceState: PortalServiceState, flowID: PortalServiceFlowID): void {
+ serviceState.flows[flowID].status = emptyStatus();
+}
+
+export function setRefundStatus(serviceState: PortalServiceState, message: string, isError: boolean): void {
+ serviceState.refund.status = {
+ visible: true,
+ message,
+ error: !!isError,
+ };
+}
+
+export function toggleServicePanelState(serviceState: PortalServiceState, panelID: string): void {
+ serviceState.openPanelID = serviceState.openPanelID === panelID ? '' : panelID;
+}
+
+export function resetVerificationFlowState(serviceState: PortalServiceState, flowID: PortalServiceFlowID): void {
+ var previous = serviceState.flows[flowID];
+ serviceState.flows[flowID] = newVerificationFlowState();
+ serviceState.flows[flowID].emailValue = previous.emailValue;
+}
+
+export function updateServiceInputValue(serviceState: PortalServiceState, inputKind: string, value: string): void {
+ switch (inputKind) {
+ case 'manage-email':
+ serviceState.flows.manage.emailValue = value;
+ return;
+ case 'manage-code':
+ serviceState.flows.manage.codeValue = value;
+ return;
+ case 'retrieve-email':
+ serviceState.flows.retrieve.emailValue = value;
+ return;
+ case 'retrieve-code':
+ serviceState.flows.retrieve.codeValue = value;
+ return;
+ case 'refund-email':
+ serviceState.refund.emailValue = value;
+ return;
+ case 'refund-token':
+ serviceState.refund.tokenValue = value;
+ return;
+ case 'data-export-email':
+ serviceState.flows.export.emailValue = value;
+ return;
+ case 'data-export-code':
+ serviceState.flows.export.codeValue = value;
+ return;
+ case 'data-delete-email':
+ serviceState.flows.delete.emailValue = value;
+ return;
+ case 'data-delete-code':
+ serviceState.flows.delete.codeValue = value;
+ return;
+ default:
+ return;
+ }
+}
+
+export function updateDeleteConfirmation(serviceState: PortalServiceState, checked: boolean): void {
+ serviceState.flows.delete.checkboxChecked = checked;
+}
diff --git a/internal/cloudcp/portal/frontend/src/types.ts b/internal/cloudcp/portal/frontend/src/types.ts
index c03590547d..97792465c7 100644
--- a/internal/cloudcp/portal/frontend/src/types.ts
+++ b/internal/cloudcp/portal/frontend/src/types.ts
@@ -40,6 +40,8 @@ export interface PortalLoginState {
error: string;
}
+export type PortalServiceFlowID = 'manage' | 'retrieve' | 'export' | 'delete';
+
export interface ServiceStatus {
visible: boolean;
message: string;
@@ -64,3 +66,9 @@ export interface RefundState {
submitting: boolean;
status: ServiceStatus;
}
+
+export interface PortalServiceState {
+ openPanelID: string;
+ flows: Record<PortalServiceFlowID, VerificationFlowState>;
+ refund: RefundState;
+}