You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

706 lines
23 KiB

// Solutions.tsx
import React, { useState, useEffect, useRef } from "react"
import { useQuery, useQueryClient } from "@tanstack/react-query"
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"
import { dracula } from "react-syntax-highlighter/dist/esm/styles/prism"
import ScreenshotQueue from "../components/Queue/ScreenshotQueue"
import { ProblemStatementData } from "../types/solutions"
import SolutionCommands from "../components/Solutions/SolutionCommands"
import Debug from "./Debug"
import { useToast } from "../contexts/toast"
import { COMMAND_KEY } from "../utils/platform"
export const ContentSection = ({
title,
content,
isLoading
}: {
title: string
content: React.ReactNode
isLoading: boolean
}) => (
<div className="space-y-2">
<h2 className="text-[13px] font-medium text-white tracking-wide">
{title}
</h2>
{isLoading ? (
<div className="mt-4 flex">
<p className="text-xs bg-gradient-to-r from-gray-300 via-gray-100 to-gray-300 bg-clip-text text-transparent animate-pulse">
Extracting problem statement...
</p>
</div>
) : (
<div className="text-[13px] leading-[1.4] text-gray-100 max-w-[600px]">
{content}
</div>
)}
</div>
)
const SolutionSection = ({
title,
content,
isLoading,
currentLanguage
}: {
title: string
content: React.ReactNode
isLoading: boolean
currentLanguage: string
}) => {
const [copied, setCopied] = useState(false)
const copyToClipboard = () => {
if (typeof content === "string") {
navigator.clipboard.writeText(content).then(() => {
setCopied(true)
setTimeout(() => setCopied(false), 2000)
})
}
}
return (
<div className="space-y-2 relative">
<h2 className="text-[13px] font-medium text-white tracking-wide">
{title}
</h2>
{isLoading ? (
<div className="space-y-1.5">
<div className="mt-4 flex">
<p className="text-xs bg-gradient-to-r from-gray-300 via-gray-100 to-gray-300 bg-clip-text text-transparent animate-pulse">
Loading solutions...
</p>
</div>
</div>
) : (
<div className="w-full relative">
<button
onClick={copyToClipboard}
className="absolute top-2 right-2 text-xs text-white bg-white/10 hover:bg-white/20 rounded px-2 py-1 transition"
>
{copied ? "Copied!" : "Copy"}
</button>
<SyntaxHighlighter
showLineNumbers
language={currentLanguage == "golang" ? "go" : currentLanguage}
style={dracula}
customStyle={{
maxWidth: "100%",
margin: 0,
padding: "1rem",
whiteSpace: "pre-wrap",
wordBreak: "break-all",
backgroundColor: "rgba(22, 27, 34, 0.5)"
}}
wrapLongLines={true}
>
{content as string}
</SyntaxHighlighter>
</div>
)}
</div>
)
}
interface FollowUp {
id: number
question: string
answer: string | null
error?: string
}
const FollowUpSection = ({ design }: { design: string }) => {
const [followUps, setFollowUps] = useState<FollowUp[]>([])
const [question, setQuestion] = useState("")
const latestFollowUpRef = useRef<HTMLDivElement>(null)
const isAsking = followUps.some(
(followUp) => followUp.answer === null && !followUp.error
)
// Bring each new question into view so its answer appears right below it
useEffect(() => {
latestFollowUpRef.current?.scrollIntoView({
behavior: "smooth",
block: "start"
})
}, [followUps.length])
const askFollowUp = async () => {
const trimmedQuestion = question.trim()
if (!trimmedQuestion || isAsking) return
const id = Date.now()
// Failed follow-ups have no answer, so they don't go back to the model
const history = followUps.flatMap((followUp) =>
followUp.answer
? [{ question: followUp.question, answer: followUp.answer }]
: []
)
setFollowUps((current) => [
...current,
{ id, question: trimmedQuestion, answer: null }
])
setQuestion("")
let result: { success: boolean; data?: string; error?: string }
try {
result = await window.electronAPI.askFollowUp({
design,
history,
question: trimmedQuestion
})
} catch (error) {
console.error("Error asking follow-up:", error)
result = { success: false }
}
setFollowUps((current) =>
current.map((followUp) => {
if (followUp.id !== id) return followUp
return result.success && result.data
? { ...followUp, answer: result.data }
: {
...followUp,
error: result.error || "Failed to answer the follow-up."
}
})
)
}
return (
<div className="space-y-2">
<h2 className="text-[13px] font-medium text-white tracking-wide">
Follow-ups
</h2>
{followUps.map((followUp, index) => (
<div
key={followUp.id}
ref={index === followUps.length - 1 ? latestFollowUpRef : undefined}
className="space-y-2"
>
<div className="text-[13px] leading-[1.4] text-gray-100 bg-white/10 rounded-md px-3 py-2 whitespace-pre-wrap">
{followUp.question}
</div>
{followUp.error ? (
<p className="text-xs text-red-400">{followUp.error}</p>
) : followUp.answer === null ? (
<p className="text-xs bg-gradient-to-r from-gray-300 via-gray-100 to-gray-300 bg-clip-text text-transparent animate-pulse">
Thinking...
</p>
) : (
<SyntaxHighlighter
language="markdown"
style={dracula}
customStyle={{
maxWidth: "100%",
margin: 0,
padding: "1rem",
whiteSpace: "pre-wrap",
wordBreak: "break-word",
backgroundColor: "rgba(22, 27, 34, 0.5)"
}}
wrapLongLines={true}
>
{followUp.answer}
</SyntaxHighlighter>
)}
</div>
))}
<textarea
value={question}
onChange={(e) => setQuestion(e.target.value)}
onKeyDown={(e) => {
// Enter sends; Shift+Enter adds a line. Cmd+Enter is the global process shortcut.
if (e.key === "Enter" && !e.shiftKey && !e.nativeEvent.isComposing) {
e.preventDefault()
askFollowUp()
}
}}
rows={2}
placeholder={
isAsking
? "Waiting for the answer..."
: "Ask about any part of the design, e.g. how would this handle 10x the traffic? (Enter to send)"
}
className="w-full resize-none rounded-md border border-white/10 bg-white/5 px-3 py-2 text-[13px] leading-[1.4] text-gray-100 placeholder:text-gray-500 focus:outline-none focus:border-white/30"
/>
</div>
)
}
export const ComplexitySection = ({
timeComplexity,
spaceComplexity,
isLoading
}: {
timeComplexity: string | null
spaceComplexity: string | null
isLoading: boolean
}) => {
// Helper to ensure we have proper complexity values
const formatComplexity = (complexity: string | null): string => {
// Default if no complexity returned by LLM
if (!complexity || complexity.trim() === "") {
return "Complexity not available";
}
const bigORegex = /O\([^)]+\)/i;
// Return the complexity as is if it already has Big O notation
if (bigORegex.test(complexity)) {
return complexity;
}
// Concat Big O notation to the complexity
return `O(${complexity})`;
};
const formattedTimeComplexity = formatComplexity(timeComplexity);
const formattedSpaceComplexity = formatComplexity(spaceComplexity);
return (
<div className="space-y-2">
<h2 className="text-[13px] font-medium text-white tracking-wide">
Complexity
</h2>
{isLoading ? (
<p className="text-xs bg-gradient-to-r from-gray-300 via-gray-100 to-gray-300 bg-clip-text text-transparent animate-pulse">
Calculating complexity...
</p>
) : (
<div className="space-y-3">
<div className="text-[13px] leading-[1.4] text-gray-100 bg-white/5 rounded-md p-3">
<div className="flex items-start gap-2">
<div className="w-1 h-1 rounded-full bg-blue-400/80 mt-2 shrink-0" />
<div>
<strong>Time:</strong> {formattedTimeComplexity}
</div>
</div>
</div>
<div className="text-[13px] leading-[1.4] text-gray-100 bg-white/5 rounded-md p-3">
<div className="flex items-start gap-2">
<div className="w-1 h-1 rounded-full bg-blue-400/80 mt-2 shrink-0" />
<div>
<strong>Space:</strong> {formattedSpaceComplexity}
</div>
</div>
</div>
</div>
)}
</div>
);
}
export interface SolutionsProps {
setView: (view: "queue" | "solutions" | "debug") => void
credits: number
currentLanguage: string
setLanguage: (language: string) => void
}
const Solutions: React.FC<SolutionsProps> = ({
setView,
credits,
currentLanguage,
setLanguage
}) => {
const queryClient = useQueryClient()
const contentRef = useRef<HTMLDivElement>(null)
const [debugProcessing, setDebugProcessing] = useState(false)
const [problemStatementData, setProblemStatementData] =
useState<ProblemStatementData | null>(null)
const isSystemDesign = problemStatementData?.problem_type === "system_design"
const [solutionData, setSolutionData] = useState<string | null>(null)
const [thoughtsData, setThoughtsData] = useState<string[] | null>(null)
const [timeComplexityData, setTimeComplexityData] = useState<string | null>(
null
)
const [spaceComplexityData, setSpaceComplexityData] = useState<string | null>(
null
)
const [isTooltipVisible, setIsTooltipVisible] = useState(false)
const [tooltipHeight, setTooltipHeight] = useState(0)
const [isResetting, setIsResetting] = useState(false)
interface Screenshot {
id: string
path: string
preview: string
timestamp: number
}
const [extraScreenshots, setExtraScreenshots] = useState<Screenshot[]>([])
useEffect(() => {
const fetchScreenshots = async () => {
try {
const existing = await window.electronAPI.getScreenshots()
console.log("Raw screenshot data:", existing)
const screenshots = (Array.isArray(existing) ? existing : []).map(
(p) => ({
id: p.path,
path: p.path,
preview: p.preview,
timestamp: Date.now()
})
)
console.log("Processed screenshots:", screenshots)
setExtraScreenshots(screenshots)
} catch (error) {
console.error("Error loading extra screenshots:", error)
setExtraScreenshots([])
}
}
fetchScreenshots()
}, [solutionData])
const { showToast } = useToast()
useEffect(() => {
// Height update logic
const updateDimensions = () => {
if (contentRef.current) {
let contentHeight = contentRef.current.scrollHeight
const contentWidth = contentRef.current.scrollWidth
if (isTooltipVisible) {
contentHeight += tooltipHeight
}
window.electronAPI.updateContentDimensions({
width: contentWidth,
height: contentHeight
})
}
}
// Initialize resize observer
const resizeObserver = new ResizeObserver(updateDimensions)
if (contentRef.current) {
resizeObserver.observe(contentRef.current)
}
updateDimensions()
// Set up event listeners
const cleanupFunctions = [
window.electronAPI.onScreenshotTaken(async () => {
try {
const existing = await window.electronAPI.getScreenshots()
const screenshots = (Array.isArray(existing) ? existing : []).map(
(p) => ({
id: p.path,
path: p.path,
preview: p.preview,
timestamp: Date.now()
})
)
setExtraScreenshots(screenshots)
} catch (error) {
console.error("Error loading extra screenshots:", error)
}
}),
window.electronAPI.onResetView(() => {
// Set resetting state first
setIsResetting(true)
// Remove queries
queryClient.removeQueries({
queryKey: ["solution"]
})
queryClient.removeQueries({
queryKey: ["new_solution"]
})
// Reset screenshots
setExtraScreenshots([])
// After a small delay, clear the resetting state
setTimeout(() => {
setIsResetting(false)
}, 0)
}),
window.electronAPI.onSolutionStart(() => {
// Every time processing starts, reset relevant states
setSolutionData(null)
setThoughtsData(null)
setTimeComplexityData(null)
setSpaceComplexityData(null)
}),
window.electronAPI.onProblemExtracted((data) => {
queryClient.setQueryData(["problem_statement"], data)
}),
//if there was an error processing the initial solution
window.electronAPI.onSolutionError((error: string) => {
showToast("Processing Failed", error, "error")
// Reset solutions in the cache (even though this shouldn't ever happen) and complexities to previous states
const solution = queryClient.getQueryData(["solution"]) as {
code: string
thoughts: string[]
time_complexity: string
space_complexity: string
} | null
if (!solution) {
setView("queue")
}
setSolutionData(solution?.code || null)
setThoughtsData(solution?.thoughts || null)
setTimeComplexityData(solution?.time_complexity || null)
setSpaceComplexityData(solution?.space_complexity || null)
console.error("Processing error:", error)
}),
//when the initial solution is generated, we'll set the solution data to that
window.electronAPI.onSolutionSuccess((data) => {
if (!data) {
console.warn("Received empty or invalid solution data")
return
}
console.log({ data })
const solutionData = {
code: data.code,
thoughts: data.thoughts,
time_complexity: data.time_complexity,
space_complexity: data.space_complexity
}
queryClient.setQueryData(["solution"], solutionData)
setSolutionData(solutionData.code || null)
setThoughtsData(solutionData.thoughts || null)
setTimeComplexityData(solutionData.time_complexity || null)
setSpaceComplexityData(solutionData.space_complexity || null)
// Fetch latest screenshots when solution is successful
const fetchScreenshots = async () => {
try {
const existing = await window.electronAPI.getScreenshots()
const screenshots =
existing.previews?.map((p) => ({
id: p.path,
path: p.path,
preview: p.preview,
timestamp: Date.now()
})) || []
setExtraScreenshots(screenshots)
} catch (error) {
console.error("Error loading extra screenshots:", error)
setExtraScreenshots([])
}
}
fetchScreenshots()
}),
//########################################################
//DEBUG EVENTS
//########################################################
window.electronAPI.onDebugStart(() => {
//we'll set the debug processing state to true and use that to render a little loader
setDebugProcessing(true)
}),
//the first time debugging works, we'll set the view to debug and populate the cache with the data
window.electronAPI.onDebugSuccess((data) => {
queryClient.setQueryData(["new_solution"], data)
setDebugProcessing(false)
}),
//when there was an error in the initial debugging, we'll show a toast and stop the little generating pulsing thing.
window.electronAPI.onDebugError(() => {
showToast(
"Processing Failed",
"There was an error debugging your code.",
"error"
)
setDebugProcessing(false)
}),
window.electronAPI.onProcessingNoScreenshots(() => {
showToast(
"No Screenshots",
"There are no extra screenshots to process.",
"neutral"
)
}),
// Removed out of credits handler - unlimited credits in this version
]
return () => {
resizeObserver.disconnect()
cleanupFunctions.forEach((cleanup) => cleanup())
}
}, [isTooltipVisible, tooltipHeight])
useEffect(() => {
setProblemStatementData(
queryClient.getQueryData(["problem_statement"]) || null
)
setSolutionData(queryClient.getQueryData(["solution"]) || null)
const unsubscribe = queryClient.getQueryCache().subscribe((event) => {
if (event?.query.queryKey[0] === "problem_statement") {
setProblemStatementData(
queryClient.getQueryData(["problem_statement"]) || null
)
}
if (event?.query.queryKey[0] === "solution") {
const solution = queryClient.getQueryData(["solution"]) as {
code: string
thoughts: string[]
time_complexity: string
space_complexity: string
} | null
setSolutionData(solution?.code ?? null)
setThoughtsData(solution?.thoughts ?? null)
setTimeComplexityData(solution?.time_complexity ?? null)
setSpaceComplexityData(solution?.space_complexity ?? null)
}
})
return () => unsubscribe()
}, [queryClient])
const handleTooltipVisibilityChange = (visible: boolean, height: number) => {
setIsTooltipVisible(visible)
setTooltipHeight(height)
}
const handleDeleteExtraScreenshot = async (index: number) => {
const screenshotToDelete = extraScreenshots[index]
try {
const response = await window.electronAPI.deleteScreenshot(
screenshotToDelete.path
)
if (response.success) {
// Fetch and update screenshots after successful deletion
const existing = await window.electronAPI.getScreenshots()
const screenshots = (Array.isArray(existing) ? existing : []).map(
(p) => ({
id: p.path,
path: p.path,
preview: p.preview,
timestamp: Date.now()
})
)
setExtraScreenshots(screenshots)
} else {
console.error("Failed to delete extra screenshot:", response.error)
showToast("Error", "Failed to delete the screenshot", "error")
}
} catch (error) {
console.error("Error deleting extra screenshot:", error)
showToast("Error", "Failed to delete the screenshot", "error")
}
}
return (
<>
{!isResetting && queryClient.getQueryData(["new_solution"]) ? (
<Debug
isProcessing={debugProcessing}
setIsProcessing={setDebugProcessing}
currentLanguage={currentLanguage}
setLanguage={setLanguage}
/>
) : (
<div ref={contentRef} className="relative">
<div className="space-y-3 px-4 py-3">
{/* Conditionally render the screenshot queue if solutionData is available */}
{solutionData && (
<div className="bg-transparent w-fit">
<div className="pb-3">
<div className="space-y-3 w-fit">
<ScreenshotQueue
isLoading={debugProcessing}
screenshots={extraScreenshots}
onDeleteScreenshot={handleDeleteExtraScreenshot}
/>
</div>
</div>
</div>
)}
{/* Navbar of commands with the SolutionsHelper */}
<SolutionCommands
onTooltipVisibilityChange={handleTooltipVisibilityChange}
isProcessing={!problemStatementData || !solutionData}
extraScreenshots={extraScreenshots}
credits={credits}
currentLanguage={currentLanguage}
setLanguage={setLanguage}
/>
{/* Main Content - Modified width constraints */}
<div className="w-full text-sm text-black bg-black/60 rounded-md">
<div className="rounded-lg overflow-hidden">
<div className="px-4 py-3 space-y-4 max-w-full">
{!solutionData && (
<>
<ContentSection
title="Problem Statement"
content={problemStatementData?.problem_statement}
isLoading={!problemStatementData}
/>
{problemStatementData && (
<div className="mt-4 flex">
<p className="text-xs bg-gradient-to-r from-gray-300 via-gray-100 to-gray-300 bg-clip-text text-transparent animate-pulse">
Generating solutions...
</p>
</div>
)}
</>
)}
{solutionData && (
<>
<ContentSection
title={`${isSystemDesign ? "Key Decisions" : "My Thoughts"} (${COMMAND_KEY} + Arrow keys to scroll)`}
content={
thoughtsData && (
<div className="space-y-3">
<div className="space-y-1">
{thoughtsData.map((thought, index) => (
<div
key={index}
className="flex items-start gap-2"
>
<div className="w-1 h-1 rounded-full bg-blue-400/80 mt-2 shrink-0" />
<div>{thought}</div>
</div>
))}
</div>
</div>
)
}
isLoading={!thoughtsData}
/>
<SolutionSection
title={isSystemDesign ? "Design" : "Solution"}
content={solutionData}
isLoading={!solutionData}
currentLanguage={isSystemDesign ? "markdown" : currentLanguage}
/>
{isSystemDesign ? (
<FollowUpSection design={solutionData} />
) : (
<ComplexitySection
timeComplexity={timeComplexityData}
spaceComplexity={spaceComplexityData}
isLoading={!timeComplexityData || !spaceComplexityData}
/>
)}
</>
)}
</div>
</div>
</div>
</div>
</div>
)}
</>
)
}
export default Solutions