-
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathSidebar.tsx
More file actions
159 lines (148 loc) · 5.8 KB
/
Sidebar.tsx
File metadata and controls
159 lines (148 loc) · 5.8 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
import GameElement from "./GameElement";
import styles from "./sidebar.module.scss";
import {useState} from "react";
import {
Button, Dropdown,
Input,
Option,
Popover,
PopoverSurface,
PopoverTrigger,
Select,
Subtitle1
} from "@fluentui/react-components";
import {AddFilled, AddRegular, ArrowSyncFilled, ArrowSyncRegular, bundleIcon} from "@fluentui/react-icons";
import {t} from "@lingui/macro";
import useSWR from "swr";
import {api} from "@/api";
import {CreateGameDto, GameInfoDto} from "@/api/Api";
import normalizeFileName from "@/utils/normalizeFileName";
interface ISidebarProps {
gameList: GameInfoDto[];
currentSetGame: string | null;
setCurrentGame: (currentGame: string) => void;
createGame: (createGameData: CreateGameDto) => void;
refreash?: () => void;
}
const AddIcon = bundleIcon(AddFilled, AddRegular);
const ArrowSyncIcon = bundleIcon(ArrowSyncFilled, ArrowSyncRegular);
export default function Sidebar(props: ISidebarProps) {
const [createGameFormOpen, setCreateGameFormOpen] = useState(false);
const [gameName, setGameName] = useState(t`新的游戏`);
const [gameDir, setGameDir] = useState(t`新的游戏`);
const [derivative, setDerivative] = useState<string | undefined>(undefined);
const [templateName, setTemplateName] = useState<string | undefined>(undefined);
// 可用的衍生版
const derivativeEnginesResp = useSWR('derivativeEngines', async () => {
const resp = await api.manageGameControllerGetDerivativeEngines();
return resp.data as unknown as string[];
});
const templatesResp = useSWR('template-list-selector', async () => {
const resp = await api.manageTemplateControllerGetTemplateList();
return resp.data as unknown as { name: string; dir: string }[];
});
const defaultDerivativeValue = 'WebGAL Standard';
const defaultTemplateValue = 'WebGAL Refine 2026';
const selector = <Dropdown value={derivative ?? t`WebGAL Standard`}
selectedOptions={[derivative ?? defaultDerivativeValue]} onOptionSelect={(_, elem) => {
setDerivative(elem.optionValue === defaultDerivativeValue ? undefined : elem.optionValue);
}}>
<Option key="default-engine" value={defaultDerivativeValue}>{t`WebGAL Standard`}</Option>
{(derivativeEnginesResp.data ?? []).map(e =>
<Option key={e} value={e}>{e}</Option>
)}
</Dropdown>;
const selectorTemplate = <Dropdown value={templateName ?? defaultTemplateValue}
selectedOptions={[templateName ?? defaultTemplateValue]}
onOptionSelect={(_, elem) => {
setTemplateName(elem.optionValue === defaultTemplateValue ? undefined : elem.optionValue);
}}>
<Option key="default-template" value={defaultTemplateValue}>{defaultTemplateValue}</Option>
{(templatesResp.data ?? []).map(e =>
<Option key={e.dir} value={e.dir}>{e.name}</Option>
)}
</Dropdown>;
function createNewGame() {
if (gameName.trim() !== '' && gameDir.trim() !== '' && !props.gameList.find((item) => item.dir === gameDir.trim())) {
props.createGame({
gameName: gameName.trim(),
gameDir,
derivative,
templateDir: templateName,
});
setCreateGameFormOpen(false);
setGameName(t`新的游戏`);
}
}
return <div className={`${styles.sidebar_main} ${!props.currentSetGame ? styles.sidebar_main_fullwidth : ""}`}>
<div className={styles.sidebar_top}>
<span className={styles.sidebar_top_title}>{t`游戏列表`}</span>
<div className={styles.sidebar_top_buttons}>
<Popover
withArrow
trapFocus
open={createGameFormOpen}
onOpenChange={() => setCreateGameFormOpen(!createGameFormOpen)}
>
<PopoverTrigger>
<Button appearance='primary' icon={<AddIcon/>}>{t`新建游戏`}</Button>
</PopoverTrigger>
<PopoverSurface>
<form style={{display: "flex", flexDirection: "column", gap: '16px'}}>
<Subtitle1>{t`创建新游戏`}</Subtitle1>
{t`游戏名称`}
<Input
value={gameName}
onChange={(event) => {
setGameName(event.target.value);
gameDir === normalizeFileName(gameName) && setGameDir(normalizeFileName(event.target.value));
}}
onKeyDown={(event) => (event.key === 'Enter') && createNewGame()}
defaultValue={t`新的游戏`}
placeholder={t`游戏名称`}
/>
{t`游戏目录`}
<Input
value={gameDir}
onChange={(event) => setGameDir(event.target.value)}
onKeyDown={(event) => (event.key === 'Enter') && createNewGame()}
defaultValue={gameDir}
placeholder={t`游戏目录`}
/>
{t`选择游戏引擎版本`}
{selector}
{t`选择应用的模板`}
{selectorTemplate}
<Button
appearance='primary'
disabled={
gameName.trim() === ''
|| gameDir.trim() === ''
|| props.gameList.find((item) => item.dir === gameDir.trim()) !== undefined
}
onClick={createNewGame}
>
{t`创建`}
</Button>
</form>
</PopoverSurface>
</Popover>
<Button appearance='secondary' onClick={props.refreash} icon={<ArrowSyncIcon/>}/>
</div>
</div>
<div className={styles.game_list}>
{
props.gameList.map(e => {
const checked = props.currentSetGame === e.dir;
return <GameElement
onClick={() => props.setCurrentGame(e.dir)}
refreash={props.refreash}
gameInfo={e}
key={e.dir}
checked={checked}
/>;
})
}
</div>
</div>;
}