All files / web/src/components/practice OfflineWorkSection.parsingLimit.stories.tsx

0% Statements 0/221
0% Branches 0/1
0% Functions 0/1
0% Lines 0/221

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222                                                                                                                                                                                                                                                                                                                                                                                                                                                           
/**
 * OfflineWorkSection — Parsing Limit Stories
 *
 * Demonstrates the PARSING_LIMIT_REACHED upgrade prompt vs. normal retry button
 * on failed worksheet photo tiles.
 */

import type { Meta, StoryObj } from '@storybook/react'
import { useRef } from 'react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { VisualDebugProvider } from '@/contexts/VisualDebugContext'
import {
  MockWorksheetParsingProvider,
  mockParsingStates,
} from '@/contexts/WorksheetParsingContext.mock'
import { css } from '../../../styled-system/css'
import { OfflineWorkSection, type OfflineAttachment } from './OfflineWorkSection'

function createQueryClient() {
  return new QueryClient({
    defaultOptions: { queries: { retry: false, staleTime: Infinity } },
  })
}

const WORKSHEET_URL = '/storybook-assets/worksheets/worksheet-10-problems.jpg'

// =============================================================================
// Shared attachments
// =============================================================================

const failedAttachment: OfflineAttachment = {
  id: 'att-failed',
  url: WORKSHEET_URL,
  filename: 'worksheet.jpg',
  parsingStatus: 'failed',
  rawParsingResult: null,
  needsReview: false,
  sessionCreated: false,
}

const unparsedAttachment: OfflineAttachment = {
  id: 'att-unparsed',
  url: WORKSHEET_URL,
  filename: 'worksheet-2.jpg',
  parsingStatus: null,
  rawParsingResult: null,
  needsReview: false,
  sessionCreated: false,
}

// =============================================================================
// Wrapper with all required providers + no-op handlers
// =============================================================================

function StoryShell({ children, label }: { children: React.ReactNode; label: string }) {
  return (
    <div className={css({ maxWidth: '500px', margin: '0 auto' })}>
      <div
        className={css({
          padding: '0.5rem 1rem',
          backgroundColor: 'gray.100',
          fontSize: '0.75rem',
          color: 'gray.500',
          textAlign: 'center',
          borderBottom: '1px solid',
          borderColor: 'gray.200',
        })}
      >
        {label}
      </div>
      {children}
    </div>
  )
}

function OfflineWorkSectionDemo({
  attachments,
  parsingState,
  label,
  isDark = false,
}: {
  attachments: OfflineAttachment[]
  parsingState?: Parameters<typeof MockWorksheetParsingProvider>[0]['state']
  label: string
  isDark?: boolean
}) {
  const fileInputRef = useRef<HTMLInputElement>(null)

  return (
    <QueryClientProvider client={createQueryClient()}>
      <VisualDebugProvider>
        <MockWorksheetParsingProvider state={parsingState}>
          <StoryShell label={label}>
            <OfflineWorkSection
              attachments={attachments}
              fileInputRef={fileInputRef as React.RefObject<HTMLInputElement>}
              isUploading={false}
              uploadError={null}
              deletingId={null}
              parsingId={null}
              dragOver={false}
              isDark={isDark}
              canUpload={true}
              onFileSelect={() => {}}
              onDrop={() => {}}
              onDragOver={() => {}}
              onDragLeave={() => {}}
              onOpenCamera={() => {}}
              onOpenViewer={() => {}}
              onDeletePhoto={() => {}}
            />
          </StoryShell>
        </MockWorksheetParsingProvider>
      </VisualDebugProvider>
    </QueryClientProvider>
  )
}

// =============================================================================
// Meta
// =============================================================================

const meta: Meta<typeof OfflineWorkSection> = {
  title: 'Practice/OfflineWorkSection/Parsing Limit',
  component: OfflineWorkSection,
  parameters: {
    layout: 'padded',
  },
  tags: ['autodocs'],
}

export default meta
type Story = StoryObj<typeof OfflineWorkSection>

// =============================================================================
// Stories
// =============================================================================

export const LimitReached: Story = {
  name: 'Upgrade Plan (PARSING_LIMIT_REACHED)',
  render: () => (
    <OfflineWorkSectionDemo
      attachments={[failedAttachment]}
      parsingState={mockParsingStates.error(
        'att-failed',
        'Monthly parsing limit reached. Upgrade your plan for more parses.',
        'PARSING_LIMIT_REACHED'
      )}
      label="Failed with PARSING_LIMIT_REACHED — shows amber 'Upgrade Plan' link"
    />
  ),
}

export const GenericFailure: Story = {
  name: 'Retry Button (Generic Error)',
  render: () => (
    <OfflineWorkSectionDemo
      attachments={[failedAttachment]}
      parsingState={mockParsingStates.error(
        'att-failed',
        'Server error: could not parse worksheet'
      )}
      label="Failed with generic error — shows orange 'Retry' button"
    />
  ),
}

export const SideBySide: Story = {
  name: 'Side-by-Side Comparison',
  render: () => (
    <div className={css({ display: 'flex', gap: '2rem', flexWrap: 'wrap' })}>
      <OfflineWorkSectionDemo
        attachments={[failedAttachment]}
        parsingState={mockParsingStates.error(
          'att-failed',
          'Monthly parsing limit reached. Upgrade your plan for more parses.',
          'PARSING_LIMIT_REACHED'
        )}
        label="Limit reached → Upgrade Plan"
      />
      <OfflineWorkSectionDemo
        attachments={[{ ...failedAttachment, id: 'att-generic' }]}
        parsingState={mockParsingStates.error('att-generic', 'Server error')}
        label="Generic error → Retry"
      />
    </div>
  ),
}

export const MixedTiles: Story = {
  name: 'Mixed: Unparsed + Limit Reached',
  render: () => (
    <OfflineWorkSectionDemo
      attachments={[unparsedAttachment, { ...failedAttachment, id: 'att-limit' }]}
      parsingState={mockParsingStates.error(
        'att-limit',
        'Monthly parsing limit reached',
        'PARSING_LIMIT_REACHED'
      )}
      label="First tile: unparsed (Parse button), Second tile: limit reached (Upgrade Plan)"
    />
  ),
}

export const DarkMode: Story = {
  name: 'Dark Mode — Limit Reached',
  render: () => (
    <div className={css({ backgroundColor: 'gray.900', padding: '1rem', borderRadius: '8px' })}>
      <OfflineWorkSectionDemo
        attachments={[failedAttachment]}
        parsingState={mockParsingStates.error(
          'att-failed',
          'Monthly parsing limit reached',
          'PARSING_LIMIT_REACHED'
        )}
        label="Dark mode variant"
        isDark
      />
    </div>
  ),
}