rob-revisited/ide/ui/js/schema-model.js

41 lines
1.8 KiB
JavaScript

// The database schema banner's model (pure: no DOM, tested by schema-model.test.mjs): what a check result (PHP SchemaCheck) shows.
// result: {state: 'ok'|'problems'|'checking'|'error'|'skipped', problems: [{code, message, params}], fixes: [{id, label, primary}],
// plan: [{id, label}], production, connection, statements}
/** the banner: null when nothing is wrong (or it was dismissed) */
export function bannerOf(result, dismissed = '') {
if (!result || result.state !== 'problems' || !result.problems?.length) return null;
const key = signature(result);
if (key === dismissed) return null;
const hard = result.problems.some((p) => ['no_database', 'entity_without_table', 'observed_table'].includes(p.code));
return {
key,
level: hard ? 'error' : 'warning',
lines: result.problems.map((p) => p.message),
fixes: result.fixes || [],
production: !!result.production,
};
}
export function signature(result) {
return (result?.problems || []).map((p) => p.message).join('|');
}
/** the badge of the Run widget / status bar: {kind: 'ok'|'problem'|'checking'|'', count} */
export function badgeOf(result) {
if (!result) return { kind: '', count: 0 };
if (result.state === 'checking') return { kind: 'checking', count: 0 };
if (result.state === 'problems' && result.problems?.length) return { kind: 'problem', count: result.problems.length };
if (result.state === 'ok') return { kind: 'ok', count: 0 };
return { kind: '', count: 0 };
}
/** the steps of "Fix everything" as its confirmation reads them */
export function planText(plan) {
return (plan || []).map((s, i) => `${i + 1}. ${s.label}`).join('\n');
}
/** whether a fix needs the connection's name typed (a production database) */
export function needsName(fix, result) {
return !!result?.production && fix !== 'openDoctrine' && fix !== 'recheck';
}