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

141 lines
3.2 KiB
TypeScript
Raw Normal View History

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2022-12-14 18:12:04 +00:00
import { ipcRenderer } from 'electron';
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
2022-12-14 18:12:04 +00:00
import type { NoopActionType } from './noop';
import type { ReplacementValuesType } from '../../types/Util';
2022-12-14 18:12:04 +00:00
import { useBoundActions } from '../../hooks/useBoundActions';
export enum ToastType {
AddingUserToGroup = 'AddingUserToGroup',
2022-12-08 07:43:48 +00:00
Blocked = 'Blocked',
BlockedGroup = 'BlockedGroup',
CannotMixMultiAndNonMultiAttachments = 'CannotMixMultiAndNonMultiAttachments',
CannotStartGroupCall = 'CannotStartGroupCall',
CopiedUsername = 'CopiedUsername',
CopiedUsernameLink = 'CopiedUsernameLink',
DangerousFileType = 'DangerousFileType',
DeleteForEveryoneFailed = 'DeleteForEveryoneFailed',
2022-07-29 00:10:07 +00:00
Error = 'Error',
2022-12-08 07:43:48 +00:00
Expired = 'Expired',
FailedToDeleteUsername = 'FailedToDeleteUsername',
2022-12-14 18:12:04 +00:00
FileSaved = 'FileSaved',
FileSize = 'FileSize',
2022-12-08 07:43:48 +00:00
InvalidConversation = 'InvalidConversation',
LeftGroup = 'LeftGroup',
MaxAttachments = 'MaxAttachments',
MessageBodyTooLong = 'MessageBodyTooLong',
2022-12-07 01:00:02 +00:00
PinnedConversationsFull = 'PinnedConversationsFull',
2022-12-06 19:03:09 +00:00
ReportedSpamAndBlocked = 'ReportedSpamAndBlocked',
StoryMuted = 'StoryMuted',
StoryReact = 'StoryReact',
StoryReply = 'StoryReply',
StoryVideoError = 'StoryVideoError',
StoryVideoTooLong = 'StoryVideoTooLong',
StoryVideoUnsupported = 'StoryVideoUnsupported',
UnableToLoadAttachment = 'UnableToLoadAttachment',
UnsupportedMultiAttachment = 'UnsupportedMultiAttachment',
UserAddedToGroup = 'UserAddedToGroup',
}
// State
export type ToastStateType = {
toast?: {
toastType: ToastType;
parameters?: ReplacementValuesType;
};
};
// Actions
const HIDE_TOAST = 'toast/HIDE_TOAST';
export const SHOW_TOAST = 'toast/SHOW_TOAST';
type HideToastActionType = {
type: typeof HIDE_TOAST;
};
export type ShowToastActionType = {
type: typeof SHOW_TOAST;
payload: {
toastType: ToastType;
parameters?: ReplacementValuesType;
};
};
export type ToastActionType = HideToastActionType | ShowToastActionType;
// Action Creators
function hideToast(): HideToastActionType {
return {
type: HIDE_TOAST,
};
}
2022-12-14 18:12:04 +00:00
function openFileInFolder(target: string): NoopActionType {
ipcRenderer.send('show-item-in-folder', target);
return {
type: 'NOOP',
payload: null,
};
}
export type ShowToastActionCreatorType = (
toastType: ToastType,
parameters?: ReplacementValuesType
) => ShowToastActionType;
2022-10-18 17:12:02 +00:00
export const showToast: ShowToastActionCreatorType = (
toastType,
parameters
) => {
return {
type: SHOW_TOAST,
payload: {
toastType,
parameters,
},
};
};
export const actions = {
hideToast,
2022-12-14 18:12:04 +00:00
openFileInFolder,
showToast,
};
export const useToastActions = (): BoundActionCreatorsMapObject<
typeof actions
> => useBoundActions(actions);
// Reducer
export function getEmptyState(): ToastStateType {
return {};
}
export function reducer(
state: Readonly<ToastStateType> = getEmptyState(),
action: Readonly<ToastActionType>
): ToastStateType {
if (action.type === HIDE_TOAST) {
return {
...state,
toast: undefined,
};
}
if (action.type === SHOW_TOAST) {
return {
...state,
toast: action.payload,
};
}
return state;
}