1 import { END, eventChannel } from 'redux-saga';
2 import { put, select, take, takeLeading } from 'redux-saga/effects';
4 import { restoreItems } from '@proton/pass/lib/items/item.requests';
6 itemBulkRestoreFailure,
8 itemBulkRestoreProgress,
9 itemBulkRestoreSuccess,
10 } from '@proton/pass/store/actions';
11 import type { RequestProgress } from '@proton/pass/store/request/types';
12 import { selectBulkSelection } from '@proton/pass/store/selectors';
13 import type { RootSagaOptions } from '@proton/pass/store/types';
14 import type { BatchItemRevisionIDs, ItemRevision, ItemRevisionResponse } from '@proton/pass/types';
15 import noop from '@proton/utils/noop';
17 export type BulkRestoreChannel = RequestProgress<ItemRevisionResponse[], BatchItemRevisionIDs>;
19 export const bulkRestoreChannel = (items: ItemRevision[]) =>
20 eventChannel<BulkRestoreChannel>((emitter) => {
21 restoreItems(items, (data, progress) => emitter({ type: 'progress', progress, data }))
22 .then((result: any) => emitter({ type: 'done', result }))
23 .catch((error) => emitter({ type: 'error', error }))
24 .finally(() => emitter(END));
29 function* itemBulkDeleteWorker(
30 { onItemsUpdated }: RootSagaOptions,
31 { payload, meta }: ReturnType<typeof itemBulkRestoreIntent>
33 const requestId = meta.request.id;
34 const { selected } = payload;
35 const items = (yield select(selectBulkSelection(selected))) as ItemRevision[];
36 const progressChannel = bulkRestoreChannel(items);
39 const action: BulkRestoreChannel = yield take(progressChannel);
42 if (action.type === 'progress') yield put(itemBulkRestoreProgress(requestId, action.progress, action.data));
43 if (action.type === 'done') yield put(itemBulkRestoreSuccess(requestId, {}));
44 if (action.type === 'error') yield put(itemBulkRestoreFailure(requestId, {}, action.error));
48 export default function* watcher(options: RootSagaOptions) {
49 yield takeLeading(itemBulkRestoreIntent.match, itemBulkDeleteWorker, options);