Skip to content

Commit 51b0a08

Browse files
committed
Factor out language selector into its own component
1 parent 6da5e9b commit 51b0a08

File tree

19 files changed

+79
-50
lines changed

19 files changed

+79
-50
lines changed

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
@@ -12,11 +12,11 @@ import { mergeJavaCode } from "./mergeJavaCode";
1212
import type { JavaGoody } from "./parsers/javaGoodyParser";
1313

1414
function topo({
15-
goodies,
1615
equippedGoodies,
16+
goodies,
1717
}: {
18-
goodies: ReadonlyDeep<Record<string, Goody>>;
1918
equippedGoodies: ReadonlySet<string>;
19+
goodies: ReadonlyDeep<Record<string, Goody>>;
2020
}): string[] {
2121
const pq = new BinaryHeap<string>(compareStringsCaseInsensitive);
2222

@@ -76,16 +76,16 @@ function topo({
7676

7777
export type Data = {
7878
commitHash: string;
79+
equippedGoodies: ReadonlySet<string>;
7980
goodies: ReadonlyDeep<Record<string, Goody>>;
8081
language: Language;
81-
equippedGoodies: ReadonlySet<string>;
8282
};
8383

8484
export function mergeCode({
8585
commitHash,
86+
equippedGoodies,
8687
goodies,
8788
language,
88-
equippedGoodies,
8989
}: Data): string {
9090
if (equippedGoodies.size === 0) {
9191
return language === "python3"
@@ -139,26 +139,26 @@ export function mergeCode({
139139
if (language === "java") {
140140
return (
141141
centerTextInComment({
142-
text: "BEGIN ADVENTURE PACK CODE",
143142
commentType: "//",
143+
text: "BEGIN ADVENTURE PACK CODE",
144144
}) +
145145
"\n" +
146146
`// Adventure Pack commit ${commitHash}\n` +
147147
`// Running at: ${window.location.href}\n\n` +
148148
mergedCode +
149149
"\n\n" +
150150
centerTextInComment({
151-
text: "END ADVENTURE PACK CODE",
152151
commentType: "//",
152+
text: "END ADVENTURE PACK CODE",
153153
})
154154
);
155155
}
156156

157157
if (language === "kotlin") {
158158
return (
159159
centerTextInComment({
160-
text: "BEGIN ADVENTURE PACK CODE",
161160
commentType: "//",
161+
text: "BEGIN ADVENTURE PACK CODE",
162162
}) +
163163
"\n" +
164164
`// Adventure Pack commit ${commitHash}\n` +
@@ -175,31 +175,31 @@ export function mergeCode({
175175
if (language === "python3") {
176176
return (
177177
centerTextInComment({
178-
text: "BEGIN ADVENTURE PACK CODE",
179178
commentType: "#",
179+
text: "BEGIN ADVENTURE PACK CODE",
180180
}) +
181181
"\n" +
182182
`# Adventure Pack commit ${commitHash}\n` +
183183
`# Running at: ${window.location.href}\n\n` +
184184
mergedCode +
185185
"\n\n" +
186-
centerTextInComment({ text: "END ADVENTURE PACK CODE", commentType: "#" })
186+
centerTextInComment({ commentType: "#", text: "END ADVENTURE PACK CODE" })
187187
);
188188
}
189189

190190
return (
191191
centerTextInComment({
192-
text: "BEGIN ADVENTURE PACK CODE",
193192
commentType: "//",
193+
text: "BEGIN ADVENTURE PACK CODE",
194194
}) +
195195
"\n" +
196196
`// Adventure Pack commit ${commitHash}\n` +
197197
`// Running at: ${window.location.href}\n\n` +
198198
mergedCode.replaceAll(/^export\s+/gm, "") +
199199
"\n\n" +
200200
centerTextInComment({
201-
text: "END ADVENTURE PACK CODE",
202201
commentType: "//",
202+
text: "END ADVENTURE PACK CODE",
203203
})
204204
).trim();
205205
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function mergeJavaCode(goodies: Iterable<ReadonlyDeep<JavaGoody>>) {
1515
`Only the ${ADVENTURE_PACK_CLASS_NAME} class can exist in multiple goodies!`,
1616
);
1717

18-
classes[className] ??= { modifiers: new Set(), code: [] };
18+
classes[className] ??= { code: [], modifiers: new Set() };
1919
for (const modifier of goody.codeByClass[className].modifiers) {
2020
classes[className].modifiers.add(modifier);
2121
}

0 commit comments

Comments
 (0)