import { toast } from 'react-toastify'; import ReactModal from 'react-modal'; import { useState, useEffect } from 'react'; import { workbenchStore } from '~/lib/stores/workbench'; import { submitProblem, BoltProblemStatus } from '~/lib/replay/Problems'; import type { BoltProblemInput } from '~/lib/replay/Problems'; import { getRepositoryContents } from '~/lib/replay/Repository'; import { shouldUseSupabase, getCurrentUser } from '~/lib/supabase/client'; import { authModalStore } from '~/lib/stores/authModal'; import { authStatusStore } from '~/lib/stores/auth'; import { useStore } from '@nanostores/react'; ReactModal.setAppElement('#root'); // Component for saving the current chat as a new problem. export function SaveProblem() { const [isModalOpen, setIsModalOpen] = useState(false); const [formData, setFormData] = useState({ title: '', description: '', username: '', }); const [problemId, setProblemId] = useState(null); const isLoggedIn = useStore(authStatusStore.isLoggedIn); const username = useStore(authStatusStore.username); // Update the username from the store when component mounts useEffect(() => { if (username) { setFormData((prev) => ({ ...prev, username })); } }, [username]); const handleSaveProblem = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); setIsModalOpen(true); setProblemId(null); }; const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData((prev) => ({ ...prev, [name]: value, })); // Update username in the store if it's the username field if (name === 'username') { authStatusStore.updateUsername(value); } }; const handleSubmitProblem = async () => { if (!formData.title) { toast.error('Please fill in title field'); return; } if (!shouldUseSupabase() && !formData.username) { toast.error('Please enter a username'); return; } toast.info('Submitting problem...'); const repositoryId = workbenchStore.repositoryId.get(); if (!repositoryId) { toast.error('No repository ID found'); return; } const repositoryContents = await getRepositoryContents(repositoryId); const problem: BoltProblemInput = { version: 2, title: formData.title, description: formData.description, username: shouldUseSupabase() ? (undefined as any) : formData.username, user_id: shouldUseSupabase() ? (await getCurrentUser())?.id || '' : undefined, repositoryContents, status: BoltProblemStatus.Pending, }; const problemId = await submitProblem(problem); if (problemId) { setProblemId(problemId); localStorage.setItem('loadedProblemId', problemId); } }; return ( <> setIsModalOpen(false)} shouldCloseOnOverlayClick={true} shouldCloseOnEsc={true} style={{ overlay: { backgroundColor: 'rgba(0, 0, 0, 0.5)', zIndex: 1000, }, content: { top: '50%', left: '50%', right: 'auto', bottom: 'auto', marginRight: '-50%', transform: 'translate(-50%, -50%)', backgroundColor: 'white', padding: '20px', borderRadius: '8px', maxWidth: '500px', width: '100%', }, }} > {shouldUseSupabase() && !isLoggedIn && (
Please log in to save a problem
)} {(!shouldUseSupabase() || isLoggedIn) && problemId && ( <>
Problem Submitted: {problemId}
)} {(!shouldUseSupabase() || isLoggedIn) && !problemId && ( <>
Save prompts as new problems when AI results are unsatisfactory. Problems are publicly visible and are used to improve AI performance.
{!shouldUseSupabase() && ( <>
Username:
)}
Title:
Description:
)}
); }