1import { List, Record } from 'immutable'; 2 3import { HistoryItem as HistoryItemInput } from '../types'; 4 5type HistoryItemObject = { 6 url: null | string; 7 bundleUrl: null | string; 8 manifestUrl: null | string; 9 manifest: null | { [key: string]: any }; 10 time: null | number; 11}; 12 13type HistoryItemType = Record<HistoryItemObject> & Readonly<HistoryItemObject>; 14 15const HistoryItem = Record<HistoryItemObject>({ 16 url: null, 17 bundleUrl: null, 18 manifestUrl: null, 19 manifest: null, 20 time: null, 21}); 22 23type HistoryObject = { 24 history: List<HistoryItemType>; 25}; 26 27export type HistoryType = Record<HistoryObject> & Readonly<HistoryObject>; 28 29const HistoryState = Record<HistoryObject>({ 30 history: List(), 31}); 32 33type HistoryActions = 34 | { 35 type: 'loadHistory'; 36 payload: { history: HistoryItemInput[] }; 37 } 38 | { type: 'clearHistory' }; 39 40export default (state: HistoryType, action: HistoryActions): HistoryType => { 41 switch (action.type) { 42 case 'loadHistory': { 43 const { history } = action.payload; 44 const immutableHistoryList = history 45 ? List(history.map((item) => new HistoryItem(item))) 46 : List(); 47 return state.merge({ 48 // @ts-ignore 49 history: immutableHistoryList, 50 }); 51 } 52 case 'clearHistory': 53 return state.merge({ 54 history: state.get('history').clear(), 55 }); 56 default: 57 return state ? state : new HistoryState(); 58 } 59}; 60