You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.2 KiB

import {Middleware, Injectable, Inject, Config, Session} from '@extollo/lib'
import {ColorPalette} from '../../configs/app.config'
/**
* SiteTheme Middleware
* --------------------------------------------
* Determines the correct color theme for the request.
*/
@Injectable()
export class SiteTheme extends Middleware {
@Inject()
protected readonly config!: Config
@Inject()
protected readonly session!: Session
public async apply() {
const themes = this.config.get('app.colors') as Record<string, ColorPalette>
const themeKeys = Object.keys(themes)
const existingThemeName = this.session.get('theme.name')
const forceNewTheme = Boolean(this.request.input('forceNewTheme'))
const explicitTheme = String(this.request.input('theme') || '')
if ( explicitTheme && themeKeys.includes(explicitTheme) ) {
this.session.set('theme.name', explicitTheme)
return
}
if ( existingThemeName && !forceNewTheme ) {
return
}
const themeName = themeKeys[Math.floor(Math.random() * themeKeys.length)]
this.session.set('theme.name', themeName)
}
}