mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-26 08:33:12 +09:00
Refactor to avoid duplicated code
This commit is contained in:
1 parent
1f5f28c820
commit
c6f10e2195
1 file changed
+20
-45
@@ -28,6 +28,9 @@ const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base"
|
|||||||
// Define the allowed Factorio versions based on the lib schema
|
// Define the allowed Factorio versions based on the lib schema
|
||||||
type FactorioVersion = Static<(typeof lib.ModPortalGetAllRequest)["allowedVersions"][number]>;
|
type FactorioVersion = Static<(typeof lib.ModPortalGetAllRequest)["allowedVersions"][number]>;
|
||||||
|
|
||||||
|
// Type alias for the mod structure derived from the library response
|
||||||
|
type ModPortalModType = InstanceType<typeof lib.ModPortalGetAllRequest.Response>["mods"][number];
|
||||||
|
|
||||||
function ImportModPackButton() {
|
function ImportModPackButton() {
|
||||||
let control = useContext(ControlContext);
|
let control = useContext(ControlContext);
|
||||||
let navigate = useNavigate();
|
let navigate = useNavigate();
|
||||||
@@ -136,27 +139,6 @@ function CreateModPackButton() {
|
|||||||
</>;
|
</>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define a basic type for the mod data we expect back from the new backend request
|
|
||||||
// This should ideally match the structure returned by the backend.
|
|
||||||
// We reuse the ModPortalMod structure name for convenience, but it's fetched via backend.
|
|
||||||
interface ModPortalMod {
|
|
||||||
name: string;
|
|
||||||
title: string;
|
|
||||||
owner: string;
|
|
||||||
summary: string;
|
|
||||||
downloads_count: number;
|
|
||||||
category: string;
|
|
||||||
score?: number;
|
|
||||||
latest_release?: {
|
|
||||||
download_url: string;
|
|
||||||
file_name: string;
|
|
||||||
info_json: { factorio_version: string; };
|
|
||||||
released_at: string;
|
|
||||||
version: string;
|
|
||||||
sha1: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function SearchModsButton() {
|
function SearchModsButton() {
|
||||||
const control = useContext(ControlContext);
|
const control = useContext(ControlContext);
|
||||||
const account = useAccount();
|
const account = useAccount();
|
||||||
@@ -166,7 +148,7 @@ function SearchModsButton() {
|
|||||||
const [factorioVersion, setFactorioVersion] = useState<FactorioVersion>("2.0");
|
const [factorioVersion, setFactorioVersion] = useState<FactorioVersion>("2.0");
|
||||||
|
|
||||||
// State for all mods fetched from backend
|
// State for all mods fetched from backend
|
||||||
const [allMods, setAllMods] = useState<ModPortalMod[]>([]);
|
const [allMods, setAllMods] = useState<ModPortalModType[]>([]);
|
||||||
const [loading, setLoading] = useState<boolean>(false);
|
const [loading, setLoading] = useState<boolean>(false);
|
||||||
const [error, setError] = useState<Error | null>(null);
|
const [error, setError] = useState<Error | null>(null);
|
||||||
|
|
||||||
@@ -214,14 +196,11 @@ function SearchModsButton() {
|
|||||||
setError(null);
|
setError(null);
|
||||||
let canceled = false;
|
let canceled = false;
|
||||||
|
|
||||||
// Use the hypothetical new backend request
|
|
||||||
control.send(
|
control.send(
|
||||||
// Factorio version state now matches the required type
|
|
||||||
new lib.ModPortalGetAllRequest(factorioVersion)
|
new lib.ModPortalGetAllRequest(factorioVersion)
|
||||||
).then((response: any) => {
|
).then((response: any) => {
|
||||||
if (canceled) { return; }
|
if (canceled) { return; }
|
||||||
// *** Backend Change Needed: Ensure the response has a 'mods' array ***
|
setAllMods(response.mods || []);
|
||||||
setAllMods(response?.mods || []); // Added optional chaining
|
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
if (canceled) { return; }
|
if (canceled) { return; }
|
||||||
@@ -254,8 +233,8 @@ function SearchModsButton() {
|
|||||||
// Apply sorting
|
// Apply sorting
|
||||||
if (sort && sortOrder) {
|
if (sort && sortOrder) {
|
||||||
filtered = [...filtered].sort((a, b) => {
|
filtered = [...filtered].sort((a, b) => {
|
||||||
let aValue: any = a[sort as keyof ModPortalMod] ?? "";
|
let aValue: any = a[sort as keyof ModPortalModType] ?? "";
|
||||||
let bValue: any = b[sort as keyof ModPortalMod] ?? "";
|
let bValue: any = b[sort as keyof ModPortalModType] ?? "";
|
||||||
|
|
||||||
// Handle specific types if necessary (e.g., numbers, dates)
|
// Handle specific types if necessary (e.g., numbers, dates)
|
||||||
if (sort === "downloads_count") {
|
if (sort === "downloads_count") {
|
||||||
@@ -354,7 +333,6 @@ function SearchModsButton() {
|
|||||||
<Button key="close" onClick={() => { setOpen(false); }}>
|
<Button key="close" onClick={() => { setOpen(false); }}>
|
||||||
Close
|
Close
|
||||||
</Button>,
|
</Button>,
|
||||||
// Remove explicit search button as form updates trigger search/filter
|
|
||||||
]}
|
]}
|
||||||
>
|
>
|
||||||
<Form
|
<Form
|
||||||
@@ -373,16 +351,14 @@ function SearchModsButton() {
|
|||||||
label="Factorio Version"
|
label="Factorio Version"
|
||||||
>
|
>
|
||||||
<Select>
|
<Select>
|
||||||
{/* Map allowed versions to Select Options */}
|
{lib.ModPortalGetAllRequest.allowedVersions.map((literalSchema: any) => (
|
||||||
<Select.Option value="2.0">2.0</Select.Option>
|
<Select.Option
|
||||||
<Select.Option value="1.1">1.1</Select.Option>
|
key={literalSchema.const}
|
||||||
<Select.Option value="1.0">1.0</Select.Option>
|
value={literalSchema.const}
|
||||||
<Select.Option value="0.18">0.18</Select.Option>
|
>
|
||||||
<Select.Option value="0.17">0.17</Select.Option>
|
{literalSchema.const}
|
||||||
<Select.Option value="0.16">0.16</Select.Option>
|
</Select.Option>
|
||||||
<Select.Option value="0.15">0.15</Select.Option>
|
)).reverse()}
|
||||||
<Select.Option value="0.14">0.14</Select.Option>
|
|
||||||
<Select.Option value="0.13">0.13</Select.Option>
|
|
||||||
</Select>
|
</Select>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
</Form>
|
</Form>
|
||||||
@@ -431,14 +407,13 @@ function SearchModsButton() {
|
|||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
) : <p>No release information available.</p>}
|
) : <p>No release information available.</p>}
|
||||||
{/* Download button logic remains the same, assuming backend handles downloads */}
|
{account.hasPermission("core.mod.download_from_portal") && record.latest_release && (
|
||||||
{account.hasPermission("core.mod.download") && record.latest_release && (
|
|
||||||
<Button
|
<Button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
handleControllerDownload(
|
handleControllerDownload(
|
||||||
record.name,
|
record.name,
|
||||||
record.title,
|
record.title,
|
||||||
record.latest_release?.version,
|
record.latest_release!.version,
|
||||||
factorioVersion
|
factorioVersion
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
@@ -507,17 +482,17 @@ function SearchModsButton() {
|
|||||||
</Button>
|
</Button>
|
||||||
</a>
|
</a>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
{account.hasPermission("core.mod.download") && record.latest_release && (
|
{account.hasPermission("core.mod.download_from_portal") && record.latest_release && (
|
||||||
<Tooltip title="Download to Controller">
|
<Tooltip title="Download to Controller">
|
||||||
<Button
|
<Button
|
||||||
type="text"
|
type="text"
|
||||||
icon={<DownloadOutlined />}
|
icon={<DownloadOutlined />}
|
||||||
disabled={!record.latest_release?.version}
|
disabled={!record.latest_release.version}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
handleControllerDownload(
|
handleControllerDownload(
|
||||||
record.name,
|
record.name,
|
||||||
record.title,
|
record.title,
|
||||||
record.latest_release?.version,
|
record.latest_release!.version,
|
||||||
factorioVersion
|
factorioVersion
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
|
|||||||
Reference in new issue
Block a user