Files
web2app/web/src/pages/Upload.tsx

96 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { FormEvent, useState } from "react";
import { useNavigate } from "react-router-dom";
import { createBuild } from "../api";
export default function Upload() {
const navigate = useNavigate();
const [appNameZh, setAppNameZh] = useState("");
const [appNameEn, setAppNameEn] = useState("");
const [identifier, setIdentifier] = useState("");
const [file, setFile] = useState<File | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
async function onSubmit(event: FormEvent) {
event.preventDefault();
if (!file) {
setError("请选择 zip 文件");
return;
}
setLoading(true);
setError(null);
try {
const formData = new FormData();
formData.append("file", file);
formData.append("appNameZh", appNameZh);
formData.append("appNameEn", appNameEn);
if (identifier.trim()) {
formData.append("identifier", identifier.trim());
}
const result = await createBuild(formData);
navigate(`/jobs/${result.id}`);
} catch (err) {
setError(err instanceof Error ? err.message : "上传失败");
} finally {
setLoading(false);
}
}
return (
<section className="card">
<h2></h2>
<p className="hint">
index.html 50MB zip logo.png
favicon.ico logo.png
Bundle ID
</p>
<form className="form" onSubmit={onSubmit}>
<label>
<input
value={appNameZh}
onChange={(e) => setAppNameZh(e.target.value)}
placeholder="我的应用"
required
/>
</label>
<label>
<input
value={appNameEn}
onChange={(e) => setAppNameEn(e.target.value)}
placeholder="My App"
required
pattern="[a-zA-Z][a-zA-Z0-9 _.-]*"
title="以字母开头,仅含英文字母、数字、空格、下划线和连字符"
/>
</label>
<label>
Bundle ID
<input
value={identifier}
onChange={(e) => setIdentifier(e.target.value)}
placeholder="com.example.myapp"
/>
</label>
<label>
zip
<input
type="file"
accept=".zip,application/zip"
onChange={(e) => setFile(e.target.files?.[0] ?? null)}
required
/>
</label>
{error ? <p className="error">{error}</p> : null}
<button type="submit" disabled={loading}>
{loading ? "上传并触发构建..." : "开始构建"}
</button>
</form>
</section>
);
}