All files / app command.ts

20% Statements 1/5
100% Branches 0/0
0% Functions 0/3
20% Lines 1/5

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                2x                              
import { Effect, Schema } from 'effect'
import { Command } from 'foldkit'
 
import { Camera, LatLon } from '../core/domain'
import { Message } from './message'
import { withState } from './url'
 
/** Keep the cue and camera in the URL without adding history entries, so the link can be shared. */
export const ReplaceUrl = Command.define('ReplaceUrl', {
  args: {
    maybeCue: Schema.Option(LatLon.LatLon),
    maybeCamera: Schema.Option(Camera.Camera),
  },
  messages: [Message.SucceededReplaceUrl, Message.FailedReplaceUrl],
  execute: ({ maybeCue, maybeCamera }) =>
    Effect.try(() => {
      const url = withState(window.location.href, maybeCue, maybeCamera)
      window.history.replaceState(window.history.state, '', url)
    }).pipe(
      Effect.as(Message.SucceededReplaceUrl()),
      Effect.catch(() => Effect.succeed(Message.FailedReplaceUrl())),
    ),
})