dc78d8fea3
Componentes React reutilizables: card, dialog, tabs, select, alert, badge, button, input, label, skeleton, tooltip, progress_bar, page_header, form_field, settings_page, crud_page, analytics_page, dashboard_layout. Charts: area, bar, line, sparkline, kpi_card, chart_container. Hooks Wails: use_wails_query, use_wails_mutation, use_wails_stream, use_wails_event, use_animated_canvas. Funciones core: cn, format_compact, chart_colors, get_series_color, wails_cache, theme_config_to_colors. Tipos: chart_series, wails_ipc, theme_config, component_variants. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
import { createContext, useContext, useMemo, type ReactNode } from 'react'
|
|
import { WailsCache, wailsCache } from '../core/wails_cache'
|
|
import type { QueryOptions, MutationOptions } from '../../types/ui/wails_ipc'
|
|
|
|
interface WailsContextValue {
|
|
cache: WailsCache
|
|
defaultQueryOptions: Partial<QueryOptions>
|
|
defaultMutationOptions: Partial<MutationOptions>
|
|
}
|
|
|
|
const WailsContext = createContext<WailsContextValue | null>(null)
|
|
|
|
export interface WailsProviderProps {
|
|
children: ReactNode
|
|
/** Usar un cache custom en lugar del singleton */
|
|
cache?: WailsCache
|
|
/** Opciones por defecto para queries */
|
|
defaultQueryOptions?: Partial<QueryOptions>
|
|
/** Opciones por defecto para mutations */
|
|
defaultMutationOptions?: Partial<MutationOptions>
|
|
}
|
|
|
|
export function WailsProvider({
|
|
children,
|
|
cache,
|
|
defaultQueryOptions = {},
|
|
defaultMutationOptions = {},
|
|
}: WailsProviderProps) {
|
|
const value = useMemo<WailsContextValue>(
|
|
() => ({
|
|
cache: cache ?? wailsCache,
|
|
defaultQueryOptions,
|
|
defaultMutationOptions,
|
|
}),
|
|
[cache, defaultQueryOptions, defaultMutationOptions]
|
|
)
|
|
|
|
return (
|
|
<WailsContext.Provider value={value}>
|
|
{children}
|
|
</WailsContext.Provider>
|
|
)
|
|
}
|
|
|
|
export function useWailsContext(): WailsContextValue {
|
|
const context = useContext(WailsContext)
|
|
if (!context) {
|
|
// Fallback to singleton cache if no provider
|
|
return {
|
|
cache: wailsCache,
|
|
defaultQueryOptions: {},
|
|
defaultMutationOptions: {},
|
|
}
|
|
}
|
|
return context
|
|
}
|
|
|
|
export function useWailsCache(): WailsCache {
|
|
return useWailsContext().cache
|
|
}
|