Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / pass / store / sagas / invites / invite-accept.saga.ts
blob385a4b81e2ba70e6c6b57b04ef0a44a2e489f848
1 import { END, eventChannel } from 'redux-saga';
2 import { put, select, take, takeEvery } from 'redux-saga/effects';
3 import { c } from 'ttag';
5 import { acceptInvite } from '@proton/pass/lib/invites/invite.requests';
6 import { requestItemsForShareId } from '@proton/pass/lib/items/item.requests';
7 import { parseShareResponse } from '@proton/pass/lib/shares/share.parser';
8 import {
9     inviteAcceptFailure,
10     inviteAcceptIntent,
11     inviteAcceptSuccess,
12     startEventPolling,
13     stopEventPolling,
14 } from '@proton/pass/store/actions';
15 import { requestProgress } from '@proton/pass/store/request/actions';
16 import type { RequestProgress } from '@proton/pass/store/request/types';
17 import { selectInviteByToken } from '@proton/pass/store/selectors/invites';
18 import type { Invite, ItemRevision, Maybe, Share, ShareGetResponse, ShareType } from '@proton/pass/types';
19 import noop from '@proton/utils/noop';
21 type AcceptInviteChannel = RequestProgress<ItemRevision[], null>;
23 function* acceptInviteWorker({ payload, meta: { request } }: ReturnType<typeof inviteAcceptIntent>) {
24     const requestId = request.id;
25     const { inviteToken } = payload;
27     try {
28         yield put(stopEventPolling());
30         const invite: Maybe<Invite> = yield select(selectInviteByToken(inviteToken));
31         if (!invite) throw new Error(c('Error').t`Unknown invite`);
33         const encryptedShare: ShareGetResponse = yield acceptInvite({ ...payload, inviteKeys: invite.keys });
34         const share: Maybe<Share<ShareType.Vault>> = yield parseShareResponse(encryptedShare);
35         if (!share) throw new Error(c('Error').t`Could not open invited vault`);
37         const progressChannel = eventChannel<AcceptInviteChannel>((emitter) => {
38             requestItemsForShareId(share.shareId, (progress) => emitter({ type: 'progress', progress, data: null }))
39                 .then((result) => emitter({ type: 'done', result }))
40                 .catch((error) => emitter({ type: 'error', error }))
41                 .finally(() => emitter(END));
43             return noop;
44         });
46         while (true) {
47             const action: AcceptInviteChannel = yield take(progressChannel);
48             if (action.type === 'progress') yield put(requestProgress(requestId, action.progress));
49             if (action.type === 'done') yield put(inviteAcceptSuccess(requestId, inviteToken, share, action.result));
50             if (action.type === 'error') throw action.error;
51         }
52     } catch (err) {
53         yield put(inviteAcceptFailure(requestId, err));
54     } finally {
55         yield put(startEventPolling());
56     }
59 export default function* watcher() {
60     yield takeEvery(inviteAcceptIntent.match, acceptInviteWorker);