import { __experimentalHStack as HStack, BaseControl, Button, Card, CardBody, CardHeader, SelectControl, ToggleControl, FormTokenField } from "@wordpress/components"; import { __ } from "@wordpress/i18n"; import { useMutation, useQuery } from "@tanstack/react-query"; import { getPlooginsSettingsQueryOptions } from "@/shared/site-settings/query-options"; import { useForm } from "@tanstack/react-form"; import { updatePlooginsSettingsMutationOptions } from "@/shared/site-settings/mutation-options"; import { getPluginAuthorsQueryOptions, getSimpleSearchPluginsQueryOptions } from "@/shared/ploogins-search/query-options"; import { useState } from "@wordpress/element"; const Settings = () => { const { data: settings, isPending } = useQuery(getPlooginsSettingsQueryOptions()) const { mutate: updateSettings, isPending: isUpdatingSettings } = useMutation(updatePlooginsSettingsMutationOptions()) // State for author input value const [authorInputValue, setAuthorInputValue] = useState('') const [excludeAuthorInputValue, setExcludeAuthorInputValue] = useState('') const [pluginInputValue, setPluginInputValue] = useState('') const [excludePluginInputValue, setExcludePluginInputValue] = useState('') const { data: pluginsData } = useQuery(getSimpleSearchPluginsQueryOptions(pluginInputValue)) const { data: excludePluginsData } = useQuery(getSimpleSearchPluginsQueryOptions(excludePluginInputValue)) // Query for plugin authors based on input const { data: authorsData } = useQuery(getPluginAuthorsQueryOptions(authorInputValue)) const { data: excludeAuthorsData } = useQuery(getPluginAuthorsQueryOptions(excludeAuthorInputValue)) const pluginSuggestions = pluginsData?.data?.map(plugin => plugin.slug) || [] const excludePluginSuggestions = excludePluginsData?.data?.map(plugin => plugin.slug) || [] // Extract author names from the API response for suggestions const authorSuggestions = authorsData?.data?.map(author => author.author) || [] const excludeAuthorSuggestions = excludeAuthorsData?.data?.map(author => author.author) || [] const form = useForm({ defaultValues: { max_results: settings?.max_results ?? 20, min_rating: settings?.min_rating ?? null, max_rating: settings?.max_rating ?? null, min_last_updated: settings?.min_last_updated ?? '', min_active_installs: settings?.min_active_installs ?? null, min_tested_version: settings?.min_tested_version ?? '', exclude_premium: settings?.exclude_premium ?? false, featured_authors: settings?.featured_authors ?? [], exclude_authors: settings?.exclude_authors ?? [], featured_plugins: settings?.featured_plugins ?? [], exclude_plugins: settings?.exclude_plugins ?? [], } as Settings, onSubmit: (values) => { updateSettings(values.value as Settings) } }) return (

{__('AI Search Settings', 'ploogins-ai-assistant')}

{__('Search Results', 'ploogins-ai-assistant')} ( field.handleChange(parseInt(value, 10))} options={[ { label: "10", value: "10" }, { label: "20", value: "20" }, { label: "30", value: "30" }, { label: "50", value: "50" }, ]} /> {field.state.meta.errors.length > 0 && ( {field.state.meta.errors.join(',')} )} )} /> ( field.handleChange(parseInt(value, 10))} options={[ { label: __('None', 'ploogins-ai-assistant'), value: "" }, { label: "⭐", value: "1" }, { label: "⭐⭐", value: "2" }, { label: "⭐⭐⭐", value: "3" }, { label: "⭐⭐⭐⭐", value: "4" }, { label: "⭐⭐⭐⭐⭐", value: "5" }, ]} /> {field.state.meta.errors.length > 0 && ( {field.state.meta.errors.join(',')} )} )} /> ( field.handleChange(parseInt(value, 10))} options={[ { label: __('None', 'ploogins-ai-assistant'), value: "" }, { label: "⭐", value: "1" }, { label: "⭐⭐", value: "2" }, { label: "⭐⭐⭐", value: "3" }, { label: "⭐⭐⭐⭐", value: "4" }, { label: "⭐⭐⭐⭐⭐", value: "5" }, ]} /> {field.state.meta.errors.length > 0 && ( {field.state.meta.errors.join(',')} )} )} /> ( field.handleChange(value)} options={[ { label: __('None', 'ploogins-ai-assistant'), value: "" }, { label: "1 month ago", value: "1 month ago" }, { label: "6 months ago", value: "6 months ago" }, { label: "1 year ago", value: "1 year ago" }, ]} /> {field.state.meta.errors.length > 0 && ( {field.state.meta.errors.join(',')} )} )} /> ( field.handleChange(parseInt(value, 10))} options={[ { label: __('None', 'ploogins-ai-assistant'), value: "" }, { label: "100", value: "100" }, { label: "1K", value: "1000" }, { label: "10K", value: "10000" }, { label: "100K", value: "100000" }, { label: "1M", value: "1000000" }, ]} /> {field.state.meta.errors.length > 0 && ( {field.state.meta.errors.join(',')} )} )} /> ( field.handleChange(value)} options={[ { label: __('None', 'ploogins-ai-assistant'), value: "" }, { label: "Latest Major Version", value: "latest major" }, { label: "Latest Version", value: "latest" }, ]} /> {field.state.meta.errors.length > 0 && ( {field.state.meta.errors.join(',')} )} )} /> {__('Premium Plugins', 'ploogins-ai-assistant')} ( field.handleChange(value)} /> {field.state.meta.errors.length > 0 && ( {field.state.meta.errors.join(',')} )} )} /> {__('Authors', 'ploogins-ai-assistant')} ( field.handleChange(value.filter(item => typeof item === 'string') as string[])} label={''} onInputChange={(value) => { setAuthorInputValue(value) }} suggestions={authorSuggestions} /> {field.state.meta.errors.length > 0 && ( {field.state.meta.errors.join(',')} )} )} /> ( field.handleChange(value.filter(item => typeof item === 'string') as string[])} label={''} onInputChange={(value) => { setExcludeAuthorInputValue(value) }} suggestions={excludeAuthorSuggestions} /> {field.state.meta.errors.length > 0 && ( {field.state.meta.errors.join(',')} )} )} /> {__('Plugins', 'ploogins-ai-assistant')} ( field.handleChange(value.filter(item => typeof item === 'string') as string[])} label={''} onInputChange={(value) => { setPluginInputValue(value) }} suggestions={pluginSuggestions} /> {field.state.meta.errors.length > 0 && ( {field.state.meta.errors.join(',')} )} )} /> ( field.handleChange(value.filter(item => typeof item === 'string') as string[])} label={''} onInputChange={(value) => { setExcludePluginInputValue(value) }} suggestions={excludePluginSuggestions} /> {field.state.meta.errors.length > 0 && ( {field.state.meta.errors.join(',')} )} )} />
) } export default Settings;