All files / src/utils warning-remedies.ts

100% Statements 10/10
100% Branches 4/4
100% Functions 4/4
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40                                  3x               6x 2x 2x 2x 2x             10x 6x    
import type { App } from "obsidian";
import { MESSAGES } from "../locales/en";
import type { RebuildPlugin } from "./reindex";
import { ConfirmModal } from "../views/confirm-modal";
 
/** A server degradation the plugin knows how to fix: what to tell the user, and what the button does. */
export interface WarningRemedy {
    text: string;
    action: () => void;
}
 
/** The slice of the plugin a warning-remedy action needs: the app for dialogs and the rebuild entry point. */
export interface WarningRemedyPlugin extends RebuildPlugin {
    app: App;
}
 
/** Codes the plugin can fix itself; every other code falls back to the server's remedy text. */
const KNOWN_CODES = new Set([
    "fts_unavailable",
    "embedding_prefix_mismatch",
    "index_embedding_mismatch",
    "stale_index",
]);
 
function rebuildAction(plugin: WarningRemedyPlugin): () => void {
    return () => {
        const modal = new ConfirmModal(plugin.app, MESSAGES.CONFIRM_SYNC_REBUILD);
        modal.open();
        void modal.result.then((confirmed) => {
            if (confirmed) void plugin.triggerSync({ forceRebuild: true });
        });
    };
}
 
/** Map a warning code to a plugin-native remedy, or null when the plugin does not know the code. */
export function remedyForWarning(code: string, plugin: WarningRemedyPlugin): WarningRemedy | null {
    if (!KNOWN_CODES.has(code)) return null;
    return { text: MESSAGES.WARNING_REMEDY_REBUILD, action: rebuildAction(plugin) };
}