asasvirtuais 2.4.1 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "asasvirtuais",
3
3
  "type": "module",
4
- "version": "2.4.1",
4
+ "version": "2.5.0",
5
5
  "description": "React form and action management utilities",
6
6
  "directories": {
7
7
  "packages": "./packages"
@@ -289,15 +289,14 @@ export function useSingleProvider<TSchema extends TableSchema>({
289
289
  }, [index[id]])
290
290
  return {
291
291
  id,
292
+ table,
292
293
  single,
293
294
  setSingle,
294
295
  loading: find.loading,
295
296
  }
296
297
  }
297
298
 
298
- export const SingleContext = createContext<
299
- ReturnType<typeof useSingleProvider> | undefined
300
- >(undefined)
299
+ const SingleRegistryContext = createContext<Record<string, ReturnType<typeof useSingleProvider<any>>> | undefined>(undefined)
301
300
 
302
301
  export function SingleProvider<TSchema extends TableSchema>({
303
302
  children,
@@ -307,22 +306,29 @@ export function SingleProvider<TSchema extends TableSchema>({
307
306
  table: string
308
307
  schema: TSchema
309
308
  children: React.ReactNode | ((props: ReturnType<typeof useSingleProvider<TSchema>>) => React.ReactNode)
309
+ nullIfNotFound?: boolean
310
310
  }) {
311
311
  const value = useSingleProvider<TSchema>(props)
312
- if (!value.single) return null
312
+ const registry = useContext(SingleRegistryContext) ?? {}
313
+
314
+ const newRegistry = useMemo(() => {
315
+ return { ...registry, [props.table]: value }
316
+ }, [registry, props.table, value])
317
+
318
+ if (props.nullIfNotFound && !value.single) return null
313
319
  return (
314
- // @ts-expect-error
315
- <SingleContext.Provider value={value}>
320
+ <SingleRegistryContext.Provider value={newRegistry}>
316
321
  {typeof children === 'function' ? (
317
322
  children(value)
318
323
  ) : (
319
324
  children
320
325
  )}
321
- </SingleContext.Provider>
326
+ </SingleRegistryContext.Provider>
322
327
  )
323
328
  }
324
329
 
325
- export function useSingle<TSchema extends TableSchema>() {
326
- // @ts-expect-error
327
- return useContext(SingleContext) as ReturnType<typeof useSingleProvider<TSchema>>
330
+ export function useSingle<TSchema extends TableSchema>(table: string, _schema: TSchema) {
331
+ const registry = useContext(SingleRegistryContext)
332
+ if (!registry || !registry[table]) throw new Error(`useSingle("${table}") must be used within a SingleProvider for that table.`)
333
+ return registry[table] as ReturnType<typeof useSingleProvider<TSchema>>
328
334
  }