refactor: slice web app wip

This commit is contained in:
3252a8
2026-05-11 22:32:23 +03:00
parent 116b0a5c08
commit a575cb057b
9 changed files with 177 additions and 427 deletions
@@ -0,0 +1,4 @@
/** Drop cached topup / change-tariff option payloads so the next open refetches from /api. */
export function invalidateWebappTariffOptionCaches(billingStore) {
billingStore.update((s) => ({ ...s, topupOptions: null, deviceTopupOptions: null, changeOptions: null }));
}
@@ -0,0 +1,24 @@
/**
* Pure helpers for HWID / device limits UI (used by DevicesScreen).
* @param {Record<string, unknown>} devicesData API payload from /api/devices
* @param {(key: string, vars?: Record<string, unknown>, fallback?: string) => string} t i18n function
* @param {unknown} [maxDevicesOverride] optional max_devices override (defaults to devicesData.max_devices)
*/
export function devicesLimitLabel(devicesData, t, maxDevicesOverride) {
const value = maxDevicesOverride !== undefined ? maxDevicesOverride : devicesData?.max_devices;
const numeric = Number(value ?? 0);
if (!Number.isFinite(numeric) || numeric <= 0) return t("wa_devices_unlimited");
return String(Math.trunc(numeric));
}
export function devicesCountLabel(devicesData, t) {
const current = Number(devicesData?.current_devices ?? devicesData?.devices?.length ?? 0);
return t("wa_devices_count", { current, max: devicesLimitLabel(devicesData, t) });
}
export function devicesPercent(devicesData) {
const current = Number(devicesData?.current_devices ?? devicesData?.devices?.length ?? 0);
const max = Number(devicesData?.max_devices || 0);
if (!max || max <= 0) return 100;
return Math.max(0, Math.min(100, Math.round((current / max) * 100)));
}
@@ -211,5 +211,6 @@ export function createAccountStore({ api, publicApi, setToken, loadData, t, show
linkTelegramAccount,
updateAccountLanguage,
logout,
clearLinkEmailResendTimer: clearCooldownTimer,
};
}
@@ -269,6 +269,7 @@ export function createAuthStore({
verifyEmailCode,
openTelegramLogin,
clearCooldownTimer,
setAuthStatus
stopTelegramLoginWatchdog,
setAuthStatus,
};
}
@@ -63,28 +63,6 @@ export function createDevicesStore({ api, t, showToast }) {
}
}
function devicesLimitLabel() {
const s = get(state);
const value = s.devicesData?.max_devices;
const numeric = Number(value ?? 0);
if (!Number.isFinite(numeric) || numeric <= 0) return t("wa_devices_unlimited");
return String(Math.trunc(numeric));
}
function devicesCountLabel() {
const s = get(state);
const current = Number(s.devicesData?.current_devices ?? s.devicesData?.devices?.length ?? 0);
return t("wa_devices_count", { current, max: devicesLimitLabel() });
}
function devicesPercent() {
const s = get(state);
const current = Number(s.devicesData?.current_devices ?? s.devicesData?.devices?.length ?? 0);
const max = Number(s.devicesData?.max_devices || 0);
if (!max || max <= 0) return 100;
return Math.max(0, Math.min(100, Math.round((current / max) * 100)));
}
return {
subscribe: state.subscribe,
set: state.set,
@@ -93,8 +71,5 @@ export function createDevicesStore({ api, t, showToast }) {
openDeviceDisconnectDialog,
closeDeviceDisconnectDialog,
disconnectDevice,
devicesLimitLabel,
devicesCountLabel,
devicesPercent,
};
}
@@ -0,0 +1,87 @@
import {
readMagicLoginToken,
readTelegramAuthStatus,
readTelegramLoginWidgetAuthData,
clearAuthQuery,
} from "./authHelpers.js";
import { TELEGRAM_SDK_BOOT_TIMEOUT_MS } from "./constants.js";
/**
* Initial auth / session bootstrap for the subscription webapp (non-preview).
* Keeps side effects in App (mode, tg, token) via injected callbacks.
*/
export async function runWebappBoot({
MOCK,
setMode,
hasTelegramLaunchParams,
loadTelegramSdk,
prepareTelegramMiniApp,
loadData,
showLogin,
clearToken,
clearManualLogoutFlag,
isManuallyLoggedOut,
finalizeMagicLogin,
finalizeTelegramAuth,
setAuthStatus,
t,
getInitDataForBoot,
getToken,
getCsrfToken,
}) {
setMode("loading");
if (hasTelegramLaunchParams()) await loadTelegramSdk(TELEGRAM_SDK_BOOT_TIMEOUT_MS);
prepareTelegramMiniApp();
if (MOCK) {
await loadData();
return;
}
const magicToken = readMagicLoginToken();
if (magicToken && (await finalizeMagicLogin(magicToken))) return;
const telegramAuthStatus = readTelegramAuthStatus();
if (telegramAuthStatus === "success") {
clearManualLogoutFlag();
clearAuthQuery();
try {
await loadData();
return;
} catch {
clearToken();
}
} else if (telegramAuthStatus) {
clearAuthQuery();
setAuthStatus(
telegramAuthStatus === "cancelled" ? t("wa_auth_telegram_cancelled") : t("wa_auth_telegram_not_confirmed"),
true,
);
}
if (isManuallyLoggedOut()) {
showLogin();
return;
}
const widgetAuthData = readTelegramLoginWidgetAuthData();
if (widgetAuthData && (await finalizeTelegramAuth(widgetAuthData, "auth_data"))) return;
const initData = getInitDataForBoot();
if (initData) {
try {
if (await finalizeTelegramAuth(initData, "init_data")) return;
} catch {}
}
if (getToken() || getCsrfToken()) {
try {
await loadData();
return;
} catch {
clearToken();
}
}
showLogin();
}