11import { Dispatch , ReactNode , SetStateAction , useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
22import ErrorIndicator from '@/components/common/indicators/error-indicator' ;
33import LoadingPulse from '@/components/common/indicators/loading-pulse' ;
4+ import LoadingIndicator from '@/components/common/indicators/loading-indicator' ;
45
56
67type ValueRoot < T > = {
@@ -54,9 +55,12 @@ interface DataStateMethods<T> {
5455// either the `ValueState`'s value, or the `ErrorState`'s error.
5556interface RenderConfig {
5657 // Optional callback function for rendering a subfield of the `ValueState`'s value IFF that value is present:
58+ // (Can optionally use the className provided to the render function, see below)
5759 value ?: ( className ?: string ) => ReactNode ;
58- // Optional short error to display instead of full error :
60+ // Optional error message to display instead of 'Error' :
5961 error ?: string ;
62+ // Optional message to display while loading:
63+ loading ?: string ;
6064 // Optionally don't display fallback components like Loading- or ErrorIndicators:
6165 showFallback ?: boolean ;
6266 // Optionally display another component instead of the default LoadingIndicator:
@@ -162,7 +166,7 @@ export const DataState = {
162166 const useInit = ( ) => useEffect ( ( ) => { fetch ( ) } , [ fetch ] ) ;
163167
164168 const Render = ( conf : RenderConfig = { } ) : ReactNode => {
165- const { value, error, showFallback = true , loadingFallback, errorFallback, className } = conf ;
169+ const { value, error, loading , showFallback = true , loadingFallback, errorFallback, className } = conf ;
166170
167171 if ( dataRoot . value ) {
168172 return value ?
@@ -172,13 +176,13 @@ export const DataState = {
172176 }
173177 else if ( dataRoot . error ) {
174178 if ( showFallback ) {
175- return errorFallback ?
176- errorFallback : < ErrorIndicator error = { error } className = { className } /> ;
179+ return errorFallback ?? < ErrorIndicator error = { error } className = { className } /> ;
177180 }
178181 }
179182 else if ( showFallback ) {
180- return loadingFallback ?
181- loadingFallback : < LoadingPulse className = { className } /> ;
183+ if ( loadingFallback ) return loadingFallback ;
184+ if ( loading ) return < LoadingIndicator message = { loading } className = { className } /> ;
185+ return < LoadingPulse className = { className } /> ;
182186 }
183187 }
184188
0 commit comments