-
-
Notifications
You must be signed in to change notification settings - Fork 12.2k
/
Copy pathUpgradeButton.tsx
74 lines (59 loc) · 2.11 KB
/
UpgradeButton.tsx
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
import { Button } from 'antd';
import { createStore, set } from 'idb-keyval';
import { ReactNode, memo } from 'react';
import { useTranslation } from 'react-i18next';
import { Migration } from '@/migrations';
import { importService } from '@/services/import';
import { useChatStore } from '@/store/chat';
import { useSessionStore } from '@/store/session';
import { MIGRATE_KEY, MigrationError, UpgradeStatus, V1DB_NAME, V1DB_TABLE_NAME } from './const';
export interface UpgradeButtonProps {
children?: ReactNode;
primary?: boolean;
setError: (error: MigrationError) => void;
setUpgradeStatus: (status: UpgradeStatus) => void;
state: any;
upgradeStatus: UpgradeStatus;
}
const UpgradeButton = memo<UpgradeButtonProps>(
({ setUpgradeStatus, upgradeStatus, state, setError, primary = true, children }) => {
const { t } = useTranslation('migration');
const refreshSession = useSessionStore((s) => s.refreshSessions);
const [refreshMessages, refreshTopic] = useChatStore((s) => [
s.refreshMessages,
s.refreshTopic,
]);
const upgrade = async () => {
try {
const data = Migration.migrate({ state, version: 1 });
setUpgradeStatus(UpgradeStatus.UPGRADING);
await importService.importConfigState({
exportType: 'sessions',
state: data.state,
version: 2,
});
await refreshSession();
await refreshMessages();
await refreshTopic();
await set(MIGRATE_KEY, true, createStore(V1DB_NAME, V1DB_TABLE_NAME));
setUpgradeStatus(UpgradeStatus.UPGRADED);
return { success: true };
} catch (error) {
setUpgradeStatus(UpgradeStatus.UPGRADE_FAILED);
const err = error as { message: string; stack: string };
setError({ message: err.message, stack: err.stack });
}
};
return (
<Button
loading={upgradeStatus === UpgradeStatus.UPGRADING}
onClick={upgrade}
size={'large'}
type={primary ? 'primary' : undefined}
>
{children ?? t('dbV1.action.upgrade')}
</Button>
);
},
);
export default UpgradeButton;