Skip to content

Commit 2c78683

Browse files
authored
Factor out language selector into its own component (#156)
1 parent 2f5f8e3 commit 2c78683

File tree

21 files changed

+180
-81
lines changed

21 files changed

+180
-81
lines changed

tools/adventure-pack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"jest": "29.7.0",
6060
"prettier": "3.3.2",
6161
"prettier-plugin-java": "2.6.0",
62-
"ts-jest": "29.1.5",
62+
"ts-jest": "29.2.2",
6363
"ts-loader": "9.5.1",
6464
"ts-morph": "23.0.0",
6565
"ts-node": "10.9.2",

tools/adventure-pack/src/app/App.tsx renamed to tools/adventure-pack/src/app/components/App.tsx

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ import React, { useEffect } from "react";
33
import { Checkbox } from "./Checkbox";
44
import { GoodyCard } from "./GoodyCard";
55
import { HighlightedCode } from "./HighlightedCode";
6-
import { fetchGoodies } from "./fetchGoodies";
7-
import type { Language } from "./Language";
8-
import { useMergedCode } from "./useMergedCode";
9-
import { useAppState } from "./useAppState";
10-
import { Goody } from "./Goody";
11-
12-
import { LANGUAGE_NAMES } from "./constants";
6+
import { fetchGoodies } from "../fetchGoodies";
7+
import { useMergedCode } from "../useMergedCode";
8+
import { useAppState } from "../useAppState";
9+
import { Goody } from "../Goody";
10+
import { LanguageSelector } from "./LanguageSelector";
1311

1412
function Column({
1513
children,
@@ -56,19 +54,19 @@ export function App({ commitHash }: Props) {
5654
return (
5755
<div
5856
style={{
57+
alignItems: "center",
5958
display: "flex",
6059
flexDirection: "column",
61-
alignItems: "center",
6260
height: "100%",
6361
}}
6462
>
6563
<div
6664
style={{
67-
flex: "0 0 auto",
68-
display: "flex",
6965
alignItems: "center",
70-
justifyContent: "space-between",
66+
display: "flex",
67+
flex: "0 0 auto",
7168
gap: "24px",
69+
justifyContent: "space-between",
7270
width: "60%",
7371
}}
7472
>
@@ -95,23 +93,15 @@ export function App({ commitHash }: Props) {
9593
}}
9694
>
9795
<Column title="Equip Goodies" flex="0 0 auto">
98-
<select
99-
value={state.activeLanguage}
100-
onChange={(ev) =>
96+
<LanguageSelector
97+
selectedLanguage={state.activeLanguage}
98+
onChange={(language) =>
10199
dispatch({
102100
type: "select-language",
103-
language: ev.target.value as Language,
101+
language,
104102
})
105103
}
106-
>
107-
{(Object.entries(LANGUAGE_NAMES) as [Language, string][]).map(
108-
([language, languageName]) => (
109-
<option key={language} value={language}>
110-
{languageName}
111-
</option>
112-
),
113-
)}
114-
</select>
104+
/>
115105
{Object.values(goodies ?? {}).map((goody) => (
116106
<Checkbox
117107
key={goody.name}

tools/adventure-pack/src/app/GoodyCard.tsx renamed to tools/adventure-pack/src/app/components/GoodyCard.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22

3-
import type { Goody } from "./Goody";
4-
import { goodyToText } from "./goodyToText";
3+
import type { Goody } from "../Goody";
4+
import { goodyToText } from "../goodyToText";
55
import { HighlightedCode } from "./HighlightedCode";
66

77
type Props = {

tools/adventure-pack/src/app/HighlightedCode.tsx renamed to tools/adventure-pack/src/app/components/HighlightedCode.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react";
22
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
33
import { tomorrow as syntaxHighlighterStyle } from "react-syntax-highlighter/dist/esm/styles/prism";
44

5-
import type { Language } from "./Language";
5+
import type { Language } from "../Language";
66

77
// Cross-reference with https://github.com/react-syntax-highlighter/react-syntax-highlighter/blob/master/AVAILABLE_LANGUAGES_PRISM.MD
88
type HighlighterLanguage =
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from "react";
2+
3+
import { LANGUAGE_NAMES } from "../constants";
4+
import type { Language } from "../Language";
5+
6+
type Props = {
7+
selectedLanguage: Language;
8+
onChange: (language: Language) => void;
9+
};
10+
11+
export function LanguageSelector({ selectedLanguage, onChange }: Props) {
12+
return (
13+
<select
14+
value={selectedLanguage}
15+
onChange={(ev) => onChange(ev.target.value as Language)}
16+
>
17+
{(Object.entries(LANGUAGE_NAMES) as [Language, string][]).map(
18+
([language, languageName]) => (
19+
<option key={language} value={language}>
20+
{languageName}
21+
</option>
22+
),
23+
)}
24+
</select>
25+
);
26+
}

tools/adventure-pack/src/app/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import nullthrows from "nullthrows";
22
import React from "react";
33
import ReactDOM from "react-dom/client";
44

5-
import { App } from "./App";
5+
import { App } from "./components/App";
66

77
declare const ADVENTURE_PACK_COMMIT_HASH: string;
88

tools/adventure-pack/src/app/mergeCode.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import { sortTypeScriptModuleAndInterfaceDeclarations } from "./sortTypeScriptMo
1414
import { stringifyTypeScriptModuleDeclarations } from "./stringifyTypeScriptModuleDeclarations";
1515

1616
function topo({
17-
goodies,
1817
equippedGoodies,
18+
goodies,
1919
}: {
20-
goodies: ReadonlyDeep<Record<string, Goody>>;
2120
equippedGoodies: ReadonlySet<string>;
21+
goodies: ReadonlyDeep<Record<string, Goody>>;
2222
}): string[] {
2323
const pq = new BinaryHeap<string>(compareStringsCaseInsensitive);
2424

@@ -78,16 +78,16 @@ function topo({
7878

7979
export type Data = {
8080
commitHash: string;
81+
equippedGoodies: ReadonlySet<string>;
8182
goodies: ReadonlyDeep<Record<string, Goody>>;
8283
language: Language;
83-
equippedGoodies: ReadonlySet<string>;
8484
};
8585

8686
export function mergeCode({
8787
commitHash,
88+
equippedGoodies,
8889
goodies,
8990
language,
90-
equippedGoodies,
9191
}: Data): string {
9292
if (equippedGoodies.size === 0) {
9393
return language === "python3"
@@ -159,26 +159,26 @@ export function mergeCode({
159159
if (language === "java") {
160160
return (
161161
centerTextInComment({
162-
text: "BEGIN ADVENTURE PACK CODE",
163162
commentType: "//",
163+
text: "BEGIN ADVENTURE PACK CODE",
164164
}) +
165165
"\n" +
166166
`// Adventure Pack commit ${commitHash}\n` +
167167
`// Running at: ${window.location.href}\n\n` +
168168
mergedCode +
169169
"\n\n" +
170170
centerTextInComment({
171-
text: "END ADVENTURE PACK CODE",
172171
commentType: "//",
172+
text: "END ADVENTURE PACK CODE",
173173
})
174174
);
175175
}
176176

177177
if (language === "kotlin") {
178178
return (
179179
centerTextInComment({
180-
text: "BEGIN ADVENTURE PACK CODE",
181180
commentType: "//",
181+
text: "BEGIN ADVENTURE PACK CODE",
182182
}) +
183183
"\n" +
184184
`// Adventure Pack commit ${commitHash}\n` +
@@ -195,31 +195,31 @@ export function mergeCode({
195195
if (language === "python3") {
196196
return (
197197
centerTextInComment({
198-
text: "BEGIN ADVENTURE PACK CODE",
199198
commentType: "#",
199+
text: "BEGIN ADVENTURE PACK CODE",
200200
}) +
201201
"\n" +
202202
`# Adventure Pack commit ${commitHash}\n` +
203203
`# Running at: ${window.location.href}\n\n` +
204204
mergedCode +
205205
"\n\n" +
206-
centerTextInComment({ text: "END ADVENTURE PACK CODE", commentType: "#" })
206+
centerTextInComment({ commentType: "#", text: "END ADVENTURE PACK CODE" })
207207
);
208208
}
209209

210210
return (
211211
centerTextInComment({
212-
text: "BEGIN ADVENTURE PACK CODE",
213212
commentType: "//",
213+
text: "BEGIN ADVENTURE PACK CODE",
214214
}) +
215215
"\n" +
216216
`// Adventure Pack commit ${commitHash}\n` +
217217
`// Running at: ${window.location.href}\n\n` +
218218
mergedCode.replaceAll(/^export\s+/gm, "") +
219219
"\n\n" +
220220
centerTextInComment({
221-
text: "END ADVENTURE PACK CODE",
222221
commentType: "//",
222+
text: "END ADVENTURE PACK CODE",
223223
})
224224
).trim();
225225
}

0 commit comments

Comments
 (0)