2022-07-12 16:41:41 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-12-14 18:12:04 +00:00
|
|
|
import { ipcRenderer } from 'electron';
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2022-12-09 17:35:34 +00:00
|
|
|
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
|
2022-12-14 18:12:04 +00:00
|
|
|
import type { NoopActionType } from './noop';
|
2022-09-26 16:24:52 +00:00
|
|
|
import type { ReplacementValuesType } from '../../types/Util';
|
2022-12-14 18:12:04 +00:00
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
2022-12-15 00:48:36 +00:00
|
|
|
import type { ToastType } from '../../types/Toast';
|
2022-12-20 01:04:47 +00:00
|
|
|
|
2022-07-12 16:41:41 +00:00
|
|
|
// State
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
// eslint-disable-next-line local-rules/type-alias-readonlydeep
|
2022-07-12 16:41:41 +00:00
|
|
|
export type ToastStateType = {
|
2022-09-26 16:24:52 +00:00
|
|
|
toast?: {
|
|
|
|
toastType: ToastType;
|
|
|
|
parameters?: ReplacementValuesType;
|
|
|
|
};
|
2022-07-12 16:41:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
|
|
|
const HIDE_TOAST = 'toast/HIDE_TOAST';
|
2022-12-05 22:56:23 +00:00
|
|
|
export const SHOW_TOAST = 'toast/SHOW_TOAST';
|
2022-07-12 16:41:41 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type HideToastActionType = ReadonlyDeep<{
|
2022-07-12 16:41:41 +00:00
|
|
|
type: typeof HIDE_TOAST;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2022-07-12 16:41:41 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
// eslint-disable-next-line local-rules/type-alias-readonlydeep
|
2022-12-05 22:56:23 +00:00
|
|
|
export type ShowToastActionType = {
|
2022-07-12 16:41:41 +00:00
|
|
|
type: typeof SHOW_TOAST;
|
2022-09-26 16:24:52 +00:00
|
|
|
payload: {
|
|
|
|
toastType: ToastType;
|
|
|
|
parameters?: ReplacementValuesType;
|
|
|
|
};
|
2022-07-12 16:41:41 +00:00
|
|
|
};
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
// eslint-disable-next-line local-rules/type-alias-readonlydeep
|
2022-07-12 16:41:41 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type ShowToastActionCreatorType = ReadonlyDeep<
|
|
|
|
(
|
|
|
|
toastType: ToastType,
|
|
|
|
parameters?: ReplacementValuesType
|
|
|
|
) => ShowToastActionType
|
|
|
|
>;
|
2022-07-12 16:41:41 +00:00
|
|
|
|
2022-10-18 17:12:02 +00:00
|
|
|
export const showToast: ShowToastActionCreatorType = (
|
|
|
|
toastType,
|
|
|
|
parameters
|
|
|
|
) => {
|
2022-07-12 16:41:41 +00:00
|
|
|
return {
|
|
|
|
type: SHOW_TOAST,
|
2022-09-26 16:24:52 +00:00
|
|
|
payload: {
|
|
|
|
toastType,
|
|
|
|
parameters,
|
|
|
|
},
|
2022-07-12 16:41:41 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
hideToast,
|
2022-12-14 18:12:04 +00:00
|
|
|
openFileInFolder,
|
2022-07-12 16:41:41 +00:00
|
|
|
showToast,
|
|
|
|
};
|
|
|
|
|
2022-12-09 17:35:34 +00:00
|
|
|
export const useToastActions = (): BoundActionCreatorsMapObject<
|
|
|
|
typeof actions
|
|
|
|
> => useBoundActions(actions);
|
2022-07-12 16:41:41 +00:00
|
|
|
|
|
|
|
// Reducer
|
|
|
|
|
|
|
|
export function getEmptyState(): ToastStateType {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reducer(
|
|
|
|
state: Readonly<ToastStateType> = getEmptyState(),
|
|
|
|
action: Readonly<ToastActionType>
|
|
|
|
): ToastStateType {
|
|
|
|
if (action.type === HIDE_TOAST) {
|
|
|
|
return {
|
|
|
|
...state,
|
2022-09-26 16:24:52 +00:00
|
|
|
toast: undefined,
|
2022-07-12 16:41:41 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === SHOW_TOAST) {
|
|
|
|
return {
|
|
|
|
...state,
|
2022-09-26 16:24:52 +00:00
|
|
|
toast: action.payload,
|
2022-07-12 16:41:41 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|