summaryrefslogtreecommitdiff
path: root/packages/frontend/src/utility/get-note-versions-menu.ts
blob: aac03756401effb480edb3a2fd0a5649877bf49a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * SPDX-FileCopyrightText: marie and other Sharkey contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { defineAsyncComponent } from 'vue';
import * as Misskey from 'misskey-js';
import { misskeyApi } from './misskey-api.js';
import { dateTimeFormat } from './intl-const.js';
import type { MenuItem } from '@/types/menu.js';
import * as os from '@/os.js';

interface NoteEdit {
	oldDate: string;
	updatedAt: string;
	text: string | null;
}

export async function getNoteVersionsMenu(props: { note: Misskey.entities.Note }) {
	const isRenote = (
		props.note.renote != null &&
		props.note.text == null &&
		!props.note.fileIds?.length &&
		props.note.poll == null
	);

	const appearNote = isRenote ? props.note.renote as Misskey.entities.Note : props.note;

	const cleanups = [] as (() => void)[];

	function openVersion(info: NoteEdit): void {
		const { dispose } = os.popup(defineAsyncComponent(() => import('@/components/SkOldNoteWindow.vue')), {
			note: appearNote,
			oldText: info.text ?? '',
			updatedAt: info.updatedAt,
		}, {
			closed: () => dispose(),
		});
	}

	const menu: MenuItem[] = [];
	const statePromise = misskeyApi('notes/versions', {
		noteId: appearNote.id,
	});

	await statePromise.then((versions) => {
		for (const edit of versions) {
			menu.push({
				icon: 'ph-pencil-simple ph-bold ph-lg',
				text: dateTimeFormat.format(new Date(edit.oldDate)),
				action: () => openVersion(edit),
			});
		}
	});

	const cleanup = () => {
		if (_DEV_) console.log('note menu cleanup', cleanups);
		for (const cl of cleanups) {
			cl();
		}
	};

	return {
		menu,
		cleanup,
	};
}