Mirror / foldkit-mirror

Add remembered local state with Mirror.kv

For state that should survive reload outside the URL, use an Effect KeyValueStore. A key-value mirror restores through a Message, so give it a variant in your Message union:

ts
const Model = Schema.Struct({
  filter: Schema.Literals(['all', 'active', 'done']),
  page: Schema.Number,
  sidebar: Schema.Literals(['open', 'closed']),
  draft: Schema.String,
})
type Model = typeof Model.Type

const Message = defineMessageUnion({
  GotPrefsMessage: { message: Mirror.Message },
  UrlChanged: { url: Url },
})
type Message = typeof Message.Type

Build App from that Model and Message, declare Filters and Prefs, and fold the key-value mirror under GotPrefsMessage:

ts
import type { KeyValueStore } from 'effect/persistence'
import type * as Update from 'foldkit/update'
import { Projection, Surface } from 'foldkit-surface'

type Return = Update.Return<Model, Message, KeyValueStore.KeyValueStore>

const initial: Model = { filter: 'all', page: 1, sidebar: 'open', draft: '' }
const App = Surface.application({ Model, Message, initial, update })

const Filters = Mirror.url(App, {
  name: 'filters',
  fields: [App.model.filter, App.model.page],
})
const Prefs = Mirror.kv(App, {
  key: 'todo/prefs',
  fields: Projection.pick(App.model.sidebar, App.model.draft),
})

const foldPrefs = Mirror.fold(Prefs, message => Message.GotPrefsMessage({ message }))

Match the union in update and start foldPrefs.init on boot:

ts
function update(model: Model, message: Message): Return {
  return Message.match(message, {
    GotPrefsMessage: ({ message }) => foldPrefs(model, message),
    UrlChanged: ({ url }) => ({ model: Filters.urlChanged(model, url) }),
  })
}

const init = (url: Url): Return =>
  // URL wins first because startup already has it; the store answers later.
  foldPrefs.init(Filters.reduce(initial, url))

foldPrefs.init attaches the Prefs.restore Command lifted to yield GotPrefsMessage, so Story and Scene tests can resolve Prefs.restore directly.

Alternatively, spread Mirror.messages into your union and narrow with Mirror.reduces(message):

ts
import { Match } from 'effect'

const Message = defineMessageUnion({ ...Mirror.messages, UrlChanged: { url: Url } })
type Message = typeof Message.Type

function update(model: Model, message: Message): Return {
  if (Mirror.reduces(message)) return { model: Prefs.reduce(model, message) }
  return Match.value(message).pipe(
    Match.tag('UrlChanged', ({ url }) => ({ model: Filters.urlChanged(model, url) })),
    Match.orElse(() => ({ model })),
  )
}

const init = (url: Url): Return => ({
  model: Filters.reduce(initial, url),
  commands: [Prefs.restore],
})

Then spread both mirrors into subscriptions so changes write out from the same Model:

ts
import type { KeyValueStore } from 'effect/persistence'
import * as Subscription from 'foldkit/subscription'

const subscriptions = Subscription.make<
  Model,
  Message,
  KeyValueStore.KeyValueStore
>()(() => ({
  ...Filters.subscriptions,
  ...Prefs.subscriptions,
}))

sidebar and draft stay ordinary Model fields; the key-value store only persists them between sessions.