2026-12-04 18:58:31 -08:00
|
|
|
import { useCallback, useEffect, useState } from 'react';
|
|
|
|
|
import { Button, Result, Spin } from 'antd';
|
|
|
|
|
import type { LedgerClient } from './lib/api';
|
|
|
|
|
import { loadSession, loadState } from './lib/session';
|
|
|
|
|
import { navigate, useRoute } from './lib/router';
|
|
|
|
|
import type { LedgerState, SessionInfo } from './shared';
|
|
|
|
|
import { LoginPage } from './pages/Login/LoginPage';
|
|
|
|
|
import { CreateOrderPage } from './pages/CreateOrder/CreateOrderPage';
|
2026-12-04 20:07:46 -08:00
|
|
|
import { AppShell } from './components/AppShell';
|
2026-12-04 18:58:31 -08:00
|
|
|
import { RequestStatus } from './components/RequestStatus';
|
|
|
|
|
|
|
|
|
|
export function App({ client }: { client: LedgerClient }) {
|
|
|
|
|
const [session, setSession] = useState<SessionInfo | null>(null);
|
|
|
|
|
const [state, setState] = useState<LedgerState | null>(null);
|
|
|
|
|
const [error, setError] = useState('');
|
|
|
|
|
const route = useRoute();
|
|
|
|
|
|
|
|
|
|
// 重新读取登录状态:登录、退出、账号切换后调用。
|
|
|
|
|
const [initRequest, setInitRequest] = useState(0);
|
|
|
|
|
const init = useCallback(() => setInitRequest(value => value + 1), []);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let current = true;
|
|
|
|
|
loadSession(client)
|
|
|
|
|
.then(async latest => {
|
|
|
|
|
if (!current) return;
|
|
|
|
|
setError('');
|
|
|
|
|
setSession(latest);
|
|
|
|
|
if (!latest.user) {
|
|
|
|
|
setState(null);
|
|
|
|
|
navigate('login', { keepSearch: true });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const next = await loadState(client);
|
|
|
|
|
if (!current) return;
|
|
|
|
|
setState(next);
|
|
|
|
|
navigate('create', { keepSearch: true });
|
|
|
|
|
})
|
|
|
|
|
.catch(err => current && setError((err as Error).message));
|
|
|
|
|
return () => {
|
|
|
|
|
current = false;
|
|
|
|
|
};
|
|
|
|
|
}, [client, initRequest]);
|
|
|
|
|
const refreshState = useCallback(async () => {
|
|
|
|
|
const latest = await loadState(client);
|
|
|
|
|
setState(latest);
|
|
|
|
|
return latest;
|
|
|
|
|
}, [client]);
|
|
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
|
return (
|
2026-12-04 20:07:46 -08:00
|
|
|
<div className="page-center">
|
|
|
|
|
<Result
|
2026-12-04 18:58:31 -08:00
|
|
|
status="warning"
|
|
|
|
|
title="系统暂时没有连接上"
|
|
|
|
|
subTitle={error + ' 请保持启动窗口打开,再刷新页面。'}
|
|
|
|
|
extra={
|
|
|
|
|
<Button type="primary" onClick={init}>
|
|
|
|
|
重新连接
|
|
|
|
|
</Button>
|
|
|
|
|
}
|
2026-12-04 20:07:46 -08:00
|
|
|
/>
|
|
|
|
|
</div>
|
2026-12-04 18:58:31 -08:00
|
|
|
);
|
|
|
|
|
if (!session)
|
|
|
|
|
return (
|
|
|
|
|
<div className="page-loading">
|
|
|
|
|
<Spin tip="正在读取登录状态…" size="large">
|
|
|
|
|
<div style={{ height: 80 }} />
|
|
|
|
|
</Spin>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
if (!session.user || route === 'login' && !state)
|
|
|
|
|
return <LoginPage client={client} session={session} onLoggedIn={init} />;
|
|
|
|
|
if (!state)
|
|
|
|
|
return (
|
|
|
|
|
<div className="page-loading">
|
|
|
|
|
<Spin tip="正在读取店铺资料…" size="large">
|
|
|
|
|
<div style={{ height: 80 }} />
|
|
|
|
|
</Spin>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
return (
|
2026-12-04 20:07:46 -08:00
|
|
|
<AppShell client={client} state={state} session={session} onLoggedOut={init}>
|
2026-12-04 18:58:31 -08:00
|
|
|
<RequestStatus client={client} />
|
2026-12-04 20:07:46 -08:00
|
|
|
<CreateOrderPage
|
|
|
|
|
key={state.user.id}
|
|
|
|
|
client={client}
|
|
|
|
|
state={state}
|
|
|
|
|
session={session}
|
|
|
|
|
refreshState={refreshState}
|
|
|
|
|
onAccountChanged={init}
|
|
|
|
|
/>
|
|
|
|
|
</AppShell>
|
2026-12-04 18:58:31 -08:00
|
|
|
);
|
|
|
|
|
}
|