All files / app flags.ts

0% Statements 0/10
100% Branches 0/0
0% Functions 0/4
0% Lines 0/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34                                                                   
import { Effect, Option, Schema } from 'effect'
import { KeyValueStore } from 'effect/persistence'
 
import { BrowserKeyValueStore } from '@effect/platform-browser'
 
import { Viewport } from '../core/domain'
import * as Settings from '../submodels/settings'
import type { Flags } from './model'
import { readCamera, readCueQuery } from './url'
 
/** Saved settings that are missing, unreadable or an older shape fall back to the defaults. */
const loadSettings = Effect.gen(function* () {
  // NOTE: KeyValueStore is marked unstable in Effect 4; Foldkit's examples use it for localStorage too.
  // @effect-diagnostics-next-line unstableApiUsage:off
  const store = yield* KeyValueStore.KeyValueStore
  const json = yield* Effect.fromOption(
    Option.fromNullishOr(yield* store.get(Settings.STORAGE_KEY)),
  )
  return yield* Schema.decodeEffect(Settings.ValuesJsonString)(json)
}).pipe(Effect.option, Effect.provide(BrowserKeyValueStore.layerLocalStorage))
 
const matches = (query: string) =>
  Effect.sync(() => window.matchMedia(query).matches)
 
const href = Effect.sync(() => window.location.href)
 
export const flags: Effect.Effect<Flags> = Effect.all({
  maybeSavedSettings: loadSettings,
  maybeCueQuery: Effect.map(href, readCueQuery),
  maybeCamera: Effect.map(href, readCamera),
  layout: Effect.map(matches(Viewport.COMPACT_QUERY), Viewport.toLayout),
  pointer: Effect.map(matches(Viewport.HOVER_QUERY), Viewport.toPointer),
})