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';
|
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
2023-04-10 21:38:34 +00:00
|
|
|
import type { AnyToast } 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 = {
|
2023-04-10 21:38:34 +00:00
|
|
|
toast?: AnyToast;
|
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;
|
2023-04-10 21:38:34 +00:00
|
|
|
payload: AnyToast;
|
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-04-10 21:38:34 +00:00
|
|
|
export type ShowToastAction = ReadonlyDeep<(toast: AnyToast) => void>;
|
|
|
|
|
|
|
|
export function showToast(toast: AnyToast): ShowToastActionType {
|
2022-07-12 16:41:41 +00:00
|
|
|
return {
|
|
|
|
type: SHOW_TOAST,
|
2023-04-10 21:38:34 +00:00
|
|
|
payload: toast,
|
2022-07-12 16:41:41 +00:00
|
|
|
};
|
2023-04-10 21:38:34 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|