Open History to see the last commit
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>
33}
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 if (existingIndex > -1 && store[existingIndex]) {
99 const { id: _, ...rest } = e.payload
100 const newHistory = [rest, ...store[existingIndex].history].slice(0, 5)
101 setStore(existingIndex, 'history', newHistory)
102 }
103 })
104β
105 onCleanup(cleanup)
106 })
107β
108 createEffect(() => {
109 const cleanup = formEventClient.on('form-unmounted', (e) => {
110 setStore((prev) => prev.filter((item) => item.id !== e.payload.id))
111 })
112β
113 onCleanup(cleanup)
114 })
115β
116 return { store }
117}
118β
119type ContextType = ReturnType<typeof useProviderValue>
120β
121const FormEventClientContext = createContext<ContextType | undefined>(undefined)
122β
123export const FormEventClientProvider: ParentComponent = (props) => {
124 const value = useProviderValue()
125β
126 return (
127 <FormEventClientContext.Provider value={value}>
128 {props.children}
129 </FormEventClientContext.Provider>
130 )
131}
132β
133export function useFormEventClient() {
134 const context = useContext(FormEventClientContext)
135β
136 if (context === undefined) {
137 throw new Error(
138 `useFormEventClient must be used within a FormEventClientContext`,
139 )
140 }
141β
142 const memoContext = createMemo(() => context.store)
143 return { store: memoContext }
144}