Skip to content

Commit

Permalink
Hide OperatorInfo on invalid signed message, reduce indentation and a…
Browse files Browse the repository at this point in the history
…dd comments to OperatorInfoAlert
  • Loading branch information
7or2ga committed Oct 27, 2024
1 parent 7928661 commit 684a2a4
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions src/components/OperatorInfoAlert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function OperatorInfoAlert({
onError: onError,
});

if (!opInfo) {
if (!opInfo || !isValid) {
return null;
}

Expand All @@ -46,41 +46,50 @@ function getQuotaText({ operatorInfo }: { operatorInfo: OperatorInfo }) {
return null;
}

// See OperatorInfo and QuotaSettings types for full explanation
// count = total # times per sliding 'window' that access can be requested
// window = sliding 'window' duration measured in seconds
// authValidityWindow = credential validity lifetime measured in seconds
// 86400 seconds = 1 day
const windowInDays = operatorInfo.quotaSettings.window / 86400;
const used = operatorInfo.credentialEvents.length;
const remaining =
operatorInfo.quotaSettings.count - operatorInfo.credentialEvents.length;

// tz is in locale string format, e.g. 'America/New_York'
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
var usageMsg = `You have not used the Rescue Node in the past ${windowInDays} days.`;
var activeCredMsg = `You do not currently have an active credential.`;
var remainingMsg = `You have ${remaining} usages remaining.`;

if (used > 0) {
// Multiplying by 1000 converts timestamp to milliseconds for working with Date()
const nextTimestamp =
operatorInfo.credentialEvents[operatorInfo.credentialEvents.length - 1] *
1000 +
operatorInfo.quotaSettings.window * 1000 +
1000;
const nextDate = new Date(nextTimestamp).toLocaleString("en-US", {
timeZone: tz,
});
const expiresTimestamp =
operatorInfo.credentialEvents[0] * 1000 +
operatorInfo.quotaSettings.authValidityWindow * 1000;
const expiresDate = new Date(expiresTimestamp).toLocaleString("en-US", {
timeZone: tz,
});
if (used == 0) {
return `${usageMsg}
${activeCredMsg}
${remainingMsg}`;
}

// tz is in locale string format, e.g. 'America/New_York'
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;

// Multiplying by 1000 converts timestamp to milliseconds for working with Date()
const nextTimestamp =
operatorInfo.credentialEvents[operatorInfo.credentialEvents.length - 1] *
1000 +
operatorInfo.quotaSettings.window * 1000 +
1000;
const nextDate = new Date(nextTimestamp).toLocaleString("en-US", {
timeZone: tz,
});
const expiresTimestamp =
operatorInfo.credentialEvents[0] * 1000 +
operatorInfo.quotaSettings.authValidityWindow * 1000;
const expiresDate = new Date(expiresTimestamp).toLocaleString("en-US", {
timeZone: tz,
});

usageMsg = `You have used the Rescue Node ${used} times in the past ${windowInDays} days.`;
remainingMsg = `You have ${remaining} usages remaining. Your next increase will be at ${nextDate} (${tz}).`;
usageMsg = `You have used the Rescue Node ${used} times in the past ${windowInDays} days.`;
remainingMsg = `You have ${remaining} usages remaining. Your next increase will be at ${nextDate} (${tz}).`;

if (expiresTimestamp > Date.now()) {
activeCredMsg = `You currently have an active credential, which will expire at ${expiresDate} (${tz}).`;
}
if (expiresTimestamp > Date.now()) {
activeCredMsg = `You currently have an active credential, which will expire at ${expiresDate} (${tz}).`;
}

return `${usageMsg}
Expand Down

0 comments on commit 684a2a4

Please sign in to comment.