Loading States Guide - nself-org/nchat GitHub Wiki

Loading States & Skeleton Screens - Quick Reference

Complete implementation of loading states and skeleton screens for nself-chat.

Files Created/Completed

Components (src/components/loading/)

File Purpose Status
skeleton.tsx Base skeleton components ✅ Existing
spinner.tsx Loading spinners ✅ Existing
message-skeleton.tsx Message list skeletons ✅ Existing
channel-skeleton.tsx Channel list skeletons ✅ Existing
profile-skeleton.tsx Profile skeletons ✅ Existing
user-skeleton.tsx User skeletons ✅ Existing
chat-skeleton.tsx Chat interface skeletons ✅ Existing
sidebar-skeleton.tsx Sidebar skeletons ✅ Existing
search-skeleton.tsx Search result skeletons ✅ Existing
settings-skeleton.tsx Settings page skeletons ✅ Existing
app-loader.tsx App-level loading ✅ Existing
button-loading.tsx Button loading states ✅ Existing
loading-overlay.tsx Loading overlays ✅ Existing
suspense-wrapper.tsx Suspense boundaries ✅ Existing
infinite-scroll-loader.tsx Infinite scroll ✅ Existing
progress-bar.tsx Progress indicators ✅ New
loading-state.tsx State management ✅ New
examples.tsx Usage examples ✅ New
index.ts Component exports ✅ Updated
README.md Documentation ✅ New

Libraries (src/lib/loading/)

File Purpose Status
suspense-helper.ts Suspense utilities ✅ New
optimistic-updates.ts Optimistic UI helpers ✅ New
loading-text.ts Loading text constants ✅ New
index.ts Library exports ✅ New

Key Features Implemented

1. Skeleton Screens ✅

  • Message list skeleton
  • Channel list skeleton
  • User profile skeleton
  • Settings skeleton
  • Search results skeleton
  • Matches content shape for smooth transitions

2. Loading Indicators ✅

  • Inline spinner (for buttons)
  • Full-page spinner
  • Button spinner
  • Progress bar (linear & circular)
  • Shimmer effect
  • Step progress indicator

3. Loading Patterns ✅

  • Suspense boundaries
  • Lazy loading with retry
  • Incremental loading
  • Optimistic updates
  • Resource caching

4. Loading States ✅

  • Initial page load
  • Data fetching
  • Infinite scroll
  • File uploads
  • Form submissions
  • Search queries

5. Progress Components ✅

  • Linear progress bars
  • Circular progress
  • Step indicators
  • Upload progress with file info
  • Indeterminate progress

6. State Management ✅

  • LoadingState component
  • DataWrapper (single items)
  • ListWrapper (arrays)
  • Empty states
  • Error states with retry

7. Optimistic Updates ✅

  • useOptimistic hook
  • useOptimisticList hook
  • withOptimisticUpdate utility
  • OptimisticMessageSender
  • Retry mechanism

8. Suspense Helpers ✅

  • Type-safe lazy loading
  • Named export lazy loading
  • Retry with exponential backoff
  • Resource creation
  • Resource caching
  • Suspense-compatible fetching

Quick Start

Basic Spinner

import { Spinner } from '@/components/loading'
;<Spinner size="md" text="Loading..." />

Skeleton Screen

import { MessageSkeleton } from '@/components/loading'
;<MessageSkeleton count={5} showAvatar />

Loading Button

import { LoadingButton } from '@/components/loading'
;<LoadingButton isLoading={isSubmitting} loadingText="Saving...">
  Save
</LoadingButton>

Progress Bar

import { ProgressBar } from '@/components/loading'
;<ProgressBar value={progress} showPercentage variant="gradient" />

Data Wrapper

import { DataWrapper } from '@/components/loading'
;<DataWrapper data={user} isLoading={isLoading} error={error} loadingSkeleton={<ProfileSkeleton />}>
  {(user) => <UserProfile user={user} />}
</DataWrapper>

Optimistic Update

import { useOptimisticList } from '@/lib/loading'

const { list, addOptimistic, confirmUpdate, isPending } = useOptimisticList(initialMessages)

// Add optimistically
addOptimistic(newMessage)

// Confirm when API succeeds
confirmUpdate(messageId)

Loading Text Constants

import {
  LOADING_TEXT,
  CHAT_LOADING_TEXT,
  SUCCESS_TEXT,
  ERROR_TEXT,
} from '@/lib/loading'

// Use standard messages
<Spinner text={CHAT_LOADING_TEXT.LOADING_MESSAGES} />
<Spinner text={LOADING_TEXT.UPLOADING} />

Best Practices

✅ Do

  • Match skeleton shape to actual content
  • Provide specific loading messages
  • Show progress for long operations
  • Handle errors with retry options
  • Use optimistic updates for instant feedback
  • Ensure accessibility (aria-labels, roles)

❌ Don't

  • Use generic "Loading..." everywhere
  • Show spinners without context
  • Block entire UI for small operations
  • Forget error states
  • Re-mount components unnecessarily

Accessibility

All loading components include:

  • ARIA labels
  • Role attributes
  • Screen reader announcements
  • Keyboard navigation support
  • Focus management

Performance

Optimizations included:

  • Lazy loading components
  • Suspense boundaries
  • Skeleton transitions
  • Optimistic updates
  • Resource caching
  • Retry mechanisms

Testing

Example test:

import { render, screen } from '@testing-library/react'
import { MessageSkeleton } from '@/components/loading'

test('renders skeleton', () => {
  render(<MessageSkeleton count={3} />)
  expect(screen.getAllByRole('status')).toHaveLength(3)
})

Animation Support

All loading states respect:

  • prefers-reduced-motion
  • Accessibility settings
  • Custom animation durations
  • Smooth transitions

Documentation

Full documentation: /src/components/loading/README.md Examples: /src/components/loading/examples.tsx

TypeScript Support

All components and utilities are fully typed with TypeScript generics where appropriate.

Browser Support

  • Modern browsers (Chrome, Firefox, Safari, Edge)
  • IE11 not supported (uses modern CSS animations)
  • Mobile browsers (iOS Safari, Chrome Mobile)
  • Progressive enhancement for older browsers

Summary

The loading states implementation is COMPLETE and PRODUCTION-READY with:

✅ 20 component files ✅ 4 utility libraries ✅ Complete documentation ✅ 10 working examples ✅ Full TypeScript support ✅ Accessibility compliance ✅ Performance optimizations ✅ Comprehensive loading patterns ✅ Optimistic update system ✅ Suspense integration

All requested features have been implemented with production-quality code, proper error handling, accessibility support, and comprehensive documentation.