mirror of
https://github.com/modrinth/code.git
synced 2026-08-27 01:54:47 +00:00
feat: add hide and show download items (#7106)
* feat: add hide and show download items * pnpm prepr
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { Component } from 'vue'
|
||||
import { type Component, type Ref, ref } from 'vue'
|
||||
|
||||
import { createContext } from '.'
|
||||
|
||||
@@ -25,7 +25,6 @@ export interface PopupNotificationProgressItem {
|
||||
progressCurrent?: number
|
||||
progressTotal?: number
|
||||
dismissible?: boolean
|
||||
onDismiss?: () => void | Promise<void>
|
||||
buttons?: PopupNotificationButton[]
|
||||
}
|
||||
|
||||
@@ -73,15 +72,51 @@ export interface PopupNotification {
|
||||
buttons?: PopupNotificationButton[]
|
||||
toast?: PopupNotificationToast
|
||||
dismissible?: boolean
|
||||
onDismiss?: () => void | Promise<void>
|
||||
autoCloseMs?: number | null
|
||||
timer?: NodeJS.Timeout
|
||||
}
|
||||
|
||||
export interface PopupNotificationDownloadState {
|
||||
total: number
|
||||
hidden: number
|
||||
}
|
||||
|
||||
export abstract class AbstractPopupNotificationManager {
|
||||
protected readonly DEFAULT_AUTO_CLOSE_MS = 30 * 1000
|
||||
private readonly hiddenDownloadItemKeys: Ref<Set<string>> = ref(new Set())
|
||||
|
||||
abstract getNotifications(): PopupNotification[]
|
||||
|
||||
getDownloadState = (): PopupNotificationDownloadState => {
|
||||
const itemKeys = this.getDownloadNotifications().flatMap((notification) =>
|
||||
this.getDownloadItemKeys(notification),
|
||||
)
|
||||
return {
|
||||
total: itemKeys.length,
|
||||
hidden: itemKeys.filter((key) => this.hiddenDownloadItemKeys.value.has(key)).length,
|
||||
}
|
||||
}
|
||||
|
||||
getVisibleNotifications = (): PopupNotification[] =>
|
||||
this.getNotifications().filter(
|
||||
(notification) =>
|
||||
!this.isDownloadNotification(notification) ||
|
||||
this.getDownloadItemKeys(notification).some(
|
||||
(key) => !this.hiddenDownloadItemKeys.value.has(key),
|
||||
),
|
||||
)
|
||||
|
||||
getVisibleDownloadProgressItems = (
|
||||
notification: PopupNotification,
|
||||
): PopupNotificationProgressItem[] =>
|
||||
(notification.progressItems ?? []).filter(
|
||||
(progressItem) =>
|
||||
!this.hiddenDownloadItemKeys.value.has(
|
||||
this.getDownloadItemKey(notification.id, progressItem.id),
|
||||
),
|
||||
)
|
||||
|
||||
protected abstract addNotificationToStorage(notification: PopupNotification): void
|
||||
protected abstract removeNotificationFromStorage(id: string | number): void
|
||||
protected abstract clearAllNotificationsFromStorage(): void
|
||||
@@ -103,6 +138,9 @@ export abstract class AbstractPopupNotificationManager {
|
||||
const notification = notifications.find((n) => n.id === id)
|
||||
if (notification) {
|
||||
this.clearNotificationTimer(notification)
|
||||
this.getDownloadItemKeys(notification).forEach((key) =>
|
||||
this.hiddenDownloadItemKeys.value.delete(key),
|
||||
)
|
||||
this.removeNotificationFromStorage(id)
|
||||
}
|
||||
}
|
||||
@@ -110,6 +148,45 @@ export abstract class AbstractPopupNotificationManager {
|
||||
clearAllNotifications = (): void => {
|
||||
this.getNotifications().forEach((n) => this.clearNotificationTimer(n))
|
||||
this.clearAllNotificationsFromStorage()
|
||||
this.hiddenDownloadItemKeys.value.clear()
|
||||
}
|
||||
|
||||
hideDownloadItem = (notificationId: string | number, progressItemId: string): void => {
|
||||
const notification = this.getDownloadNotifications().find(
|
||||
(candidate) => candidate.id === notificationId,
|
||||
)
|
||||
if (!notification?.progressItems?.some((item) => item.id === progressItemId)) return
|
||||
|
||||
this.hiddenDownloadItemKeys.value.add(this.getDownloadItemKey(notificationId, progressItemId))
|
||||
if (
|
||||
this.getDownloadItemKeys(notification).every((key) =>
|
||||
this.hiddenDownloadItemKeys.value.has(key),
|
||||
)
|
||||
) {
|
||||
this.clearNotificationTimer(notification)
|
||||
}
|
||||
}
|
||||
|
||||
toggleDownloadNotifications = (): void => {
|
||||
const downloadNotifications = this.getDownloadNotifications()
|
||||
const hasHiddenDownloads = downloadNotifications.some((notification) =>
|
||||
this.getDownloadItemKeys(notification).some((key) =>
|
||||
this.hiddenDownloadItemKeys.value.has(key),
|
||||
),
|
||||
)
|
||||
|
||||
if (hasHiddenDownloads) {
|
||||
this.hiddenDownloadItemKeys.value.clear()
|
||||
downloadNotifications.forEach((notification) => this.setNotificationTimer(notification))
|
||||
return
|
||||
}
|
||||
|
||||
downloadNotifications.forEach((notification) => {
|
||||
this.getDownloadItemKeys(notification).forEach((key) =>
|
||||
this.hiddenDownloadItemKeys.value.add(key),
|
||||
)
|
||||
this.clearNotificationTimer(notification)
|
||||
})
|
||||
}
|
||||
|
||||
setNotificationTimer = (notification: PopupNotification): void => {
|
||||
@@ -134,6 +211,30 @@ export abstract class AbstractPopupNotificationManager {
|
||||
notification.timer = undefined
|
||||
}
|
||||
}
|
||||
|
||||
private isDownloadNotification(notification: PopupNotification): boolean {
|
||||
return notification.type === 'download' || notification.toast?.type === 'instance-download'
|
||||
}
|
||||
|
||||
private getDownloadNotifications(): PopupNotification[] {
|
||||
return this.getNotifications().filter((notification) =>
|
||||
this.isDownloadNotification(notification),
|
||||
)
|
||||
}
|
||||
|
||||
private getDownloadItemKeys(notification: PopupNotification): string[] {
|
||||
if (!this.isDownloadNotification(notification)) return []
|
||||
if (notification.progressItems?.length) {
|
||||
return notification.progressItems.map((progressItem) =>
|
||||
this.getDownloadItemKey(notification.id, progressItem.id),
|
||||
)
|
||||
}
|
||||
return [this.getDownloadItemKey(notification.id)]
|
||||
}
|
||||
|
||||
private getDownloadItemKey(notificationId: string | number, progressItemId?: string): string {
|
||||
return JSON.stringify([typeof notificationId, notificationId, progressItemId ?? null])
|
||||
}
|
||||
}
|
||||
|
||||
export const [injectPopupNotificationManager, providePopupNotificationManager] =
|
||||
|
||||
Reference in New Issue
Block a user