2025-04-07 12:12:40 -07:00
|
|
|
import * as React from 'react';
|
|
|
|
|
import { useContext, useEffect, useRef } from 'react';
|
2024-03-02 14:25:27 +00:00
|
|
|
import { StateLastDeletion } from '@joplin/lib/reducer';
|
|
|
|
|
import { _, _n } from '@joplin/lib/locale';
|
|
|
|
|
import restoreItems from '@joplin/lib/services/trash/restoreItems';
|
|
|
|
|
import { ModelType } from '@joplin/lib/BaseModel';
|
|
|
|
|
import { Dispatch } from 'redux';
|
2025-04-07 12:12:40 -07:00
|
|
|
import { PopupNotificationContext } from '../PopupNotification/PopupNotificationProvider';
|
|
|
|
|
import { NotificationType } from '../PopupNotification/types';
|
|
|
|
|
import TrashNotificationMessage from './TrashNotificationMessage';
|
2024-03-02 14:25:27 +00:00
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
lastDeletion: StateLastDeletion;
|
|
|
|
|
lastDeletionNotificationTime: number;
|
|
|
|
|
themeId: number;
|
|
|
|
|
dispatch: Dispatch;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-07 12:12:40 -07:00
|
|
|
const onCancelClick = async (lastDeletion: StateLastDeletion) => {
|
|
|
|
|
if (lastDeletion.folderIds.length) {
|
|
|
|
|
await restoreItems(ModelType.Folder, lastDeletion.folderIds);
|
|
|
|
|
}
|
2024-03-02 14:25:27 +00:00
|
|
|
|
2025-04-07 12:12:40 -07:00
|
|
|
if (lastDeletion.noteIds.length) {
|
|
|
|
|
await restoreItems(ModelType.Note, lastDeletion.noteIds);
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-03-02 14:25:27 +00:00
|
|
|
|
2025-04-07 12:12:40 -07:00
|
|
|
export default (props: Props) => {
|
|
|
|
|
const popupManager = useContext(PopupNotificationContext);
|
2024-03-02 14:25:27 +00:00
|
|
|
|
2025-04-07 12:12:40 -07:00
|
|
|
const lastDeletionNotificationTimeRef = useRef<number>();
|
|
|
|
|
lastDeletionNotificationTimeRef.current = props.lastDeletionNotificationTime;
|
2024-03-02 14:25:27 +00:00
|
|
|
|
2025-04-07 12:12:40 -07:00
|
|
|
useEffect(() => {
|
|
|
|
|
const lastDeletionNotificationTime = lastDeletionNotificationTimeRef.current;
|
|
|
|
|
if (!props.lastDeletion || props.lastDeletion.timestamp <= lastDeletionNotificationTime) return;
|
2024-03-02 14:25:27 +00:00
|
|
|
|
|
|
|
|
props.dispatch({ type: 'DELETION_NOTIFICATION_DONE' });
|
|
|
|
|
|
|
|
|
|
let msg = '';
|
|
|
|
|
if (props.lastDeletion.folderIds.length) {
|
|
|
|
|
msg = _('The notebook and its content was successfully moved to the trash.');
|
|
|
|
|
} else if (props.lastDeletion.noteIds.length) {
|
|
|
|
|
msg = _n('The note was successfully moved to the trash.', 'The notes were successfully moved to the trash.', props.lastDeletion.noteIds.length);
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-07 12:12:40 -07:00
|
|
|
const handleCancelClick = () => {
|
|
|
|
|
notification.remove();
|
|
|
|
|
void onCancelClick(props.lastDeletion);
|
|
|
|
|
};
|
|
|
|
|
const notification = popupManager.createPopup(() => (
|
|
|
|
|
<TrashNotificationMessage message={msg} onCancel={handleCancelClick}/>
|
|
|
|
|
), { type: NotificationType.Success });
|
|
|
|
|
notification.scheduleDismiss();
|
|
|
|
|
}, [props.lastDeletion, props.dispatch, popupManager]);
|
2024-03-02 14:25:27 +00:00
|
|
|
|
|
|
|
|
return <div style={{ display: 'none' }}/>;
|
|
|
|
|
};
|