-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathembedContext.tsx
More file actions
191 lines (179 loc) · 5.63 KB
/
embedContext.tsx
File metadata and controls
191 lines (179 loc) · 5.63 KB
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
"use client";
import { ReplCommand, ReplOutput } from "@my-code/runtime/interface";
import { usePathname } from "next/navigation";
import {
createContext,
ReactNode,
useCallback,
useContext,
useState,
} from "react";
/*
ファイルの内容や埋め込みターミナルのログを1箇所でまとめて管理し、書き込み・読み込みできるようにする
ファイルはページのURLごとに独立して管理し、常にすべて保持している。
ターミナルのログもページごとに独立だが、ページが切り替わるたびに消す。
*/
// 全部stringだけどわかりやすくするために名前をつけている
type PagePathname = string;
type Filename = string;
type TerminalId = string;
interface IEmbedContext {
files: Readonly<Record<Filename, string>>;
// ファイルを書き込む。更新後のページ内の全ファイル内容を返す
// 返り値を使うことで再レンダリングを待たずに最新の内容を取得できる
writeFile: (
updatedFiles: Readonly<Record<Filename, string>>
) => Promise<Readonly<Record<Filename, string>>>;
replOutputs: Readonly<Record<TerminalId, ReplCommand[]>>;
addReplCommand: (terminalId: TerminalId, command: string) => string;
addReplOutput: (
terminalId: TerminalId,
commandId: string,
output: ReplOutput
) => void;
execResults: Readonly<Record<Filename, ReplOutput[]>>;
clearExecResult: (filename: Filename) => void;
addExecOutput: (filename: Filename, output: ReplOutput) => void;
}
const EmbedContext = createContext<IEmbedContext>(null!);
export function useEmbedContext() {
const context = useContext(EmbedContext);
if (!context) {
throw new Error(
"useEmbedContext must be used within a EmbedContextProvider"
);
}
return context;
}
export function EmbedContextProvider({ children }: { children: ReactNode }) {
const pathname = usePathname();
const [prevPathname, setPrevPathname] = useState<PagePathname>("");
const [files, setFiles] = useState<
Record<PagePathname, Record<Filename, string>>
>({});
const [replOutputs, setReplOutputs] = useState<
Record<TerminalId, ReplCommand[]>
>({});
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [commandIdCounters, setCommandIdCounters] = useState<
Record<TerminalId, number>
>({});
const [execResults, setExecResults] = useState<
Record<Filename, ReplOutput[]>
>({});
if (pathname && pathname !== prevPathname) {
setPrevPathname(pathname);
setReplOutputs({});
setCommandIdCounters({});
setExecResults({});
}
const writeFile = useCallback(
(updatedFiles: Record<Filename, string>) => {
return new Promise<Record<Filename, string>>((resolve) => {
setFiles((files) => {
let changed = false;
const newFiles = { ...files };
newFiles[pathname] = { ...(newFiles[pathname] ?? {}) };
for (const [name, content] of Object.entries(updatedFiles)) {
if (newFiles[pathname][name] !== content) {
changed = true;
newFiles[pathname][name] = content;
}
}
if (changed) {
resolve(newFiles[pathname]);
return newFiles;
} else {
resolve(files[pathname] || {});
return files;
}
});
});
},
[pathname]
);
const addReplCommand = useCallback(
(terminalId: TerminalId, command: string): string => {
let commandId = "";
setCommandIdCounters((counters) => {
const newCounters = { ...counters };
const currentCount = newCounters[terminalId] ?? 0;
commandId = String(currentCount);
newCounters[terminalId] = currentCount + 1;
return newCounters;
});
setReplOutputs((outs) => {
outs = { ...outs };
if (!(terminalId in outs)) {
outs[terminalId] = [];
}
outs[terminalId] = [
...outs[terminalId],
{ command: command, output: [], commandId },
];
return outs;
});
return commandId;
},
[]
);
const addReplOutput = useCallback(
(terminalId: TerminalId, commandId: string, output: ReplOutput) =>
setReplOutputs((outs) => {
outs = { ...outs };
if (terminalId in outs) {
outs[terminalId] = [...outs[terminalId]];
// Find the command by commandId
const commandIndex = outs[terminalId].findIndex(
(cmd) => cmd.commandId === commandId
);
if (commandIndex >= 0) {
const command = outs[terminalId][commandIndex];
outs[terminalId][commandIndex] = {
...command,
output: [...command.output, output],
};
}
}
return outs;
}),
[]
);
const clearExecResult = useCallback(
(filename: Filename) =>
setExecResults((results) => {
results = { ...results };
results[filename] = [];
return results;
}),
[]
);
const addExecOutput = useCallback(
(filename: Filename, output: ReplOutput) =>
setExecResults((results) => {
results = { ...results };
if (!(filename in results)) {
results[filename] = [];
}
results[filename] = [...results[filename], output];
return results;
}),
[]
);
return (
<EmbedContext.Provider
value={{
files: files[pathname] || {},
writeFile,
replOutputs,
addReplCommand,
addReplOutput,
execResults,
clearExecResult,
addExecOutput,
}}
>
{children}
</EmbedContext.Provider>
);
}