Spaces:
Runtime error
Runtime error
File size: 6,051 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | "use client";
import { useEffect, useState } from "react";
import { useTranslations } from "next-intl";
import ReactMarkdown, { type Components } from "react-markdown";
import { Modal, Button } from "@/shared/components";
const markdownComponents: Components = {
h1({ children }) {
return <h1 className="mb-4 text-xl font-bold text-text-main">{children}</h1>;
},
h2({ children }) {
return (
<h2 className="mt-6 mb-3 flex items-center gap-2 text-base font-bold text-text-main first:mt-0">
<span className="material-symbols-outlined text-[16px] text-primary">terminal</span>
{children}
</h2>
);
},
h3({ children }) {
return <h3 className="mt-4 mb-2 text-sm font-semibold text-text-main/80">{children}</h3>;
},
h4({ children }) {
return (
<h4 className="mt-3 mb-1 text-xs font-semibold uppercase tracking-wide text-text-muted">
{children}
</h4>
);
},
p({ children }) {
return <p className="mb-2 text-sm leading-relaxed text-text-muted">{children}</p>;
},
ul({ children }) {
return <ul className="my-2 flex flex-col gap-1">{children}</ul>;
},
ol({ children }) {
return <ol className="my-2 flex flex-col gap-1 list-decimal list-inside">{children}</ol>;
},
li({ children }) {
return (
<li className="ml-2 flex items-start text-sm leading-relaxed text-text-muted">
<span className="mr-2 mt-2 size-1 shrink-0 rounded-full bg-text-muted/40" />
<span>{children}</span>
</li>
);
},
strong({ children }) {
return <strong className="font-semibold text-text-main">{children}</strong>;
},
code({ children, className }) {
const isBlock = className?.includes("language-");
if (isBlock) {
return (
<code className="block w-full whitespace-pre-wrap font-mono text-[12px] text-text-main">
{children}
</code>
);
}
return (
<code className="rounded border border-black/5 bg-bg-subtle px-1 py-0.5 font-mono text-[12px] text-text-main dark:border-white/5">
{children}
</code>
);
},
pre({ children }) {
return (
<pre className="my-2 overflow-x-auto rounded-lg border border-border bg-bg-subtle p-3">
{children}
</pre>
);
},
blockquote({ children }) {
return (
<blockquote className="my-2 border-l-2 border-primary/40 pl-3 text-sm text-text-muted/80 italic">
{children}
</blockquote>
);
},
table({ children }) {
return (
<div className="my-3 overflow-x-auto rounded-lg border border-border">
<table className="w-full text-sm">{children}</table>
</div>
);
},
thead({ children }) {
return <thead className="bg-bg-subtle text-xs text-text-muted">{children}</thead>;
},
tr({ children }) {
return <tr className="border-b border-border last:border-0">{children}</tr>;
},
th({ children }) {
return <th className="px-3 py-2 text-left font-semibold">{children}</th>;
},
td({ children }) {
return <td className="px-3 py-2 text-text-muted">{children}</td>;
},
hr() {
return <hr className="my-4 border-border" />;
},
a({ href, children }) {
if (!href) return <span>{children}</span>;
return (
<a
href={href}
target="_blank"
rel="noopener noreferrer"
className="text-primary hover:underline"
>
{children}
</a>
);
},
};
interface CodexCliGuideModalProps {
isOpen: boolean;
onClose: () => void;
}
export default function CodexCliGuideModal({ isOpen, onClose }: CodexCliGuideModalProps) {
const t = useTranslations("providers");
const tc = useTranslations("common");
const [content, setContent] = useState("");
const [loading, setLoading] = useState(false);
const [error, setError] = useState(false);
const text = (key: string, fallback: string) =>
typeof t.has === "function" && t.has(key as never) ? t(key as never) : fallback;
useEffect(() => {
if (!isOpen || content) return;
let cancelled = false;
const load = async () => {
setLoading(true);
setError(false);
try {
const res = await fetch("/api/docs/codex-cli");
if (!res.ok) throw new Error(`${res.status}`);
const data = (await res.json()) as { content: string };
if (!cancelled) setContent(data.content);
} catch {
if (!cancelled) setError(true);
} finally {
if (!cancelled) setLoading(false);
}
};
void load();
return () => {
cancelled = true;
};
}, [isOpen, content]);
return (
<Modal isOpen={isOpen} title={text("codexCliGuideTitle", "Codex CLI Guide")} onClose={onClose}>
<div className="max-h-[70vh] overflow-y-auto pr-1">
{loading && (
<div className="flex flex-col items-center justify-center py-16 gap-3">
<span className="material-symbols-outlined animate-spin text-[28px] text-text-muted/50">
sync
</span>
<p className="text-sm text-text-muted">
{text("codexCliGuideLoading", "Loading guide...")}
</p>
</div>
)}
{error && (
<div className="flex flex-col items-center justify-center py-12 text-text-muted gap-3">
<span className="material-symbols-outlined text-[40px] text-red-500/50">
error_outline
</span>
<p className="text-sm">
{text("codexCliGuideLoadFailed", "Could not load the guide.")}
</p>
<Button
variant="secondary"
size="sm"
onClick={() => {
setContent("");
setError(false);
}}
>
{tc("retry")}
</Button>
</div>
)}
{!loading && !error && content && (
<div className="p-1">
<ReactMarkdown components={markdownComponents}>{content}</ReactMarkdown>
</div>
)}
</div>
</Modal>
);
}
|