Files
new-api/web/src/helpers/quota.js
T
CaIon 040e8c1da8 feat: replace quota input with amount-first UI and atomic quota adjustment
- Refactor token, redemption, and user quota inputs to prioritize monetary
  amount entry, with raw quota input collapsed by default
- Add atomic quota adjustment modal for users with add/subtract/override modes,
  bypassing batch update queue for immediate DB consistency
- Make user quota fields readonly in edit form; all modifications go through
  the dedicated adjust-quota modal via POST /api/user/manage
- Add DecreaseUserQuota `db` parameter for direct DB writes, matching
  IncreaseUserQuota behavior
- Support negative quota display in amount conversion helpers
- Add i18n keys for all new UI strings across all locales
2026-04-09 22:44:53 +08:00

48 lines
1.7 KiB
JavaScript
Vendored

/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { getCurrencyConfig } from './render';
export const getQuotaPerUnit = () => {
const raw = parseFloat(localStorage.getItem('quota_per_unit') || '1');
return Number.isFinite(raw) && raw > 0 ? raw : 1;
};
export const quotaToDisplayAmount = (quota) => {
const q = Number(quota || 0);
if (!Number.isFinite(q) || q === 0) return 0;
const sign = Math.sign(q);
const abs = Math.abs(q);
const { type, rate } = getCurrencyConfig();
if (type === 'TOKENS') return q;
const usd = abs / getQuotaPerUnit();
if (type === 'USD') return sign * usd;
return sign * usd * (rate || 1);
};
export const displayAmountToQuota = (amount) => {
const val = Number(amount || 0);
if (!Number.isFinite(val) || val === 0) return 0;
const sign = Math.sign(val);
const abs = Math.abs(val);
const { type, rate } = getCurrencyConfig();
if (type === 'TOKENS') return Math.round(val);
const usd = type === 'USD' ? abs : abs / (rate || 1);
return sign * Math.round(usd * getQuotaPerUnit());
};