import React, { useEffect, useState } from "react"; import { FieldCaption, useNavigationController, useSnackbarController } from "@firecms/core"; import { BooleanSwitchWithLabel, Button, MultiSelect, MultiSelectItem, Paper, TextField, Typography } from "@firecms/ui"; import { useProjectConfig } from "../../hooks"; import { TypesenseSearchConfig } from "../../hooks/useBuildProjectConfig"; export function TypesenseSettingsView() { const projectConfig = useProjectConfig(); const navigationController = useNavigationController(); const snackbarController = useSnackbarController(); const [config, setConfig] = useState(projectConfig.typesenseSearchConfig ?? { enabled: false, region: "us-central1", extensionInstanceId: "typesense-search", collections: [] }); const [loading, setLoading] = useState(false); useEffect(() => { if (projectConfig.typesenseSearchConfig) { setConfig(projectConfig.typesenseSearchConfig); } }, [projectConfig.typesenseSearchConfig]); const handleChange = (key: keyof TypesenseSearchConfig, value: any) => { setConfig({ ...config, [key]: value }); }; const handleEnableChange = (enabled: boolean) => { const newConfig = { ...config, enabled }; setConfig(newConfig); // Update local UI state // Persist enabled state immediately projectConfig.updateTypesenseSearchConfig(newConfig).catch((e: any) => { console.error(e); snackbarController.open({ type: "error", message: "Error updating Typesense status: " + e.message }); }); }; const handleSave = async () => { setLoading(true); try { await projectConfig.updateTypesenseSearchConfig(config); snackbarController.open({ type: "success", message: "Typesense configuration saved" }); } catch (e: any) { console.error(e); snackbarController.open({ type: "error", message: "Error saving Typesense configuration: " + e.message }); } finally { setLoading(false); } }; const collections = navigationController.collections ?? []; const saveDisabled = config.enabled && !config.region; return (
Enable Typesense search integration. You need to have the FireCMS Typesense extension installed in your Firebase project. {config.enabled && ( Typesense Configuration handleChange("region", e.target.value)} placeholder={"europe-west3"} size={"small"} error={!config.region} /> {!config.region && ( Region is required )} handleChange("extensionInstanceId", e.target.value)} placeholder={"typesense-search"} size={"small"} /> The region where your Typesense instance is located (e.g. europe-west3). And the instance ID of the installed extension (default: typesense-search).
Indexed Collections handleChange("collections", v)} label="Select collections to index" placeholder={"All collections (Default)"} size={"small"} > {collections.map(col => ( {col.name} ({col.path}) ))} Select the collections that you want to be searchable. If you leave this empty, all collections will be searchable by default.
)}
); }