import { useState, useRef } from "react"; import { useTranslation } from "react-i18next"; import CommunityHub from "@/models/communityHub"; import showToast from "@/utils/toast"; import paths from "@/utils/paths"; import { X } from "@phosphor-icons/react"; import { Link } from "react-router-dom"; export default function SystemPrompts({ entity }) { const { t } = useTranslation(); const formRef = useRef(null); const [isSubmitting, setIsSubmitting] = useState(false); const [tags, setTags] = useState([]); const [tagInput, setTagInput] = useState(""); const [visibility, setVisibility] = useState("public"); const [isSuccess, setIsSuccess] = useState(false); const [itemId, setItemId] = useState(null); const handleSubmit = async (e) => { e.preventDefault(); e.stopPropagation(); setIsSubmitting(true); try { const form = new FormData(formRef.current); const data = { name: form.get("name"), description: form.get("description"), prompt: form.get("prompt"), tags: tags, visibility: visibility, }; const { success, error, itemId } = await CommunityHub.createSystemPrompt(data); if (!success) throw new Error(error); setItemId(itemId); setIsSuccess(true); } catch (error) { console.error("Failed to publish prompt:", error); showToast(`Failed to publish prompt: ${error.message}`, "error", { clear: true, }); } finally { setIsSubmitting(false); } }; const handleKeyDown = (e) => { if (e.key === "Enter" || e.key === ",") { e.preventDefault(); const value = tagInput.trim(); if (value.length > 20) return; if (value && !tags.includes(value)) { setTags((prevTags) => [...prevTags, value].slice(0, 5)); // Limit to 5 tags setTagInput(""); } } }; const removeTag = (tagToRemove) => { setTags(tags.filter((tag) => tag !== tagToRemove)); }; if (isSuccess) { return (
{t("community_hub.publish.system_prompt.success_description")}
{t("community_hub.publish.system_prompt.success_thank_you")}
{t("community_hub.publish.system_prompt.view_on_hub")}