Files
effect-fc/packages/effect-fc/src/hooks/Hooks/useInput.ts
Julien Valverdé e7c85a43eb
All checks were successful
Lint / lint (push) Successful in 11s
Fix
2025-08-11 07:56:43 +02:00

65 lines
2.7 KiB
TypeScript

import { type Duration, Effect, flow, identity, Option, type ParseResult, Ref, Schema, Stream, SubscriptionRef } from "effect"
import * as React from "react"
import { useCallbackSync } from "./useCallbackSync.js"
import { useFork } from "./useFork.js"
import { useOnce } from "./useOnce.js"
import { useSubscribeRefs } from "./useSubscribeRefs.js"
export namespace useInput {
export interface Options<A, R> {
readonly schema: Schema.Schema<A, string, R>
readonly ref: SubscriptionRef.SubscriptionRef<A>
readonly debounce?: Duration.DurationInput
}
export interface Result {
readonly value: string
readonly onChange: React.ChangeEventHandler<HTMLInputElement | HTMLTextAreaElement>
readonly error: Option.Option<ParseResult.ParseError>
}
}
export const useInput: {
<A, R>(options: useInput.Options<A, R>): Effect.Effect<useInput.Result, ParseResult.ParseError, R>
} = Effect.fnUntraced(function* <A, R>(options: useInput.Options<A, R>) {
const internalRef = yield* useOnce(() => options.ref.pipe(
Effect.andThen(Schema.encode(options.schema)),
Effect.andThen(SubscriptionRef.make),
))
const [error, setError] = React.useState(Option.none<ParseResult.ParseError>())
yield* useFork(() => Effect.all([
// Sync the upstream state with the internal state
// Only mutate the internal state if the upstream encoded value is actually different. This avoids infinite re-render loops.
Stream.runForEach(options.ref.changes, upstreamValue =>
Effect.andThen(
Effect.all([Schema.encode(options.schema)(upstreamValue), internalRef]),
([encodedUpstreamValue, internalValue]) => Effect.when(
Ref.set(internalRef, encodedUpstreamValue),
() => encodedUpstreamValue !== internalValue,
),
)
),
// Sync all changes to the internal state with upstream
Stream.runForEach(
internalRef.changes.pipe(options.debounce ? Stream.debounce(options.debounce) : identity),
flow(
Schema.decode(options.schema),
Effect.andThen(v => Ref.set(options.ref, v)),
Effect.andThen(() => setError(Option.none())),
Effect.catchTag("ParseError", e => Effect.sync(() => setError(Option.some(e)))),
),
),
], { concurrency: "unbounded" }), [options.ref, options.schema, options.debounce, internalRef])
const [value] = yield* useSubscribeRefs(internalRef)
const onChange = yield* useCallbackSync((e: React.ChangeEvent<HTMLInputElement>) => Ref.set(
internalRef,
e.target.value,
), [internalRef])
return { value, onChange, error }
})