Skip to content

Commit

Permalink
feat: add static mode
Browse files Browse the repository at this point in the history
  • Loading branch information
robb-j committed Nov 25, 2022
1 parent ba5e337 commit 108d5fa
Show file tree
Hide file tree
Showing 16 changed files with 1,188 additions and 789 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
.DS_Store
node_modules
server/data
server/static
1,492 changes: 757 additions & 735 deletions client/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@fortawesome/free-solid-svg-icons": "^5.15.4",
"@fortawesome/vue-fontawesome": "^2.0.8",
"@openlab/deconf-shared": "^3.5.0",
"@openlab/deconf-ui-toolkit": "^2.28.1",
"@openlab/deconf-ui-toolkit": "^2.29.0",
"bulma": "^0.9.4",
"copy-to-clipboard": "^3.3.2",
"core-js": "^3.26.0",
Expand Down
7 changes: 7 additions & 0 deletions client/public/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ window.CONFIG = {
JWT_ISSUER: 'mozfest-localhost',
// DISABLE_SOCKETS: false,
}

//
// Static mode
//
window.CONFIG.SERVER_URL = 'http://localhost:3000/static/schedule/'
window.CONFIG.STATIC_BUILD = true
window.CONFIG.DISABLE_SOCKETS = true
39 changes: 39 additions & 0 deletions client/src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { EnvRecord } from '@/plugins/env-plugin'
import {
PageFlag,
ScheduleRecord as DeconfScheduleRecord,
Session,
} from '@openlab/deconf-shared'
import {
DeconfApiClient,
DeconfStaticFiles,
StaticDeconfApiClient,
} from '@openlab/deconf-ui-toolkit'

export interface MozConferenceConfig {
atrium?: PageFlag
Expand Down Expand Up @@ -40,3 +47,35 @@ export interface MozConferenceConfig {
export type ScheduleRecord = Omit<DeconfScheduleRecord, 'settings'> & {
settings: MozConferenceConfig
}

interface MozApiClient {
getWhatsOn(): Promise<Session[]>
}

export function pickApi(env: EnvRecord) {
return env.STATIC_BUILD
? new StaticApiClient(env.SERVER_URL.href)
: new LiveApiClient(env.SERVER_URL.href)
}

type MozStaticFiles = DeconfStaticFiles & {
'whats-on.json': Session[]
}

export class LiveApiClient extends DeconfApiClient implements MozApiClient {
async getWhatsOn(): Promise<Session[]> {
const response = await this.fetchJson<{ sessions: Session[] }>(
'schedule/whats-on'
)
return response?.sessions ?? []
}
}

export class StaticApiClient
extends StaticDeconfApiClient<MozStaticFiles>
implements MozApiClient
{
getWhatsOn(): Promise<Session[]> {
return this.getStaticFile('whats-on.json')
}
}
19 changes: 10 additions & 9 deletions client/src/plugins/env-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

import _Vue from 'vue'

// TODO: replace `DISABLE_SOCKETS` with `SOCKETS_URL`

export interface EnvRecord {
readonly SELF_URL: string
readonly SERVER_URL: string
readonly SELF_URL: URL
readonly SERVER_URL: URL
readonly BUILD_NAME: string | null
readonly GA_TOKEN: string | null
readonly JWT_ISSUER: string | null
readonly DISABLE_SOCKETS: boolean
readonly STATIC_BUILD: boolean
}

// window.CONFIG is from public/config.js

function normaliseUrl(input: string) {
return input.replace(/\/*$/, '/')
}

interface WindowWithConfig {
CONFIG?: Record<string, string | undefined>
}
Expand All @@ -28,15 +27,17 @@ const {
GA_TOKEN = null,
JWT_ISSUER = 'deconf-app',
DISABLE_SOCKETS = false,
STATIC_BUILD = false,
} = (window as WindowWithConfig).CONFIG || {}

export const env: EnvRecord = Object.seal({
SELF_URL: normaliseUrl(SELF_URL),
SERVER_URL: normaliseUrl(SERVER_URL),
export const env = Object.seal<EnvRecord>({
SELF_URL: new URL(SELF_URL),
SERVER_URL: new URL(SERVER_URL),
BUILD_NAME,
GA_TOKEN,
JWT_ISSUER,
DISABLE_SOCKETS: Boolean(DISABLE_SOCKETS),
STATIC_BUILD: Boolean(STATIC_BUILD),
})

//
Expand Down
2 changes: 1 addition & 1 deletion client/src/plugins/socketio-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class SocketIoPlugin {
this.sharedSocket?.emit('deauth')
}

constructor(serverUrl: string) {
constructor(serverUrl: URL) {
// Concat the path onto the server url, e.g.
// "/api/socket.io"
// "/socket.io"
Expand Down
10 changes: 3 additions & 7 deletions client/src/store/api-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import {
createApiStoreActions,
createApiStoreModule,
decodeJwt,
DeconfApiClient,
FullAuthToken,
} from '@openlab/deconf-ui-toolkit'
import { Session } from '@openlab/deconf-shared'
import { env } from '@/plugins/env-plugin'

import { SocketIoPlugin } from '@/plugins/socketio-plugin'
import { pickApi } from '@/lib/api'

// TODO: work out how to filter themes again
// import { StorageKey, themeAllowlist } from '@/lib/module'

export function apiModule(): ApiStoreModule {
const apiClient = new DeconfApiClient(env.SERVER_URL)
const apiClient = pickApi(env)

const baseActions = createApiStoreActions(apiClient)

Expand Down Expand Up @@ -60,11 +60,7 @@ export function apiModule(): ApiStoreModule {
// return data !== null
// },
async fetchWhatsOn() {
const response = await apiClient.fetchJson<{ sessions: Session[] }>(
'schedule/whats-on'
)

return response?.sessions ?? []
return apiClient.getWhatsOn()
},
},
}
Expand Down
Loading

0 comments on commit 108d5fa

Please sign in to comment.