signal-desktop/ts/state/ducks/globalModals.ts

253 lines
5.6 KiB
TypeScript
Raw Normal View History

2021-05-28 16:15:17 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
// State
export type GlobalModalsStateType = {
2021-09-21 22:37:10 +00:00
readonly contactModalState?: ContactModalStateType;
2021-07-19 19:26:06 +00:00
readonly isProfileEditorVisible: boolean;
2021-11-12 01:17:29 +00:00
readonly isWhatsNewVisible: boolean;
2021-07-19 19:26:06 +00:00
readonly profileEditorHasError: boolean;
readonly safetyNumberModalContactId?: string;
2021-11-12 01:17:29 +00:00
readonly usernameNotFoundModalState?: UsernameNotFoundModalStateType;
2021-05-28 16:15:17 +00:00
};
// Actions
2021-09-21 22:37:10 +00:00
const HIDE_CONTACT_MODAL = 'globalModals/HIDE_CONTACT_MODAL';
const SHOW_CONTACT_MODAL = 'globalModals/SHOW_CONTACT_MODAL';
const SHOW_WHATS_NEW_MODAL = 'globalModals/SHOW_WHATS_NEW_MODAL_MODAL';
2021-11-12 01:17:29 +00:00
const SHOW_USERNAME_NOT_FOUND_MODAL =
'globalModals/SHOW_USERNAME_NOT_FOUND_MODAL';
const HIDE_USERNAME_NOT_FOUND_MODAL =
'globalModals/HIDE_USERNAME_NOT_FOUND_MODAL';
const HIDE_WHATS_NEW_MODAL = 'globalModals/HIDE_WHATS_NEW_MODAL_MODAL';
2021-07-19 19:26:06 +00:00
const TOGGLE_PROFILE_EDITOR = 'globalModals/TOGGLE_PROFILE_EDITOR';
export const TOGGLE_PROFILE_EDITOR_ERROR =
'globalModals/TOGGLE_PROFILE_EDITOR_ERROR';
const TOGGLE_SAFETY_NUMBER_MODAL = 'globalModals/TOGGLE_SAFETY_NUMBER_MODAL';
2021-05-28 16:15:17 +00:00
2021-09-21 22:37:10 +00:00
export type ContactModalStateType = {
contactId: string;
conversationId?: string;
};
2021-11-12 01:17:29 +00:00
export type UsernameNotFoundModalStateType = {
username: string;
};
2021-09-21 22:37:10 +00:00
type HideContactModalActionType = {
type: typeof HIDE_CONTACT_MODAL;
};
type ShowContactModalActionType = {
type: typeof SHOW_CONTACT_MODAL;
payload: ContactModalStateType;
};
type HideWhatsNewModalActionType = {
type: typeof HIDE_WHATS_NEW_MODAL;
};
type ShowWhatsNewModalActionType = {
type: typeof SHOW_WHATS_NEW_MODAL;
};
2021-11-12 01:17:29 +00:00
type HideUsernameNotFoundModalActionType = {
type: typeof HIDE_USERNAME_NOT_FOUND_MODAL;
};
export type ShowUsernameNotFoundModalActionType = {
type: typeof SHOW_USERNAME_NOT_FOUND_MODAL;
payload: {
username: string;
};
};
2021-07-19 19:26:06 +00:00
type ToggleProfileEditorActionType = {
type: typeof TOGGLE_PROFILE_EDITOR;
};
export type ToggleProfileEditorErrorActionType = {
type: typeof TOGGLE_PROFILE_EDITOR_ERROR;
};
type ToggleSafetyNumberModalActionType = {
type: typeof TOGGLE_SAFETY_NUMBER_MODAL;
payload: string | undefined;
};
2021-07-19 19:26:06 +00:00
export type GlobalModalsActionType =
2021-09-21 22:37:10 +00:00
| HideContactModalActionType
| ShowContactModalActionType
| HideWhatsNewModalActionType
| ShowWhatsNewModalActionType
2021-11-12 01:17:29 +00:00
| HideUsernameNotFoundModalActionType
| ShowUsernameNotFoundModalActionType
2021-07-19 19:26:06 +00:00
| ToggleProfileEditorActionType
| ToggleProfileEditorErrorActionType
| ToggleSafetyNumberModalActionType;
2021-05-28 16:15:17 +00:00
// Action Creators
export const actions = {
2021-09-21 22:37:10 +00:00
hideContactModal,
showContactModal,
hideWhatsNewModal,
showWhatsNewModal,
2021-11-12 01:17:29 +00:00
hideUsernameNotFoundModal,
showUsernameNotFoundModal,
2021-07-19 19:26:06 +00:00
toggleProfileEditor,
toggleProfileEditorHasError,
toggleSafetyNumberModal,
2021-05-28 16:15:17 +00:00
};
2021-09-21 22:37:10 +00:00
function hideContactModal(): HideContactModalActionType {
return {
type: HIDE_CONTACT_MODAL,
};
}
function showContactModal(
contactId: string,
conversationId?: string
): ShowContactModalActionType {
return {
type: SHOW_CONTACT_MODAL,
payload: {
contactId,
conversationId,
},
};
}
function hideWhatsNewModal(): HideWhatsNewModalActionType {
return {
type: HIDE_WHATS_NEW_MODAL,
};
}
function showWhatsNewModal(): ShowWhatsNewModalActionType {
return {
type: SHOW_WHATS_NEW_MODAL,
};
}
2021-11-12 01:17:29 +00:00
function hideUsernameNotFoundModal(): HideUsernameNotFoundModalActionType {
return {
type: HIDE_USERNAME_NOT_FOUND_MODAL,
};
}
function showUsernameNotFoundModal(
username: string
): ShowUsernameNotFoundModalActionType {
return {
type: SHOW_USERNAME_NOT_FOUND_MODAL,
payload: {
username,
},
};
}
2021-07-19 19:26:06 +00:00
function toggleProfileEditor(): ToggleProfileEditorActionType {
return { type: TOGGLE_PROFILE_EDITOR };
}
function toggleProfileEditorHasError(): ToggleProfileEditorErrorActionType {
return { type: TOGGLE_PROFILE_EDITOR_ERROR };
}
function toggleSafetyNumberModal(
safetyNumberModalContactId?: string
): ToggleSafetyNumberModalActionType {
return {
type: TOGGLE_SAFETY_NUMBER_MODAL,
payload: safetyNumberModalContactId,
};
}
2021-05-28 16:15:17 +00:00
// Reducer
export function getEmptyState(): GlobalModalsStateType {
return {
2021-07-19 19:26:06 +00:00
isProfileEditorVisible: false,
profileEditorHasError: false,
isWhatsNewVisible: false,
2021-05-28 16:15:17 +00:00
};
}
export function reducer(
state: Readonly<GlobalModalsStateType> = getEmptyState(),
action: Readonly<GlobalModalsActionType>
): GlobalModalsStateType {
2021-07-19 19:26:06 +00:00
if (action.type === TOGGLE_PROFILE_EDITOR) {
return {
...state,
isProfileEditorVisible: !state.isProfileEditorVisible,
};
}
if (action.type === TOGGLE_PROFILE_EDITOR_ERROR) {
return {
...state,
profileEditorHasError: !state.profileEditorHasError,
};
}
if (action.type === SHOW_WHATS_NEW_MODAL) {
return {
...state,
isWhatsNewVisible: true,
};
}
if (action.type === HIDE_WHATS_NEW_MODAL) {
return {
...state,
isWhatsNewVisible: false,
};
}
2021-11-12 01:17:29 +00:00
if (action.type === HIDE_USERNAME_NOT_FOUND_MODAL) {
return {
...state,
usernameNotFoundModalState: undefined,
};
}
if (action.type === SHOW_USERNAME_NOT_FOUND_MODAL) {
const { username } = action.payload;
return {
...state,
usernameNotFoundModalState: {
username,
},
};
}
2021-09-21 22:37:10 +00:00
if (action.type === SHOW_CONTACT_MODAL) {
return {
...state,
contactModalState: action.payload,
};
}
if (action.type === HIDE_CONTACT_MODAL) {
return {
...state,
contactModalState: undefined,
};
}
if (action.type === TOGGLE_SAFETY_NUMBER_MODAL) {
return {
...state,
safetyNumberModalContactId: action.payload,
};
}
2021-05-28 16:15:17 +00:00
return state;
}