www/src/app/http/middlewares/SiteTheme.middleware.ts
garrettmills 17b51b82cc
Some checks reported errors
continuous-integration/drone Build is passing
continuous-integration/drone/promote/production Build was killed
New 70s theme
2023-01-15 19:26:19 -06:00

38 lines
1.2 KiB
TypeScript

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)
}
}