Skip to content

Commit 2fe8e07

Browse files
committed
中期草案commit
1 parent 3bf890d commit 2fe8e07

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

src/pages/install/App.tsx

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { type FTInfo, startFileTrack, unmountFileTrack } from "@App/pkg/utils/fi
2929
import { cleanupOldHandles, loadHandle, saveHandle } from "@App/pkg/utils/filehandle-db";
3030
import { dayFormat } from "@App/pkg/utils/day_format";
3131
import { intervalExecution, timeoutExecution } from "@App/pkg/utils/timer";
32+
import { useSearchParams } from "react-router-dom";
3233

3334
type ScriptOrSubscribe = Script | Subscribe;
3435

@@ -58,6 +59,8 @@ function App() {
5859
const [isUpdate, setIsUpdate] = useState<boolean>(false);
5960
const [localFileHandle, setLocalFileHandle] = useState<FileSystemFileHandle | null>(null);
6061
const { t } = useTranslation();
62+
const [searchParams, setSearchParams] = useSearchParams();
63+
const [loaded, setLoaded] = useState<boolean>(false);
6164

6265
const installOrUpdateScript = async (newScript: Script, code: string) => {
6366
if (newScript.ignoreVersion) newScript.ignoreVersion = "";
@@ -84,10 +87,16 @@ function App() {
8487

8588
const initAsync = async () => {
8689
try {
87-
const locationUrl = new URL(window.location.href);
88-
const uuid = locationUrl.searchParams.get("uuid");
90+
const uuid = searchParams.get("uuid");
91+
const fid = searchParams.get("file");
8992
let info: ScriptInfo | undefined;
9093
let isKnownUpdate: boolean = false;
94+
95+
// 如果没有 uuid 和 file,跳过初始化逻辑
96+
if (!uuid && !fid) {
97+
return;
98+
}
99+
91100
if (uuid) {
92101
const cachedInfo = await scriptClient.getInstallInfo(uuid);
93102
if (cachedInfo?.[0]) isKnownUpdate = true;
@@ -97,7 +106,6 @@ function App() {
97106
}
98107
} else {
99108
// 检查是不是本地文件安装
100-
const fid = locationUrl.searchParams.get("file");
101109
if (!fid) {
102110
throw new Error("url param - local file id is not found");
103111
}
@@ -152,6 +160,7 @@ function App() {
152160
}
153161
diffCode = prepare.oldScriptCode;
154162
}
163+
setLoaded(true);
155164
setScriptCode(code);
156165
setDiffCode(diffCode);
157166
setOldScriptVersion(typeof oldVersion === "string" ? oldVersion : null);
@@ -171,8 +180,8 @@ function App() {
171180
};
172181

173182
useEffect(() => {
174-
initAsync();
175-
}, []);
183+
!loaded && initAsync();
184+
}, [searchParams, loaded]);
176185

177186
const [watchFile, setWatchFile] = useState(false);
178187
const metadataLive = useMemo(() => (scriptInfo?.metadata || {}) as SCMetadata, [scriptInfo]);
@@ -464,6 +473,33 @@ function App() {
464473
};
465474
}, [memoWatchFile]);
466475

476+
// 处理没有 uuid 和 file 的情况
477+
// const handleCreateNewScript = () => {
478+
// const newUuid = uuidv4();
479+
// setSearchParams({ uuid: newUuid }, { replace: true });
480+
// };
481+
482+
// 检查是否有 uuid 或 file
483+
const hasParams = useMemo(() => {
484+
return !!(searchParams.get("uuid") || searchParams.get("file"));
485+
}, [searchParams]);
486+
487+
// 没有 uuid 和 file 时显示的页面
488+
if (!hasParams) {
489+
return (
490+
<div className="flex justify-center items-center h-screen">
491+
<Space direction="vertical" align="center">
492+
<Typography.Title heading={3}>{t("no_script_selected")}</Typography.Title>
493+
{/*
494+
<Button type="primary" onClick={handleCreateNewScript}>
495+
{t("create_new_script")}
496+
</Button>
497+
*/}
498+
</Space>
499+
</div>
500+
);
501+
}
502+
467503
return (
468504
<div id="install-app-container">
469505
<Grid.Row className="mb-2" gutter={8}>

src/pages/install/main.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import "@App/locales/locales";
1111
import "@App/index.css";
1212
import "./index.css";
1313
import registerEditor from "@App/pkg/utils/monaco-editor";
14+
import { BrowserRouter, Route, Routes } from "react-router-dom";
1415

1516
registerEditor();
1617

@@ -22,13 +23,20 @@ const loggerCore = new LoggerCore({
2223

2324
loggerCore.logger().debug("install page start");
2425

25-
const Root = (
26+
const MyApp = () => (
2627
<AppProvider>
2728
<MainLayout className="!flex-col !px-4 box-border install-main-layout">
2829
<App />
2930
</MainLayout>
3031
</AppProvider>
3132
);
33+
const Root = (
34+
<BrowserRouter>
35+
<Routes>
36+
<Route path="/*" element={<MyApp />} />
37+
</Routes>
38+
</BrowserRouter>
39+
);
3240

3341
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
3442
process.env.NODE_ENV === "development" ? <React.StrictMode>{Root}</React.StrictMode> : Root

0 commit comments

Comments
 (0)