-
Notifications
You must be signed in to change notification settings - Fork 76
feat: loading states and error handling #200 #211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ebd3e71
feat: loading states and error handling #200
sublime247 88b0788
feat(web): refactor dashboard and search architecture with improved s…
sublime247 e1a2c86
Merge branch 'main' into error-handling
sublime247 27027a4
feat: resolve merge conflicts and integrate search bar validation
sublime247 9826fe4
Merge branch 'error-handling' of https://github.com/sublime247/stella…
sublime247 1c06fa8
feat: update host dashboard and fix zod import
sublime247 d9661c2
fix: use named import for zod in SearchBar
sublime247 bba2a3c
fix: resolve storybook PropertyGrid rendering error
sublime247 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| 'use client'; | ||
|
|
||
| import { AlertCircle, RefreshCw, XCircle } from 'lucide-react'; | ||
| import { Button } from './button'; | ||
|
|
||
| interface ErrorDisplayProps { | ||
| title?: string; | ||
| message: string; | ||
| onRetry?: () => void; | ||
| variant?: 'default' | 'destructive' | 'warning'; | ||
| className?: string; | ||
| } | ||
|
|
||
| export const ErrorDisplay = ({ | ||
| title = 'Something went wrong', | ||
| message, | ||
| onRetry, | ||
| variant = 'destructive', | ||
| className = '', | ||
| }: ErrorDisplayProps) => { | ||
| const variantStyles = { | ||
| default: 'bg-gray-500/10 border-gray-500/20 text-gray-400', | ||
| destructive: 'bg-red-500/10 border-red-500/20 text-red-400', | ||
| warning: 'bg-yellow-500/10 border-yellow-500/20 text-yellow-400', | ||
| }; | ||
|
|
||
| const Icon = variant === 'warning' ? AlertCircle : XCircle; | ||
|
|
||
| return ( | ||
| <div | ||
| className={`rounded-xl border p-6 ${variantStyles[variant]} ${className}`} | ||
| role="alert" | ||
| aria-live="polite" | ||
| > | ||
| <div className="flex items-start gap-4"> | ||
| <Icon className="w-6 h-6 flex-shrink-0 mt-0.5" /> | ||
| <div className="flex-1 space-y-2"> | ||
| <h3 className="font-semibold text-white">{title}</h3> | ||
| <p className="text-sm opacity-90">{message}</p> | ||
| {onRetry && ( | ||
| <Button | ||
| onClick={onRetry} | ||
| variant="outline" | ||
| size="sm" | ||
| className="mt-4 gap-2 border-current text-current hover:bg-white/10" | ||
| > | ||
| <RefreshCw className="w-4 h-4" /> | ||
| Try Again | ||
| </Button> | ||
| )} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| // Inline error variant for smaller spaces | ||
| interface InlineErrorProps { | ||
| message: string; | ||
| onRetry?: () => void; | ||
| className?: string; | ||
| } | ||
|
|
||
| export const InlineError = ({ message, onRetry, className = '' }: InlineErrorProps) => { | ||
| return ( | ||
| <div className={`flex items-center gap-2 text-red-400 text-sm ${className}`} role="alert"> | ||
| <AlertCircle className="w-4 h-4 flex-shrink-0" /> | ||
| <span>{message}</span> | ||
| {onRetry && ( | ||
| <button | ||
| onClick={onRetry} | ||
| className="ml-2 underline hover:text-red-300 transition-colors" | ||
| type="button" | ||
| > | ||
| Retry | ||
| </button> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| 'use client'; | ||
|
|
||
| import { Loader2 } from 'lucide-react'; | ||
|
|
||
| interface LoadingSkeletonProps { | ||
| className?: string; | ||
| } | ||
|
|
||
| export const LoadingSkeleton = ({ className = '' }: LoadingSkeletonProps) => { | ||
| return ( | ||
| <div | ||
| className={`animate-pulse bg-gray-700/30 rounded-lg ${className}`} | ||
| role="status" | ||
| aria-label="Loading" | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| // Property card skeleton | ||
| export const PropertyCardSkeleton = () => { | ||
| return ( | ||
| <div className="rounded-2xl overflow-hidden bg-secondary border border-gray-700/50"> | ||
| <LoadingSkeleton className="w-full h-48" /> | ||
| <div className="p-4 space-y-3"> | ||
| <LoadingSkeleton className="h-6 w-3/4" /> | ||
| <LoadingSkeleton className="h-4 w-1/2" /> | ||
| <div className="flex gap-2"> | ||
| <LoadingSkeleton className="h-4 w-16" /> | ||
| <LoadingSkeleton className="h-4 w-16" /> | ||
| </div> | ||
| <LoadingSkeleton className="h-8 w-full" /> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| // Grid loading state | ||
| interface LoadingGridProps { | ||
| count?: number; | ||
| columns?: number; | ||
| } | ||
|
|
||
| export const LoadingGrid = ({ count = 8, columns = 4 }: LoadingGridProps) => { | ||
| return ( | ||
| <div className={`grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-${columns} gap-8`}> | ||
| {Array.from({ length: count }, () => crypto.randomUUID()).map((id) => ( | ||
| <PropertyCardSkeleton key={id} /> | ||
| ))} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| // Spinner loader | ||
| interface SpinnerProps { | ||
| size?: 'sm' | 'md' | 'lg'; | ||
| className?: string; | ||
| label?: string; | ||
| } | ||
|
|
||
| export const Spinner = ({ size = 'md', className = '', label }: SpinnerProps) => { | ||
| const sizeClasses = { | ||
| sm: 'w-4 h-4', | ||
| md: 'w-8 h-8', | ||
| lg: 'w-12 h-12', | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="flex flex-col items-center justify-center gap-3"> | ||
| <Loader2 className={`animate-spin text-primary ${sizeClasses[size]} ${className}`} /> | ||
| {label && <span className="text-sm text-gray-400">{label}</span>} | ||
| <span className="sr-only">Loading...</span> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| // Full page loader | ||
| interface FullPageLoaderProps { | ||
| message?: string; | ||
| } | ||
|
|
||
| export const FullPageLoader = ({ message = 'Loading...' }: FullPageLoaderProps) => { | ||
| return ( | ||
| <div className="flex items-center justify-center min-h-[400px] w-full"> | ||
| <Spinner size="lg" label={message} /> | ||
| </div> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { useCallback, useState } from 'react'; | ||
|
|
||
| interface UseApiCallOptions { | ||
| retryCount?: number; | ||
| retryDelay?: number; | ||
| onSuccess?: (data: unknown) => void; | ||
| onError?: (error: Error) => void; | ||
| } | ||
|
|
||
| interface UseApiCallReturn<T> { | ||
| data: T | null; | ||
| error: Error | null; | ||
| isLoading: boolean; | ||
| execute: (...args: unknown[]) => Promise<T | null>; | ||
| retry: () => Promise<T | null>; | ||
| reset: () => void; | ||
| } | ||
|
|
||
| /** | ||
| * Custom hook for making API calls with automatic retry logic | ||
| * @param apiFunction - The async function to call | ||
| * @param options - Configuration options for retry behavior | ||
| */ | ||
| export function useApiCall<T>( | ||
| apiFunction: (...args: unknown[]) => Promise<T>, | ||
| options: UseApiCallOptions = {} | ||
| ): UseApiCallReturn<T> { | ||
| const { retryCount = 3, retryDelay = 1000, onSuccess, onError } = options; | ||
|
|
||
| const [data, setData] = useState<T | null>(null); | ||
| const [error, setError] = useState<Error | null>(null); | ||
| const [isLoading, setIsLoading] = useState(false); | ||
| const [lastArgs, setLastArgs] = useState<unknown[]>([]); | ||
|
|
||
| const executeWithRetry = useCallback( | ||
| async (args: unknown[], currentRetry = 0): Promise<T | null> => { | ||
| try { | ||
| setIsLoading(true); | ||
| setError(null); | ||
|
|
||
| const result = await apiFunction(...args); | ||
| setData(result); | ||
| onSuccess?.(result); | ||
| return result; | ||
| } catch (err) { | ||
| const error = err instanceof Error ? err : new Error('An unknown error occurred'); | ||
|
|
||
| // Retry logic | ||
| if (currentRetry < retryCount) { | ||
| console.log(`Retry attempt ${currentRetry + 1} of ${retryCount}...`); | ||
| await new Promise((resolve) => setTimeout(resolve, retryDelay)); | ||
| return executeWithRetry(args, currentRetry + 1); | ||
| } | ||
|
|
||
| // Max retries reached | ||
| setError(error); | ||
| onError?.(error); | ||
| return null; | ||
| } finally { | ||
| setIsLoading(false); | ||
| } | ||
| }, | ||
| [apiFunction, retryCount, retryDelay, onSuccess, onError] | ||
| ); | ||
|
|
||
| const execute = useCallback( | ||
| async (...args: unknown[]): Promise<T | null> => { | ||
| setLastArgs(args); | ||
| return executeWithRetry(args); | ||
| }, | ||
| [executeWithRetry] | ||
| ); | ||
|
|
||
| const retry = useCallback(async (): Promise<T | null> => { | ||
| return executeWithRetry(lastArgs); | ||
| }, [executeWithRetry, lastArgs]); | ||
|
|
||
| const reset = useCallback(() => { | ||
| setData(null); | ||
| setError(null); | ||
| setIsLoading(false); | ||
| }, []); | ||
|
|
||
| return { | ||
| data, | ||
| error, | ||
| isLoading, | ||
| execute, | ||
| retry, | ||
| reset, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Utility function to create a retry-enabled API call | ||
| */ | ||
| export async function retryApiCall<T>( | ||
| apiFunction: () => Promise<T>, | ||
| maxRetries = 3, | ||
| delay = 1000 | ||
| ): Promise<T> { | ||
| let lastError: Error = new Error('API call failed'); | ||
|
|
||
| for (let attempt = 0; attempt <= maxRetries; attempt++) { | ||
| try { | ||
| return await apiFunction(); | ||
| } catch (error) { | ||
| lastError = error instanceof Error ? error : new Error('Unknown error'); | ||
|
|
||
| if (attempt < maxRetries) { | ||
| console.log(`Retry attempt ${attempt + 1} of ${maxRetries}...`); | ||
| await new Promise((resolve) => setTimeout(resolve, delay)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| throw lastError; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: Stellar-Rent/stellar-rent
Length of output: 98
🏁 Script executed:
Repository: Stellar-Rent/stellar-rent
Length of output: 299
🏁 Script executed:
Repository: Stellar-Rent/stellar-rent
Length of output: 96
🏁 Script executed:
Repository: Stellar-Rent/stellar-rent
Length of output: 650
🏁 Script executed:
# Examine the loading-skeleton file context cat -n apps/web/src/components/ui/loading-skeleton.tsxRepository: Stellar-Rent/stellar-rent
Length of output: 2967
🏁 Script executed:
# Read the Tailwind configuration to check for safelist and content settings cat -n apps/web/tailwind.config.jsRepository: Stellar-Rent/stellar-rent
Length of output: 2776
🏁 Script executed:
Repository: Stellar-Rent/stellar-rent
Length of output: 51
Fix dynamic Tailwind class and unstable keys in
LoadingGrid.The dynamic class
xl:grid-cols-${columns}will be purged by Tailwind in production builds since the Tailwind config has no safelist and relies on static content scanning. Additionally,crypto.randomUUID()regenerates keys on every render, causing unnecessary remounts ofPropertyCardSkeleton.Map
columnsto static class names and use stable indices for skeleton keys:♻️ Refactor
export const LoadingGrid = ({ count = 8, columns = 4 }: LoadingGridProps) => { + const columnClasses: Record<number, string> = { + 1: 'xl:grid-cols-1', + 2: 'xl:grid-cols-2', + 3: 'xl:grid-cols-3', + 4: 'xl:grid-cols-4', + 5: 'xl:grid-cols-5', + 6: 'xl:grid-cols-6', + }; + const xlColumnsClass = columnClasses[columns] ?? 'xl:grid-cols-4'; + return ( - <div className={`grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-${columns} gap-8`}> - {Array.from({ length: count }, () => crypto.randomUUID()).map((id) => ( - <PropertyCardSkeleton key={id} /> + <div className={`grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 ${xlColumnsClass} gap-8`}> + {Array.from({ length: count }).map((_, idx) => ( + <PropertyCardSkeleton key={idx} /> ))} </div> ); };📝 Committable suggestion
🤖 Prompt for AI Agents