PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.5
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.5
4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 All 203 releases
betterdocs / react-src / admin / settings / fields / ReembedConfirmButton.js

ReembedConfirmButton.js in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.9.5, at react-src/admin/settings/fields/ReembedConfirmButton.js

163 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import React, { useState } from "react";
2 import { __ } from "@wordpress/i18n";
3 import classNames from "classnames";
4 import Swal from "sweetalert2";
5 import {
6 hitAAJX,
7 isObject,
8 useBuilderContext,
9 validFieldProps,
10 withLabel,
11 } from "quickbuilder";
12 import { reembedNeeded } from "./reembedChange";
13
14 /**
15 * Submit button that warns before a destructive re-embed.
16 *
17 * Behaves exactly like the standard QuickBuilder button (same loading/saved
18 * text states, toast, reload, error handling), EXCEPT: when the field's
19 * `reembed_confirm` config is present and the *active* embedding model changed,
20 * it shows a confirmation dialog before firing the request. The user must
21 * confirm; cancelling aborts the save entirely.
22 *
23 * The active embedding model is platform-dependent, mirroring the server's
24 * re-embed trigger in REST/Settings.php::embed_aichatbot()
25 * ($prev_active_embed !== $new_active_embed): a change to the embedding-model
26 * field for the current platform, OR a switch of the platform itself, both
27 * mean every doc/product gets re-embedded.
28 *
29 * PHP field config:
30 * 'type' => 'reembed_confirm_button',
31 * 'reembed_confirm' => array(
32 * 'platform_field' => 'ai_chatbot_chat_provider',
33 * 'model_field_map' => array(
34 * 'openai' => 'ai_chatbot_embed_model',
35 * 'gemini' => 'ai_chatbot_embed_model_gemini',
36 * ),
37 * 'title' => '...',
38 * 'message' => '...',
39 * 'confirm_text' => '...',
40 * 'cancel_text' => '...',
41 * ),
42 */
43 const ReembedConfirmButton = ( props ) => {
44 const context = useBuilderContext();
45 const [ isLoading, setIsLoading ] = useState( false );
46
47 const validProps = validFieldProps( props, [
48 "is_pro",
49 "visible",
50 "disable",
51 "parentIndex",
52 "context",
53 "onBlur",
54 "value",
55 "ajax",
56 "text",
57 "reembed_confirm",
58 ] );
59
60 // Faithful copy of QuickBuilder Button's submit flow.
61 const runRequest = () => {
62 if ( ! props?.ajax ) {
63 return;
64 }
65 setIsLoading( true );
66 hitAAJX( props.ajax, context )
67 .then( ( res ) => {
68 setIsLoading( false );
69 if ( res?.status == "error" ) {
70 throw new Error( res?.message );
71 }
72 props.onChange( {
73 target: { type: "button", name: props.name, value: true },
74 } );
75 if ( ! props.ajax?.hideSwal ) {
76 const type = props.ajax?.swal?.icon || "success";
77 const message = props.ajax?.swal?.text || "Complete";
78 context.alerts.toast( type, message, {
79 autoClose: props.ajax?.swal?.autoClose,
80 } );
81 }
82 if ( props.ajax?.reload ) {
83 if (
84 typeof props.ajax.reload === "boolean" &&
85 props.ajax.reload
86 ) {
87 setTimeout( () => window.location.reload(), 1000 );
88 } else if ( typeof props.ajax.reload === "string" ) {
89 window.location.href = props.ajax.reload;
90 }
91 }
92 } )
93 .catch( ( err ) => {
94 console.error( "Error In ReembedConfirmButton", props.name, err );
95 setIsLoading( false );
96 props.onChange( {
97 target: { type: "button", name: props.name, value: false },
98 } );
99 if ( ! props.ajax?.hideSwal ) {
100 context.alerts.toast(
101 "error",
102 err?.message || __( "Something went wrong.", "betterdocs" )
103 );
104 }
105 } );
106 };
107
108 const handleClick = () => {
109 if ( ! props?.ajax ) {
110 return;
111 }
112 const cfg = props.reembed_confirm;
113 if ( cfg && reembedNeeded( cfg, context.values ) ) {
114 Swal.fire( {
115 title:
116 cfg.title || __( "Change AI embedding model?", "betterdocs" ),
117 text:
118 cfg.message ||
119 __(
120 "This re-embeds all of your content on the AI server and may take a while.",
121 "betterdocs"
122 ),
123 icon: "warning",
124 showCancelButton: true,
125 confirmButtonText:
126 cfg.confirm_text || __( "Yes, re-embed", "betterdocs" ),
127 cancelButtonText: cfg.cancel_text || __( "Cancel", "betterdocs" ),
128 customClass: { popup: "betterdocs-bulk-action-popup" },
129 } ).then( ( result ) => {
130 if ( result?.isConfirmed ) {
131 runRequest();
132 }
133 } );
134 return;
135 }
136 runRequest();
137 };
138
139 return (
140 <button
141 { ...validProps }
142 type="button"
143 name={ props.name }
144 disabled={ isLoading }
145 onClick={ handleClick }
146 className={ classNames(
147 "wprf-control wprf-button wprf-btn",
148 props?.classes
149 ) }
150 >
151 { isObject( props?.text ) && props?.ajax
152 ? isLoading
153 ? props?.text?.loading
154 : props.value
155 ? props?.text?.saved
156 : props?.text?.normal
157 : props?.text }
158 </button>
159 );
160 };
161
162 export default withLabel( ReembedConfirmButton );
163