summaryrefslogtreecommitdiff
path: root/packages/frontend/src/scripts/get-note-versions-menu.ts
blob: 345cec9018e23c94dff57ed212bd3e285fd30ef4 (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
68
69
/*
 * SPDX-FileCopyrightText: marie and other Sharkey contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 */

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

export async function getNoteVersionsMenu(props: {
	note: Misskey.entities.Note;
	menuButton: Ref<HTMLElement>;
}) {
	const isRenote = (
		props.note.renote != null &&
		props.note.text == null &&
		props.note.fileIds.length === 0 &&
		props.note.poll == null
	);

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

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

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

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

	await statePromise.then((versions) => {
		for (const edit of versions) {
			const _time = edit.oldDate == null ? NaN :
				typeof edit.oldDate === 'number' ? edit.oldDate :
				(edit.oldDate instanceof Date ? edit.oldDate : new Date(edit.oldDate)).getTime();

			menu.push({
				icon: 'ph-pencil-simple ph-bold ph-lg',
				text: _time ? dateTimeFormat.format(_time) : dateTimeFormat.format(new Date(edit.updatedAt)),
				action: () => openVersion(edit),
			});
		}
	});

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

	return {
		menu,
		cleanup,
	};
}