import { confirmDialog } from '@/services/alertService';
import { STRING_RESTORE_LOCKED_ATTEMPT } from '@/strings';
import { WebApplication } from '@/ui_models/application';
import { AppState } from '@/ui_models/app_state';
import { getPlatformString } from '@/utils';
import {
AlertDialogContent,
AlertDialogDescription,
AlertDialogLabel,
AlertDialogOverlay,
} from '@reach/alert-dialog';
import VisuallyHidden from '@reach/visually-hidden';
import {
ButtonType,
ContentType,
HistoryEntry,
PayloadContent,
PayloadSource,
RevisionListEntry,
SNNote,
} from '@standardnotes/snjs';
import { observer } from 'mobx-react-lite';
import { FunctionComponent } from 'preact';
import {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'preact/hooks';
import { Button } from '../Button';
import { HistoryListContainer } from './HistoryListContainer';
import { RevisionContentLocked } from './RevisionContentLocked';
import { SelectedRevisionContent } from './SelectedRevisionContent';
import {
LegacyHistoryEntry,
RemoteRevisionListGroup,
sortRevisionListIntoGroups,
} from './utils';
type RevisionHistoryModalProps = {
application: WebApplication;
appState: AppState;
};
const ABSOLUTE_CENTER_CLASSNAME =
'absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2';
type RevisionContentPlaceholderProps = {
isFetchingSelectedRevision: boolean;
selectedRevision: HistoryEntry | LegacyHistoryEntry | undefined;
showContentLockedScreen: boolean;
};
const RevisionContentPlaceholder: FunctionComponent<
RevisionContentPlaceholderProps
> = ({
isFetchingSelectedRevision,
selectedRevision,
showContentLockedScreen,
}) => (
{isFetchingSelectedRevision && (
)}
{!isFetchingSelectedRevision && !selectedRevision ? (
No revision selected
) : null}
);
export const RevisionHistoryModal: FunctionComponent =
observer(({ application, appState }) => {
const closeButtonRef = useRef(null);
const dismissModal = () => {
appState.notes.setShowRevisionHistoryModal(false);
};
const note = Object.values(appState.notes.selectedNotes)[0];
const editorForCurrentNote = useMemo(() => {
return application.componentManager.editorForNote(note);
}, [application.componentManager, note]);
const [isFetchingSelectedRevision, setIsFetchingSelectedRevision] =
useState(false);
const [selectedRevision, setSelectedRevision] = useState<
HistoryEntry | LegacyHistoryEntry
>();
const [selectedRemoteEntry, setSelectedRemoteEntry] =
useState();
const [isDeletingRevision, setIsDeletingRevision] = useState(false);
const [templateNoteForRevision, setTemplateNoteForRevision] =
useState();
const [showContentLockedScreen, setShowContentLockedScreen] =
useState(false);
const [remoteHistory, setRemoteHistory] =
useState();
const [isFetchingRemoteHistory, setIsFetchingRemoteHistory] =
useState(false);
const fetchRemoteHistory = useCallback(async () => {
if (note) {
setRemoteHistory(undefined);
setIsFetchingRemoteHistory(true);
try {
const initialRemoteHistory =
await application.historyManager.remoteHistoryForItem(note);
const remoteHistoryAsGroups =
sortRevisionListIntoGroups(initialRemoteHistory);
setRemoteHistory(remoteHistoryAsGroups);
} catch (err) {
console.error(err);
} finally {
setIsFetchingRemoteHistory(false);
}
}
}, [application.historyManager, note]);
useEffect(() => {
if (!remoteHistory?.length) {
fetchRemoteHistory();
}
}, [fetchRemoteHistory, remoteHistory?.length]);
const restore = () => {
if (selectedRevision) {
const originalNote = application.items.findItem(
selectedRevision.payload.uuid
) as SNNote;
if (originalNote.locked) {
application.alertService.alert(STRING_RESTORE_LOCKED_ATTEMPT);
return;
}
confirmDialog({
text: "Are you sure you want to replace the current note's contents with what you see in this preview?",
confirmButtonStyle: 'danger',
}).then((confirmed) => {
if (confirmed) {
application.mutator.changeAndSaveItem(
selectedRevision.payload.uuid,
(mutator) => {
mutator.unsafe_setCustomContent(
selectedRevision.payload.content
);
},
true,
PayloadSource.RemoteActionRetrieved
);
dismissModal();
}
});
}
};
const restoreAsCopy = async () => {
if (selectedRevision) {
const originalNote = application.items.findItem(
selectedRevision.payload.uuid
) as SNNote;
const duplicatedItem = await application.mutator.duplicateItem(
originalNote,
{
...(selectedRevision.payload.content as PayloadContent),
title: selectedRevision.payload.content.title
? selectedRevision.payload.content.title + ' (copy)'
: undefined,
}
);
appState.notes.selectNote(duplicatedItem.uuid);
dismissModal();
}
};
useEffect(() => {
const fetchTemplateNote = async () => {
if (selectedRevision) {
const newTemplateNote = (await application.mutator.createTemplateItem(
ContentType.Note,
selectedRevision.payload.content
)) as SNNote;
setTemplateNoteForRevision(newTemplateNote);
}
};
fetchTemplateNote();
}, [application, selectedRevision]);
const deleteSelectedRevision = () => {
if (!selectedRemoteEntry) {
return;
}
application.alertService
.confirm(
'Are you sure you want to delete this revision?',
'Delete revision?',
'Delete revision',
ButtonType.Danger,
'Cancel'
)
.then((shouldDelete) => {
if (shouldDelete) {
setIsDeletingRevision(true);
application.historyManager
.deleteRemoteRevision(note.uuid, selectedRemoteEntry)
.then((res) => {
if (res.error?.message) {
throw new Error(res.error.message);
}
fetchRemoteHistory();
setIsDeletingRevision(false);
})
.catch(console.error);
}
})
.catch(console.error);
};
return (
Note revision history
{showContentLockedScreen && !selectedRevision && (
)}
{selectedRevision && templateNoteForRevision && (
)}
{selectedRevision && (
{selectedRemoteEntry && (
)}
)}
);
});
export const RevisionHistoryModalWrapper: FunctionComponent =
observer(({ application, appState }) => {
if (!appState.notes.showRevisionHistoryModal) {
return null;
}
return (
);
});