Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / pass / store / sagas / rules / website-rules.saga.ts
blob53bda8f8739b51a32f265b5badec04254964269c
1 import { select } from 'redux-saga/effects';
3 import { WEBSITE_RULES_URL } from '@proton/pass/constants';
4 import { validateRules } from '@proton/pass/lib/extension/utils/website-rules';
5 import { resolveWebsiteRules } from '@proton/pass/store/actions/creators/rules';
6 import { createRequestSaga } from '@proton/pass/store/request/sagas';
7 import type { RequestEntry, RequestStatus } from '@proton/pass/store/request/types';
8 import { selectRequest } from '@proton/pass/store/selectors';
9 import type { Maybe } from '@proton/pass/types';
10 import { msToEpoch } from '@proton/pass/utils/time/epoch';
12 /* Don't fetch if the rules were not modified since our last fetch */
13 const revalidateWebsiteRules = async (lastRequestedAt: number): Promise<boolean> => {
14     if (lastRequestedAt === 0) return true;
15     const header = (await fetch(WEBSITE_RULES_URL, { method: 'HEAD' })).headers.get('Last-Modified');
16     const lastModified = header ? msToEpoch(new Date(header).getTime()) : 0;
17     if (lastRequestedAt >= lastModified) return false;
19     return true;
22 export default createRequestSaga({
23     actions: resolveWebsiteRules,
24     call: function* (_, { getStorage }) {
25         const lastRequest: Maybe<RequestEntry<RequestStatus>> = yield select(
26             selectRequest(resolveWebsiteRules.requestID())
27         );
28         const lastRequestedAt = lastRequest?.status === 'success' ? lastRequest.requestedAt : 0;
30         if (yield revalidateWebsiteRules(lastRequestedAt)) {
31             const response: Response = yield fetch(WEBSITE_RULES_URL);
32             const rules: unknown = yield response.json();
33             if (validateRules(rules)) yield getStorage?.().setItem('websiteRules', JSON.stringify(rules));
34         }
36         return true;
37     },
38 });