You've already forked AstralRinth
forked from didirus/AstralRinth
refactor: migrate to common eslint+prettier configs (#4168)
* refactor: migrate to common eslint+prettier configs * fix: prettier frontend * feat: config changes * fix: lint issues * fix: lint * fix: type imports * fix: cyclical import issue * fix: lockfile * fix: missing dep * fix: switch to tabs * fix: continue switch to tabs * fix: rustfmt parity * fix: moderation lint issue * fix: lint issues * fix: ui intl * fix: lint issues * Revert "fix: rustfmt parity" This reverts commit cb99d2376c321d813d4b7fc7e2a213bb30a54711. * feat: revert last rs
This commit is contained in:
@@ -35,47 +35,47 @@ import { inject, provide } from 'vue'
|
||||
* @param contextName The description for injection key symbol.
|
||||
*/
|
||||
export function createContext<ContextValue>(
|
||||
providerComponentName: string | string[],
|
||||
contextName?: string,
|
||||
providerComponentName: string | string[],
|
||||
contextName?: string,
|
||||
) {
|
||||
const symbolDescription =
|
||||
typeof providerComponentName === 'string' && !contextName
|
||||
? `${providerComponentName}Context`
|
||||
: contextName
|
||||
const symbolDescription =
|
||||
typeof providerComponentName === 'string' && !contextName
|
||||
? `${providerComponentName}Context`
|
||||
: contextName
|
||||
|
||||
const injectionKey: InjectionKey<ContextValue | null> = Symbol(symbolDescription)
|
||||
const injectionKey: InjectionKey<ContextValue | null> = Symbol(symbolDescription)
|
||||
|
||||
/**
|
||||
* @param fallback The context value to return if the injection fails.
|
||||
*
|
||||
* @throws When context injection failed and no fallback is specified.
|
||||
* This happens when the component injecting the context is not a child of the root component providing the context.
|
||||
*/
|
||||
const injectContext = <T extends ContextValue | null | undefined = ContextValue>(
|
||||
fallback?: T,
|
||||
): T extends null ? ContextValue | null : ContextValue => {
|
||||
const context = inject(injectionKey, fallback)
|
||||
if (context) return context
|
||||
/**
|
||||
* @param fallback The context value to return if the injection fails.
|
||||
*
|
||||
* @throws When context injection failed and no fallback is specified.
|
||||
* This happens when the component injecting the context is not a child of the root component providing the context.
|
||||
*/
|
||||
const injectContext = <T extends ContextValue | null | undefined = ContextValue>(
|
||||
fallback?: T,
|
||||
): T extends null ? ContextValue | null : ContextValue => {
|
||||
const context = inject(injectionKey, fallback)
|
||||
if (context) return context
|
||||
|
||||
if (context === null)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return context as any
|
||||
if (context === null)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return context as any
|
||||
|
||||
throw new Error(
|
||||
`Injection \`${injectionKey.toString()}\` not found. Component must be used within ${
|
||||
Array.isArray(providerComponentName)
|
||||
? `one of the following components: ${providerComponentName.join(', ')}`
|
||||
: `\`${providerComponentName}\``
|
||||
}`,
|
||||
)
|
||||
}
|
||||
throw new Error(
|
||||
`Injection \`${injectionKey.toString()}\` not found. Component must be used within ${
|
||||
Array.isArray(providerComponentName)
|
||||
? `one of the following components: ${providerComponentName.join(', ')}`
|
||||
: `\`${providerComponentName}\``
|
||||
}`,
|
||||
)
|
||||
}
|
||||
|
||||
const provideContext = (contextValue: ContextValue) => {
|
||||
provide(injectionKey, contextValue)
|
||||
return contextValue
|
||||
}
|
||||
const provideContext = (contextValue: ContextValue) => {
|
||||
provide(injectionKey, contextValue)
|
||||
return contextValue
|
||||
}
|
||||
|
||||
return [injectContext, provideContext] as const
|
||||
return [injectContext, provideContext] as const
|
||||
}
|
||||
|
||||
export * from './web-notifications'
|
||||
|
||||
@@ -1,133 +1,133 @@
|
||||
import { createContext } from '.'
|
||||
|
||||
export interface WebNotification {
|
||||
id: string | number
|
||||
title?: string
|
||||
text?: string
|
||||
type?: 'error' | 'warning' | 'success' | 'info'
|
||||
errorCode?: string
|
||||
count?: number
|
||||
timer?: NodeJS.Timeout
|
||||
id: string | number
|
||||
title?: string
|
||||
text?: string
|
||||
type?: 'error' | 'warning' | 'success' | 'info'
|
||||
errorCode?: string
|
||||
count?: number
|
||||
timer?: NodeJS.Timeout
|
||||
}
|
||||
|
||||
export type NotificationPanelLocation = 'left' | 'right'
|
||||
|
||||
export abstract class AbstractWebNotificationManager {
|
||||
protected readonly AUTO_DISMISS_DELAY_MS = 30 * 1000
|
||||
protected readonly AUTO_DISMISS_DELAY_MS = 30 * 1000
|
||||
|
||||
abstract getNotifications(): WebNotification[]
|
||||
abstract getNotificationLocation(): NotificationPanelLocation
|
||||
abstract setNotificationLocation(location: NotificationPanelLocation): void
|
||||
abstract getNotifications(): WebNotification[]
|
||||
abstract getNotificationLocation(): NotificationPanelLocation
|
||||
abstract setNotificationLocation(location: NotificationPanelLocation): void
|
||||
|
||||
protected abstract addNotificationToStorage(notification: WebNotification): void
|
||||
protected abstract removeNotificationFromStorage(id: string | number): void
|
||||
protected abstract removeNotificationFromStorageByIndex(index: number): void
|
||||
protected abstract clearAllNotificationsFromStorage(): void
|
||||
protected abstract addNotificationToStorage(notification: WebNotification): void
|
||||
protected abstract removeNotificationFromStorage(id: string | number): void
|
||||
protected abstract removeNotificationFromStorageByIndex(index: number): void
|
||||
protected abstract clearAllNotificationsFromStorage(): void
|
||||
|
||||
addNotification = (notification: Partial<WebNotification>): WebNotification => {
|
||||
const existingNotif = this.findExistingNotification(notification)
|
||||
addNotification = (notification: Partial<WebNotification>): WebNotification => {
|
||||
const existingNotif = this.findExistingNotification(notification)
|
||||
|
||||
if (existingNotif) {
|
||||
this.refreshNotificationTimer(existingNotif)
|
||||
existingNotif.count = (existingNotif.count || 0) + 1
|
||||
return existingNotif
|
||||
}
|
||||
if (existingNotif) {
|
||||
this.refreshNotificationTimer(existingNotif)
|
||||
existingNotif.count = (existingNotif.count || 0) + 1
|
||||
return existingNotif
|
||||
}
|
||||
|
||||
const newNotification = this.createNotification(notification)
|
||||
this.setNotificationTimer(newNotification)
|
||||
this.addNotificationToStorage(newNotification)
|
||||
return newNotification
|
||||
}
|
||||
const newNotification = this.createNotification(notification)
|
||||
this.setNotificationTimer(newNotification)
|
||||
this.addNotificationToStorage(newNotification)
|
||||
return newNotification
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated You should use `addNotification` instead to provide a more human-readable error message to the user.
|
||||
*/
|
||||
handleError = (error: Error): void => {
|
||||
this.addNotification({
|
||||
title: 'An error occurred',
|
||||
text: error.message ?? error,
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
/**
|
||||
* @deprecated You should use `addNotification` instead to provide a more human-readable error message to the user.
|
||||
*/
|
||||
handleError = (error: Error): void => {
|
||||
this.addNotification({
|
||||
title: 'An error occurred',
|
||||
text: error.message ?? error,
|
||||
type: 'error',
|
||||
})
|
||||
}
|
||||
|
||||
removeNotification = (id: string | number): WebNotification | undefined => {
|
||||
const notifications = this.getNotifications()
|
||||
const notification = notifications.find((n) => n.id === id)
|
||||
removeNotification = (id: string | number): WebNotification | undefined => {
|
||||
const notifications = this.getNotifications()
|
||||
const notification = notifications.find((n) => n.id === id)
|
||||
|
||||
if (notification) {
|
||||
this.clearNotificationTimer(notification)
|
||||
this.removeNotificationFromStorage(id)
|
||||
}
|
||||
if (notification) {
|
||||
this.clearNotificationTimer(notification)
|
||||
this.removeNotificationFromStorage(id)
|
||||
}
|
||||
|
||||
return notification
|
||||
}
|
||||
return notification
|
||||
}
|
||||
|
||||
removeNotificationByIndex = (index: number): WebNotification | null => {
|
||||
const notifications = this.getNotifications()
|
||||
removeNotificationByIndex = (index: number): WebNotification | null => {
|
||||
const notifications = this.getNotifications()
|
||||
|
||||
if (index >= 0 && index < notifications.length) {
|
||||
const notification = notifications[index]
|
||||
this.clearNotificationTimer(notification)
|
||||
this.removeNotificationFromStorageByIndex(index)
|
||||
if (index >= 0 && index < notifications.length) {
|
||||
const notification = notifications[index]
|
||||
this.clearNotificationTimer(notification)
|
||||
this.removeNotificationFromStorageByIndex(index)
|
||||
|
||||
return notification
|
||||
}
|
||||
return notification
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
clearAllNotifications = (): void => {
|
||||
const notifications = this.getNotifications()
|
||||
notifications.forEach((notification) => {
|
||||
this.clearNotificationTimer(notification)
|
||||
})
|
||||
this.clearAllNotificationsFromStorage()
|
||||
}
|
||||
clearAllNotifications = (): void => {
|
||||
const notifications = this.getNotifications()
|
||||
notifications.forEach((notification) => {
|
||||
this.clearNotificationTimer(notification)
|
||||
})
|
||||
this.clearAllNotificationsFromStorage()
|
||||
}
|
||||
|
||||
setNotificationTimer = (notification: WebNotification): void => {
|
||||
if (!notification) return
|
||||
setNotificationTimer = (notification: WebNotification): void => {
|
||||
if (!notification) return
|
||||
|
||||
this.clearNotificationTimer(notification)
|
||||
this.clearNotificationTimer(notification)
|
||||
|
||||
notification.timer = setTimeout(() => {
|
||||
this.removeNotification(notification.id)
|
||||
}, this.AUTO_DISMISS_DELAY_MS)
|
||||
}
|
||||
notification.timer = setTimeout(() => {
|
||||
this.removeNotification(notification.id)
|
||||
}, this.AUTO_DISMISS_DELAY_MS)
|
||||
}
|
||||
|
||||
stopNotificationTimer = (notification: WebNotification): void => {
|
||||
this.clearNotificationTimer(notification)
|
||||
}
|
||||
stopNotificationTimer = (notification: WebNotification): void => {
|
||||
this.clearNotificationTimer(notification)
|
||||
}
|
||||
|
||||
private refreshNotificationTimer(notification: WebNotification): void {
|
||||
this.setNotificationTimer(notification)
|
||||
}
|
||||
private refreshNotificationTimer(notification: WebNotification): void {
|
||||
this.setNotificationTimer(notification)
|
||||
}
|
||||
|
||||
private clearNotificationTimer(notification: WebNotification): void {
|
||||
if (notification.timer) {
|
||||
clearTimeout(notification.timer)
|
||||
notification.timer = undefined
|
||||
}
|
||||
}
|
||||
private clearNotificationTimer(notification: WebNotification): void {
|
||||
if (notification.timer) {
|
||||
clearTimeout(notification.timer)
|
||||
notification.timer = undefined
|
||||
}
|
||||
}
|
||||
|
||||
private findExistingNotification(
|
||||
notification: Partial<WebNotification>,
|
||||
): WebNotification | undefined {
|
||||
return this.getNotifications().find(
|
||||
(existing) =>
|
||||
existing.text === notification.text &&
|
||||
existing.title === notification.title &&
|
||||
existing.type === notification.type,
|
||||
)
|
||||
}
|
||||
private findExistingNotification(
|
||||
notification: Partial<WebNotification>,
|
||||
): WebNotification | undefined {
|
||||
return this.getNotifications().find(
|
||||
(existing) =>
|
||||
existing.text === notification.text &&
|
||||
existing.title === notification.title &&
|
||||
existing.type === notification.type,
|
||||
)
|
||||
}
|
||||
|
||||
private createNotification(notification: Partial<WebNotification>): WebNotification {
|
||||
return {
|
||||
...notification,
|
||||
id: new Date().getTime(),
|
||||
count: 1,
|
||||
} as WebNotification
|
||||
}
|
||||
private createNotification(notification: Partial<WebNotification>): WebNotification {
|
||||
return {
|
||||
...notification,
|
||||
id: new Date().getTime(),
|
||||
count: 1,
|
||||
} as WebNotification
|
||||
}
|
||||
}
|
||||
|
||||
export const [injectNotificationManager, provideNotificationManager] =
|
||||
createContext<AbstractWebNotificationManager>('root', 'notificationManager')
|
||||
createContext<AbstractWebNotificationManager>('root', 'notificationManager')
|
||||
|
||||
Reference in New Issue
Block a user