aboutsummaryrefslogtreecommitdiffstats
path: root/apps/updatenotification/src/init.ts
blob: ebfbf68996af823a3b4e725f10b953eb421a2874 (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
70
71
72
73
74
75
76
77
78
79
/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
import { subscribe } from '@nextcloud/event-bus'
import { loadState } from '@nextcloud/initial-state'
import { generateOcsUrl } from '@nextcloud/router'
import Vue, { defineAsyncComponent } from 'vue'
import axios from '@nextcloud/axios'

const navigationEntries = loadState('core', 'apps', {})

const DialogVue = defineAsyncComponent(() => import('./components/AppChangelogDialog.vue'))

/**
 * Show the app changelog dialog
 *
 * @param appId The app to show the changelog for
 * @param version Optional version to show
 */
function showDialog(appId: string, version?: string) {
	const element = document.createElement('div')
	document.body.appendChild(element)

	return new Promise((resolve) => {
		let dismissed = false

		const dialog = new Vue({
			el: element,
			render: (h) => h(DialogVue, {
				props: {
					appId,
					version,
				},
				on: {
					dismiss: () => { dismissed = true },
					'update:open': (open: boolean) => {
						if (!open) {
							dialog.$destroy?.()
							resolve(dismissed)

							if (dismissed && appId in navigationEntries) {
								window.location = navigationEntries[appId].href
							}
						}
					},
				},
			}),
		})
	})
}

interface INotificationActionEvent {
	cancelAction: boolean
	notification: Readonly<{
		notificationId: number
		objectId: string
		objectType: string
	}>
	action: Readonly<{
		url: string
		type: 'WEB'|'GET'|'POST'|'DELETE'
	}>,
}

subscribe('notifications:action:execute', (event: INotificationActionEvent) => {
	if (event.notification.objectType === 'app_updated') {
		event.cancelAction = true

		// eslint-disable-next-line @typescript-eslint/no-unused-vars
		const [_, app, version, __] = event.action.url.match(/(?<=\/)([^?]+)?version=((\d+.?)+)/) ?? []
		showDialog((app as string|undefined) || (event.notification.objectId as string), version)
			.then((dismissed) => {
				if (dismissed) {
					axios.delete(generateOcsUrl('apps/notifications/api/v2/notifications/{id}', { id: event.notification.notificationId }))
				}
			})
	}
})