Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

eventClientContext.tsx

146 lines3.4 KB

1import dayjs from 'dayjs'
2import {
3 createContext,
4 createEffect,
5 createMemo,
6 onCleanup,
7 useContext,
8} from 'solid-js'
9import { createStore } from 'solid-js/store'
10import { formEventClient } from '@tanstack/form-core'
11​
12import type { ParentComponent } from 'solid-js'
13import type {
14 AnyFormOptions,
15 AnyFormState,
16 BroadcastFormSubmissionState,
17} from '@tanstack/form-core'
18import type { Dayjs } from 'dayjs'
19​
20type BroadcastFormSubmissionStateWithoutId =
21 BroadcastFormSubmissionState extends infer T
22 ? T extends any
23 ? Omit<T, 'id'>
24 : never
25 : never
26​
27export type DevtoolsFormState = {
28 id: string
29 state: AnyFormState
30 date: Dayjs
31 options: AnyFormOptions
32 history: Array<BroadcastFormSubmissionStateWithoutId>
34​
35function useProviderValue() {
36 const [store, setStore] = createStore<Array<DevtoolsFormState>>([])
37​
38 createEffect(() => {
39 const cleanup = formEventClient.on('form-api', (e) => {
40 const id = e.payload.id
41 const existingIndex = store.findIndex((item) => item.id === id)
42​
43 if (existingIndex > -1) {
44 setStore(existingIndex, {
45 state: e.payload.state,
46 options: e.payload.options,
47 date: dayjs(),
48 })
49 } else {
50 setStore((prev) => [
51 ...prev,
52 {
53 id,
54 state: e.payload.state,
55 options: e.payload.options,
56 date: dayjs(),
57 history: [],
58 },
59 ])
60 }
61 })
62​
63 onCleanup(cleanup)
64 })
65​
66 createEffect(() => {
67 const cleanup = formEventClient.on('form-state', (e) => {
68 const id = e.payload.id
69 const existingIndex = store.findIndex((item) => item.id === id)
70​
71 if (existingIndex > -1) {
72 setStore(existingIndex, {
73 state: e.payload.state,
74 date: dayjs(),
75 })
76 } else {
77 setStore((prev) => [
78 ...prev,
79 {
80 id,
81 state: e.payload.state,
82 options: {},
83 date: dayjs(),
84 history: [],
85 },
86 ])
87 }
88 })
89​
90 onCleanup(cleanup)
91 })
92​
93 createEffect(() => {
94 const cleanup = formEventClient.on('form-submission', (e) => {
95 const id = e.payload.id
96 const existingIndex = store.findIndex((item) => item.id === id)
97​
98 const existing = store[existingIndex]
99​
100 if (existingIndex > -1 && existing) {
101 const { id: _, ...rest } = e.payload
102 const newHistory = [rest, ...existing.history].slice(0, 5)
103 setStore(existingIndex, 'history', newHistory)
104 }
105 })
106​
107 onCleanup(cleanup)
108 })
109​
110 createEffect(() => {
111 const cleanup = formEventClient.on('form-unmounted', (e) => {
112 setStore((prev) => prev.filter((item) => item.id !== e.payload.id))
113 })
114​
115 onCleanup(cleanup)
116 })
117​
118 return { store }
120​
121type ContextType = ReturnType<typeof useProviderValue>
122​
123const FormEventClientContext = createContext<ContextType | undefined>(undefined)
124​
125export const FormEventClientProvider: ParentComponent = (props) => {
126 const value = useProviderValue()
127​
128 return (
129 <FormEventClientContext.Provider value={value}>
130 {props.children}
131 </FormEventClientContext.Provider>
132 )
134​
135export function useFormEventClient() {
136 const context = useContext(FormEventClientContext)
137​
138 if (context === undefined) {
139 throw new Error(
140 `useFormEventClient must be used within a FormEventClientContext`,
141 )
142 }
143​
144 const memoContext = createMemo(() => context.store)
145 return { store: memoContext }