55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
import { telemetry } from './app-telemetry.js';
|
|
|
|
const warned = new Set();
|
|
|
|
export const warnOnce = (code, message, details = {}) => {
|
|
const normalizedCode = String(code ?? '').trim() || 'UI_WARN';
|
|
const normalizedMessage = String(message ?? '').trim();
|
|
const key = `${normalizedCode}:${normalizedMessage}`;
|
|
if (warned.has(key)) {return;}
|
|
warned.add(key);
|
|
console.warn(`[${normalizedCode}] ${normalizedMessage}`, details);
|
|
|
|
const warnMeta = {
|
|
source: 'warn_once',
|
|
code: normalizedCode,
|
|
};
|
|
|
|
if (details && typeof details === 'object' && !Array.isArray(details)) {
|
|
if (typeof details.module === 'string' && details.module.trim() !== '') {
|
|
warnMeta.module = details.module.trim();
|
|
}
|
|
if (typeof details.component === 'string' && details.component.trim() !== '') {
|
|
warnMeta.component = details.component.trim();
|
|
}
|
|
if (typeof details.feature === 'string' && details.feature.trim() !== '') {
|
|
warnMeta.feature = details.feature.trim();
|
|
}
|
|
}
|
|
|
|
telemetry.capture('frontend.warn_once', {
|
|
severity: 'warning',
|
|
message: `[${normalizedCode}] ${normalizedMessage}`,
|
|
meta: warnMeta,
|
|
});
|
|
};
|
|
|
|
export const requireEl = (selector, details = {}) => {
|
|
const el = document.querySelector(selector);
|
|
if (!el) {
|
|
warnOnce('UI_EL_MISSING', `Missing element: ${selector}`, details);
|
|
return null;
|
|
}
|
|
return el;
|
|
};
|
|
|
|
export const requireAll = (selector, details = {}) => {
|
|
const list = Array.from(document.querySelectorAll(selector));
|
|
if (!list.length) {
|
|
warnOnce('UI_EL_MISSING', `Missing elements: ${selector}`, details);
|
|
}
|
|
return list;
|
|
};
|
|
|
|
export const optionalEl = (selector) => document.querySelector(selector);
|