forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryBackupPage.tsx
More file actions
209 lines (191 loc) · 7.05 KB
/
LibraryBackupPage.tsx
File metadata and controls
209 lines (191 loc) · 7.05 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import {
Alert,
Button,
Container,
} from '@openedx/paragon';
import {
useCallback,
useEffect,
useRef,
useState,
} from 'react';
import { Helmet } from 'react-helmet';
import { useIntl } from '@edx/frontend-platform/i18n';
import { Download, Loop, Newsstand } from '@openedx/paragon/icons';
import NotFoundAlert from '@src/generic/NotFoundAlert';
import SubHeader from '@src/generic/sub-header/SubHeader';
import Header from '@src/header';
import { LibraryBackupStatus } from '@src/library-authoring/backup-restore/data/constants';
import { useCreateLibraryBackup, useGetLibraryBackupStatus } from '@src/library-authoring/backup-restore/data/hooks';
import messages from '@src/library-authoring/backup-restore/messages';
import { useLibraryContext } from '@src/library-authoring/common/context/LibraryContext';
import { useContentLibrary } from '@src/library-authoring/data/apiHooks';
export const LibraryBackupPage = () => {
const intl = useIntl();
const { libraryId, readOnly } = useLibraryContext();
const [taskId, setTaskId] = useState<string>('');
const [isMutationInProgress, setIsMutationInProgress] = useState<boolean>(false);
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
const { data: libraryData } = useContentLibrary(libraryId);
const mutation = useCreateLibraryBackup(libraryId);
const backupStatus = useGetLibraryBackupStatus(libraryId, taskId);
// Clean up timeout on unmount
useEffect(() => () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
}, []);
const handleDownload = useCallback((url: string) => {
try {
// Create a temporary anchor element for better download handling
const link = document.createElement('a');
link.href = url;
link.download = `${libraryData?.slug || 'library'}-backup.tar.gz`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} catch (error) {
// Fallback to window.location.href if the above fails
window.location.href = url;
}
}, [libraryData?.slug]);
const handleDownloadBackup = useCallback(() => {
// If backup is ready, download it immediately
if (backupStatus.data?.state === LibraryBackupStatus.Succeeded && backupStatus.data.url) {
handleDownload(backupStatus.data.url);
return;
}
// If no backup in progress, create a new one
if (!taskId) {
setIsMutationInProgress(true);
mutation.mutate(undefined, {
onSuccess: (data) => {
setTaskId(data.task_id);
// Clear task id after 1 minutes to allow new backups
timeoutRef.current = setTimeout(() => {
setTaskId('');
setIsMutationInProgress(false);
timeoutRef.current = null;
}, 60 * 1000);
},
onError: () => {
setIsMutationInProgress(false);
},
});
}
}, [taskId, backupStatus.data, mutation, handleDownload]);
// Auto-download when backup becomes ready
useEffect(() => {
if (backupStatus.data?.state === LibraryBackupStatus.Succeeded && backupStatus.data.url) {
handleDownload(backupStatus.data.url);
setIsMutationInProgress(false);
}
}, [backupStatus.data?.state, backupStatus.data?.url, handleDownload]);
// Reset mutation progress when backup fails
useEffect(() => {
if (backupStatus.data?.state === LibraryBackupStatus.Failed) {
setIsMutationInProgress(false);
}
}, [backupStatus.data?.state]);
const backupState = backupStatus.data?.state;
const isBackupInProgress = isMutationInProgress || (taskId && (
backupState === LibraryBackupStatus.Pending
|| backupState === LibraryBackupStatus.Exporting
));
const hasBackupFailed = backupState === LibraryBackupStatus.Failed;
const hasBackupSucceeded = backupState === LibraryBackupStatus.Succeeded;
// Show error message for failed mutation
const mutationError = mutation.error as Error | null;
if (!libraryData) {
return <NotFoundAlert />;
}
const getButtonText = () => {
if (isBackupInProgress) {
if (isMutationInProgress && !backupState) {
return intl.formatMessage(messages.backupPending);
}
return backupState === LibraryBackupStatus.Pending
? intl.formatMessage(messages.backupPending) : intl.formatMessage(messages.backupExporting);
}
if (hasBackupSucceeded && backupStatus.data?.url) {
return intl.formatMessage(messages.downloadReadyButton);
}
return intl.formatMessage(messages.createBackupButton);
};
const getButtonIcon = () => {
if (isBackupInProgress) {
return Loop;
}
return Download;
};
return (
<div className="d-flex">
<div className="flex-grow-1">
<Helmet>
<title>{libraryData.title} | {process.env.SITE_NAME}</title>
</Helmet>
<Header
number={libraryData.slug}
title={libraryData.title}
org={libraryData.org}
contextId={libraryId}
readOnly={readOnly}
isLibrary
containerProps={{
size: undefined,
}}
/>
<Container className="px-0 mt-4 mb-5 library-authoring-page bg-white">
<div className="px-4 bg-light-200 border-bottom mb-2">
<SubHeader
title={intl.formatMessage(messages.backupPageTitle)}
subtitle={intl.formatMessage(messages.backupPageSubtitle)}
hideBorder
/>
</div>
{/* Error Messages */}
{hasBackupFailed && (
<div className="px-4">
<Alert variant="danger">
{intl.formatMessage(messages.backupFailedError)}
</Alert>
</div>
)}
{mutationError && (
<div className="px-4">
<Alert variant="danger">
{intl.formatMessage(messages.mutationError, { error: mutationError.message })}
</Alert>
</div>
)}
<Container className="px-4 py-4">
<div className="mb-4">
<p>{intl.formatMessage(messages.backupDescription)}</p>
</div>
<div className="bg-info-700 text-white p-4 rounded row justify-content-between align-items-center">
<div className="d-flex flex-column">
<div className="d-inline-flex align-items-center">
<Newsstand className="mr-2" />
<span>{libraryData.title}</span>
</div>
<span className="small">{`${libraryData.org} / ${libraryData.slug}`}</span>
</div>
<Button
variant="info"
iconBefore={getButtonIcon()}
onClick={handleDownloadBackup}
disabled={Boolean(isBackupInProgress)}
aria-label={intl.formatMessage(messages.downloadAriaLabel, {
buttonText: getButtonText(),
libraryTitle: libraryData.title,
})}
>
{getButtonText()}
</Button>
</div>
</Container>
</Container>
</div>
</div>
);
};