| 1 |
/** |
| 2 |
* Shared "did the active embedding model change?" detection, used by both the |
| 3 |
* inline re-embed notice (ReembedNotice) and the confirm-on-save button |
| 4 |
* (ReembedConfirmButton). |
| 5 |
* |
| 6 |
* The active embedding model is platform-dependent, so a change means either the |
| 7 |
* platform switched, or the embedding-model field for the current platform |
| 8 |
* changed. This mirrors the server's re-embed trigger in |
| 9 |
* REST/Settings.php::embed_aichatbot() ($prev_active_embed !== $new_active_embed). |
| 10 |
*/ |
| 11 |
|
| 12 |
// Pristine copy of the persisted settings, snapshotted at module load — BEFORE |
| 13 |
// the settings app mutates the global `betterdocsAdminSettings.values` in place |
| 14 |
// (it reassigns `.values = builder.values` on every change). This is the client |
| 15 |
// baseline, equivalent to the server's $prev_active_embed read from the DB. The |
| 16 |
// framework localizes only `values`, not a savedValues baseline, so we capture |
| 17 |
// one here. |
| 18 |
export const ORIGINAL_VALUES = |
| 19 |
typeof window !== "undefined" && |
| 20 |
window.betterdocsAdminSettings && |
| 21 |
window.betterdocsAdminSettings.values |
| 22 |
? { ...window.betterdocsAdminSettings.values } |
| 23 |
: {}; |
| 24 |
|
| 25 |
const activeModel = ( cfg, platform, source ) => { |
| 26 |
const field = cfg && cfg.model_field_map && cfg.model_field_map[ platform ]; |
| 27 |
return field ? source && source[ field ] : undefined; |
| 28 |
}; |
| 29 |
|
| 30 |
// True when the AI platform differs from the persisted baseline. |
| 31 |
export const platformChanged = ( cfg, values ) => { |
| 32 |
if ( ! cfg || ! cfg.platform_field ) { |
| 33 |
return false; |
| 34 |
} |
| 35 |
const savedPlatform = ORIGINAL_VALUES[ cfg.platform_field ]; |
| 36 |
// No persisted baseline (fresh install / nothing embedded yet) → nothing to |
| 37 |
// re-embed, so don't warn. |
| 38 |
if ( savedPlatform === undefined ) { |
| 39 |
return false; |
| 40 |
} |
| 41 |
return savedPlatform !== ( values && values[ cfg.platform_field ] ); |
| 42 |
}; |
| 43 |
|
| 44 |
// True when the active embedding model differs from the persisted baseline. With |
| 45 |
// the platform unchanged this compares the same platform's model field; after a |
| 46 |
// platform switch the active model field differs, so this is also true. |
| 47 |
const embedModelChanged = ( cfg, values ) => { |
| 48 |
if ( ! cfg || ! cfg.platform_field ) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
const savedPlatform = ORIGINAL_VALUES[ cfg.platform_field ]; |
| 52 |
if ( savedPlatform === undefined ) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
const nowPlatform = values && values[ cfg.platform_field ]; |
| 56 |
return ( |
| 57 |
activeModel( cfg, savedPlatform, ORIGINAL_VALUES ) !== |
| 58 |
activeModel( cfg, nowPlatform, values ) |
| 59 |
); |
| 60 |
}; |
| 61 |
|
| 62 |
// Any re-embed-triggering change — used by the Save-time confirm dialog. |
| 63 |
export const reembedNeeded = ( cfg, values ) => |
| 64 |
platformChanged( cfg, values ) || embedModelChanged( cfg, values ); |
| 65 |
|
| 66 |
// Embedding model changed while the platform stayed the same — used by the |
| 67 |
// embed-model field notice. (When the platform changes, the platform notice |
| 68 |
// covers it, so we suppress the embed notice.) |
| 69 |
export const embedModelChangedOnly = ( cfg, values ) => |
| 70 |
! platformChanged( cfg, values ) && embedModelChanged( cfg, values ); |
| 71 |
|