Githubgemini / assets /AiCodeExplainer-6kcSUCD1.js
admin08077's picture
Upload 121 files
e025d49 verified
import{r as s,j as e}from"./react-DKy9e2uO.js";import{e as w,C,L as j,M as S}from"./index-Ykf-MVva.js";import"./bottleneck-Cpj98o6Y.js";import"./react-dom-CpxHE_eW.js";import"./scheduler-DYLXRpC5.js";import"./idb-Dob3nYDb.js";import"./@google-D80DdW2m.js";import"./marked-CesSW9Du.js";import"./jszip-s56H2EZ-.js";const k=`const bubbleSort = (arr) => {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
};`,L=a=>a.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\b(const|let|var|function|return|if|for|=>|import|from|export|default)\b/g,'<span class="text-indigo-600 font-semibold">$1</span>').replace(/(\`|'|")(.*?)(\`|'|")/g,'<span class="text-emerald-700">$1$2$3</span>').replace(/(\/\/.*)/g,'<span class="text-gray-500 italic">$1</span>').replace(/(\{|\}|\(|\)|\[|\])/g,'<span class="text-gray-600">$1</span>'),F=({initialCode:a})=>{const[n,p]=s.useState(a||k),[t,u]=s.useState(null),[o,f]=s.useState(!1),[d,x]=s.useState(""),[h,g]=s.useState("summary"),c=s.useRef(null),i=s.useRef(null),m=s.useCallback(async r=>{if(!r.trim()){x("Please enter some code to explain.");return}f(!0),x(""),u(null),g("summary");try{const l=await w(r);u(l)}catch(l){const v=l instanceof Error?l.message:"An unknown error occurred.";x(`Failed to get explanation: ${v}`)}finally{f(!1)}},[]);s.useEffect(()=>{a&&(p(a),m(a))},[a,m]);const y=()=>{i.current&&c.current&&(i.current.scrollTop=c.current.scrollTop,i.current.scrollLeft=c.current.scrollLeft)},b=s.useMemo(()=>L(n),[n]),N=()=>{if(!t)return null;switch(h){case"summary":return e.jsx(S,{content:t.summary});case"lineByLine":return e.jsx("div",{className:"space-y-3",children:t.lineByLine.map((r,l)=>e.jsxs("div",{className:"p-3 bg-background rounded-md border border-border",children:[e.jsxs("p",{className:"font-mono text-xs text-primary mb-1",children:["Lines: ",r.lines]}),e.jsx("p",{className:"text-sm",children:r.explanation})]},l))});case"complexity":return e.jsxs("div",{children:[e.jsxs("p",{children:[e.jsx("strong",{children:"Time Complexity:"})," ",e.jsx("span",{className:"font-mono text-amber-600",children:t.complexity.time})]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Space Complexity:"})," ",e.jsx("span",{className:"font-mono text-amber-600",children:t.complexity.space})]})]});case"suggestions":return e.jsx("ul",{className:"list-disc list-inside space-y-2",children:t.suggestions.map((r,l)=>e.jsx("li",{children:r},l))})}};return e.jsxs("div",{className:"h-full flex flex-col p-4 sm:p-6 lg:p-8 text-text-primary",children:[e.jsxs("header",{className:"mb-6 flex-shrink-0",children:[e.jsxs("h1",{className:"text-3xl font-bold flex items-center",children:[e.jsx(C,{}),e.jsx("span",{className:"ml-3",children:"AI Code Explainer"})]}),e.jsx("p",{className:"text-text-secondary mt-1",children:"Get a detailed, structured analysis of any code snippet."})]}),e.jsxs("div",{className:"flex-grow flex flex-col lg:flex-row gap-6 min-h-0",children:[e.jsxs("div",{className:"flex flex-col min-h-0 lg:w-1/2",children:[e.jsx("label",{htmlFor:"code-input",className:"text-sm font-medium text-text-secondary mb-2",children:"Your Code"}),e.jsxs("div",{className:"relative flex-grow bg-surface border border-border rounded-md focus-within:ring-2 focus-within:ring-primary overflow-hidden",children:[e.jsx("textarea",{ref:c,id:"code-input",value:n,onChange:r=>p(r.target.value),onScroll:y,placeholder:"Paste your code here...",spellCheck:"false",className:"absolute inset-0 w-full h-full p-4 bg-transparent resize-none font-mono text-sm text-transparent caret-primary outline-none z-10"}),e.jsx("pre",{ref:i,"aria-hidden":"true",className:"absolute inset-0 w-full h-full p-4 font-mono text-sm text-text-primary pointer-events-none z-0 whitespace-pre-wrap overflow-auto no-scrollbar",dangerouslySetInnerHTML:{__html:b+`
`}})]}),e.jsx("div",{className:"mt-4 flex-shrink-0",children:e.jsx("button",{onClick:()=>m(n),disabled:o,className:"btn-primary w-full flex items-center justify-center px-6 py-3",children:o?e.jsx(j,{}):"Analyze Code"})})]}),e.jsxs("div",{className:"flex flex-col min-h-0 lg:w-1/2",children:[e.jsx("label",{className:"text-sm font-medium text-text-secondary mb-2",children:"AI Analysis"}),e.jsxs("div",{className:"relative flex-grow flex flex-col bg-surface border border-border rounded-md overflow-hidden",children:[e.jsx("div",{className:"flex-shrink-0 flex border-b border-border",children:["summary","lineByLine","complexity","suggestions"].map(r=>e.jsx("button",{onClick:()=>g(r),disabled:!t,className:`px-4 py-2 text-sm font-medium capitalize transition-colors ${h===r?"bg-background text-primary font-semibold":"text-text-secondary hover:bg-gray-100 disabled:text-gray-400"}`,children:r.replace(/([A-Z])/g," $1")},r))}),e.jsxs("div",{className:"p-4 flex-grow overflow-y-auto",children:[o&&e.jsx("div",{className:"flex items-center justify-center h-full",children:e.jsx(j,{})}),d&&e.jsx("p",{className:"text-red-500",children:d}),t&&!o&&N(),!o&&!t&&!d&&e.jsx("div",{className:"text-text-secondary h-full flex items-center justify-center",children:"The analysis will appear here."})]})]})]})]})]})};export{F as AiCodeExplainer};
//# sourceMappingURL=AiCodeExplainer-6kcSUCD1.js.map