|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import { ReactNode } from "react"; |
| 3 | +import { |
| 4 | + createContext, |
| 5 | + ReactNode, |
| 6 | + useCallback, |
| 7 | + useContext, |
| 8 | + useState, |
| 9 | +} from "react"; |
4 | 10 | import { type MarkdownSection } from "./splitMarkdown"; |
5 | 11 | import { StyledMarkdown } from "./markdown"; |
6 | 12 | import { ChatForm } from "./chatForm"; |
| 13 | +import { ReplCommand, ReplOutput } from "../terminal/repl"; |
| 14 | +import { useFile } from "../terminal/file"; |
| 15 | + |
| 16 | +// セクション内に埋め込まれているターミナルとファイルエディターの内容をSection側から取得できるよう、 |
| 17 | +// Contextに保存する |
| 18 | +interface ISectionCodeContext { |
| 19 | + addReplOutput: (command: string, output: ReplOutput[]) => void; |
| 20 | + addFile: (filename: string) => void; |
| 21 | + setExecResult: (filename: string, output: ReplOutput[]) => void; |
| 22 | +} |
| 23 | +const SectionCodeContext = createContext<ISectionCodeContext | null>(null); |
| 24 | +export const useSectionCode = () => useContext(SectionCodeContext); |
7 | 25 |
|
8 | 26 | // 1つのセクションのタイトルと内容を表示する。内容はMarkdownとしてレンダリングする |
9 | 27 | export function Section({ section }: { section: MarkdownSection }) { |
| 28 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 29 | + const [replOutputs, setReplOutputs] = useState<ReplCommand[]>([]); |
| 30 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 31 | + const [execResults, setExecResults] = useState<Record<string, ReplOutput[]>>( |
| 32 | + {} |
| 33 | + ); |
| 34 | + const [filenames, setFilenames] = useState<string[]>([]); |
| 35 | + const { files } = useFile(); |
| 36 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 37 | + const fileContents: { name: string; content: string }[] = filenames.map( |
| 38 | + (name) => ({ name, content: files[name] || "" }) |
| 39 | + ); |
| 40 | + const addReplOutput = useCallback( |
| 41 | + (command: string, output: ReplOutput[]) => |
| 42 | + setReplOutputs((outs) => [...outs, { command, output }]), |
| 43 | + [] |
| 44 | + ); |
| 45 | + const addFile = useCallback( |
| 46 | + (filename: string) => |
| 47 | + setFilenames((filenames) => |
| 48 | + filenames.includes(filename) ? filenames : [...filenames, filename] |
| 49 | + ), |
| 50 | + [] |
| 51 | + ); |
| 52 | + const setExecResult = useCallback( |
| 53 | + (filename: string, output: ReplOutput[]) => |
| 54 | + setExecResults((results) => { |
| 55 | + results[filename] = output; |
| 56 | + return results; |
| 57 | + }), |
| 58 | + [] |
| 59 | + ); |
| 60 | + |
| 61 | + // replOutputs: section内にあるターミナルにユーザーが入力したコマンドとその実行結果 |
| 62 | + // fileContents: section内にあるファイルエディターの内容 |
| 63 | + // execResults: section内にあるファイルの実行結果 |
| 64 | + // console.log(section.title, replOutputs, fileContents, execResults); |
| 65 | + |
10 | 66 | return ( |
11 | | - <div> |
12 | | - <Heading level={section.level}>{section.title}</Heading> |
13 | | - <StyledMarkdown content={section.content} /> |
14 | | - <ChatForm documentContent={section.content} /> |
15 | | - </div> |
| 67 | + <SectionCodeContext.Provider |
| 68 | + value={{ addReplOutput, addFile, setExecResult }} |
| 69 | + > |
| 70 | + <div> |
| 71 | + <Heading level={section.level}>{section.title}</Heading> |
| 72 | + <StyledMarkdown content={section.content} /> |
| 73 | + <ChatForm documentContent={section.content} /> |
| 74 | + </div> |
| 75 | + </SectionCodeContext.Provider> |
16 | 76 | ); |
17 | 77 | } |
18 | 78 |
|
19 | | -export function Heading({ level, children }: { level: number; children: ReactNode }) { |
| 79 | +export function Heading({ |
| 80 | + level, |
| 81 | + children, |
| 82 | +}: { |
| 83 | + level: number; |
| 84 | + children: ReactNode; |
| 85 | +}) { |
20 | 86 | switch (level) { |
21 | 87 | case 1: |
22 | 88 | return <h1 className="text-2xl font-bold my-4">{children}</h1>; |
|
0 commit comments