2024-12-21 18:51:28 +03:00
|
|
|
import { prop } from "@zos/ui";
|
2024-11-19 20:30:24 +03:00
|
|
|
import { TOTP } from "../../../lib/totp-quickjs";
|
2025-02-26 00:33:40 +03:00
|
|
|
import {
|
|
|
|
RenderExpireBar,
|
|
|
|
RenderOTPValue,
|
|
|
|
RenderTOTPContainer,
|
|
|
|
} from "../totpRenderer";
|
2024-11-19 20:30:24 +03:00
|
|
|
|
|
|
|
/**
|
2025-02-26 00:33:40 +03:00
|
|
|
*
|
|
|
|
* @param {Array<TOTP>} buffer
|
2024-11-19 20:30:24 +03:00
|
|
|
*/
|
|
|
|
export function initLoop(buffer) {
|
2025-02-26 00:33:40 +03:00
|
|
|
renderContainers(buffer);
|
|
|
|
renderTOTPs(buffer);
|
2024-11-19 20:30:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderContainers(buffer) {
|
|
|
|
for (let i = 0; i < buffer.length; i++) {
|
2025-02-26 00:33:40 +03:00
|
|
|
RenderTOTPContainer(i, buffer[i].issuer, buffer[i].client);
|
2024-11-19 20:30:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-26 01:58:11 +03:00
|
|
|
const renderData = [];
|
2024-11-19 20:30:24 +03:00
|
|
|
function renderTOTPs(buffer) {
|
|
|
|
for (let i = 0; i < buffer.length; i++) {
|
2025-02-26 00:33:40 +03:00
|
|
|
let otpData = TOTP.copy(buffer[i]).getOTP();
|
2024-11-19 20:56:36 +03:00
|
|
|
renderData[i] = {
|
|
|
|
OTP: RenderOTPValue(i, otpData.otp),
|
2025-02-26 00:33:40 +03:00
|
|
|
expireBar: RenderExpireBar(
|
|
|
|
i,
|
|
|
|
otpData.createdTime,
|
|
|
|
buffer[i].fetchTime
|
|
|
|
),
|
|
|
|
};
|
2024-11-19 20:30:24 +03:00
|
|
|
setInterval(() => {
|
2025-02-26 00:33:40 +03:00
|
|
|
const expireDif = Math.abs(
|
|
|
|
(Date.now() - otpData.createdTime) /
|
|
|
|
1000 /
|
|
|
|
buffer[i].fetchTime -
|
|
|
|
1
|
|
|
|
);
|
2024-11-19 20:56:36 +03:00
|
|
|
|
|
|
|
renderData[i].expireBar.setProperty(prop.MORE, {
|
2025-02-26 00:33:40 +03:00
|
|
|
end_angle: expireDif * 360 - 90,
|
2024-11-19 20:56:36 +03:00
|
|
|
color: expireDif > 0.25 ? 0x1ca9c9 : 0xfa0404,
|
2025-02-26 00:33:40 +03:00
|
|
|
});
|
|
|
|
|
2024-11-19 20:56:36 +03:00
|
|
|
if (otpData.expireTime < Date.now()) {
|
2025-02-26 00:33:40 +03:00
|
|
|
otpData = TOTP.copy(buffer[i]).getOTP();
|
2024-11-19 20:56:36 +03:00
|
|
|
renderData[i].OTP.setProperty(prop.MORE, {
|
2025-02-26 00:33:40 +03:00
|
|
|
text: otpData.otp,
|
|
|
|
});
|
2024-11-19 20:30:24 +03:00
|
|
|
}
|
2025-02-26 00:33:40 +03:00
|
|
|
}, 50);
|
2024-11-19 20:30:24 +03:00
|
|
|
}
|
2025-02-26 00:33:40 +03:00
|
|
|
}
|