2023-08-09 00:53:06 +00:00
|
|
|
// Copyright 2023 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
|
|
|
import type { ThunkAction } from 'redux-thunk';
|
|
|
|
import type { StateType as RootStateType } from '../reducer';
|
|
|
|
import { clearCallHistoryDataAndSync } from '../../util/callDisposition';
|
|
|
|
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
|
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
|
|
|
import type { ToastActionType } from './toast';
|
|
|
|
import { showToast } from './toast';
|
|
|
|
import { ToastType } from '../../types/Toast';
|
|
|
|
import type { CallHistoryDetails } from '../../types/CallDisposition';
|
2023-08-10 22:16:51 +00:00
|
|
|
import * as log from '../../logging/log';
|
|
|
|
import * as Errors from '../../types/errors';
|
2023-08-09 00:53:06 +00:00
|
|
|
|
|
|
|
export type CallHistoryState = ReadonlyDeep<{
|
|
|
|
// This informs the app that underlying call history data has changed.
|
|
|
|
edition: number;
|
|
|
|
callHistoryByCallId: Record<string, CallHistoryDetails>;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
const CALL_HISTORY_CACHE = 'callHistory/CACHE';
|
2023-08-10 22:16:51 +00:00
|
|
|
const CALL_HISTORY_RESET = 'callHistory/RESET';
|
2023-08-09 00:53:06 +00:00
|
|
|
|
|
|
|
export type CallHistoryCache = ReadonlyDeep<{
|
|
|
|
type: typeof CALL_HISTORY_CACHE;
|
|
|
|
payload: CallHistoryDetails;
|
|
|
|
}>;
|
|
|
|
|
2023-08-10 22:16:51 +00:00
|
|
|
export type CallHistoryReset = ReadonlyDeep<{
|
|
|
|
type: typeof CALL_HISTORY_RESET;
|
2023-08-09 00:53:06 +00:00
|
|
|
}>;
|
|
|
|
|
|
|
|
export type CallHistoryAction = ReadonlyDeep<
|
2023-08-10 22:16:51 +00:00
|
|
|
CallHistoryCache | CallHistoryReset
|
2023-08-09 00:53:06 +00:00
|
|
|
>;
|
|
|
|
|
|
|
|
export function getEmptyState(): CallHistoryState {
|
|
|
|
return {
|
|
|
|
edition: 0,
|
|
|
|
callHistoryByCallId: {},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function cacheCallHistory(callHistory: CallHistoryDetails): CallHistoryCache {
|
|
|
|
return {
|
|
|
|
type: CALL_HISTORY_CACHE,
|
|
|
|
payload: callHistory,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearAllCallHistory(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
2023-08-10 22:16:51 +00:00
|
|
|
CallHistoryReset | ToastActionType
|
2023-08-09 00:53:06 +00:00
|
|
|
> {
|
|
|
|
return async dispatch => {
|
2023-08-10 22:16:51 +00:00
|
|
|
try {
|
|
|
|
await clearCallHistoryDataAndSync();
|
|
|
|
dispatch(showToast({ toastType: ToastType.CallHistoryCleared }));
|
|
|
|
} catch (error) {
|
|
|
|
log.error('Error clearing call history', Errors.toLogFormat(error));
|
|
|
|
} finally {
|
|
|
|
// Just force a reset, even if the clear failed.
|
|
|
|
dispatch({ type: CALL_HISTORY_RESET });
|
|
|
|
}
|
2023-08-09 00:53:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
cacheCallHistory,
|
|
|
|
clearAllCallHistory,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useCallHistoryActions = (): BoundActionCreatorsMapObject<
|
|
|
|
typeof actions
|
|
|
|
> => useBoundActions(actions);
|
|
|
|
|
|
|
|
export function reducer(
|
|
|
|
state: CallHistoryState = getEmptyState(),
|
|
|
|
action: CallHistoryAction
|
|
|
|
): CallHistoryState {
|
|
|
|
switch (action.type) {
|
2023-08-10 22:16:51 +00:00
|
|
|
case CALL_HISTORY_RESET:
|
2023-08-09 00:53:06 +00:00
|
|
|
return { ...state, edition: state.edition + 1, callHistoryByCallId: {} };
|
|
|
|
case CALL_HISTORY_CACHE:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
callHistoryByCallId: {
|
|
|
|
...state.callHistoryByCallId,
|
|
|
|
[action.payload.callId]: action.payload,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|