All files / src/components RemixModal.tsx

85.07% Statements 57/67
70.96% Branches 22/31
100% Functions 14/14
85.45% Lines 47/55

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                                            54x                                               54x                                                             10x 1x   9x       9x 9x       2x 2x 1x                     64x 64x 64x 64x 64x     64x 14x 13x 13x 13x 13x 13x 13x 13x 13x 12x             12x     13x     64x 10x 10x       64x 2x 2x     64x   64x 2x 2x 2x 2x 2x 2x 2x 2x         2x       64x   63x                                                    
import { useState, useEffect } from 'react'
import { X, Sparkles, Loader2 } from 'lucide-react'
import { toast } from 'sonner'
import { api, type RecipeDetail } from '../api/client'
import { cn, handleQuotaError } from '../lib/utils'
import SuggestionSelector from './SuggestionSelector'
import CustomRemixInput from './CustomRemixInput'
 
interface RemixModalProps {
  recipe: RecipeDetail
  profileId: number
  isOpen: boolean
  onClose: () => void
  onRemixCreated: (recipeId: number) => void
}
 
interface RemixHeaderProps {
  onClose: () => void
  disabled: boolean
}
 
function RemixHeader({ onClose, disabled }: RemixHeaderProps) {
  return (
    <div className="flex items-center justify-between border-b border-border px-6 py-4">
      <div className="flex items-center gap-2">
        <Sparkles className="h-5 w-5 text-primary" />
        <h2 className="text-lg font-semibold text-foreground">Remix This Recipe</h2>
      </div>
      <button
        onClick={onClose}
        disabled={disabled}
        className="rounded-full p-1 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
      >
        <X className="h-5 w-5" />
      </button>
    </div>
  )
}
 
interface RemixCreateButtonProps {
  creating: boolean
  enabled: boolean
  onClick: () => void
}
 
function RemixCreateButton({ creating, enabled, onClick }: RemixCreateButtonProps) {
  return (
    <button
      onClick={onClick}
      disabled={!enabled}
      className={cn(
        'flex w-full items-center justify-center gap-2 rounded-lg px-4 py-3 text-sm font-medium transition-colors',
        enabled
          ? 'bg-primary text-primary-foreground hover:bg-primary/90'
          : 'bg-muted text-muted-foreground cursor-not-allowed'
      )}
    >
      {creating ? (
        <>
          <Loader2 className="h-4 w-4 animate-spin" />
          Creating Remix...
        </>
      ) : (
        <>
          <Sparkles className="h-4 w-4" />
          Create Remix
        </>
      )}
    </button>
  )
}
 
function toggleSuggestion(
  prev: string[],
  suggestion: string,
  clearCustom: () => void
): string[] {
  if (prev.includes(suggestion)) {
    return prev.filter((s) => s !== suggestion)
  }
  Iif (prev.length >= 4) {
    toast.info('You can select up to 4 modifications')
    return prev
  }
  clearCustom()
  return [...prev, suggestion]
}
 
function getModification(customInput: string, selectedSuggestions: string[]): string | null {
  Iif (customInput.trim()) return customInput.trim()
  if (selectedSuggestions.length === 1) return selectedSuggestions[0]
  Eif (selectedSuggestions.length > 1) return selectedSuggestions.join(' AND ')
  return null
}
 
export default function RemixModal({
  recipe,
  profileId,
  isOpen,
  onClose,
  onRemixCreated,
}: RemixModalProps) {
  const [suggestions, setSuggestions] = useState<string[]>([])
  const [loadingSuggestions, setLoadingSuggestions] = useState(false)
  const [selectedSuggestions, setSelectedSuggestions] = useState<string[]>([])
  const [customInput, setCustomInput] = useState('')
  const [creating, setCreating] = useState(false)
 
  // Load suggestions when modal opens
  useEffect(() => {
    if (!isOpen) return
    let cancelled = false
    ;(async () => {
      setSuggestions([])
      setSelectedSuggestions([])
      setCustomInput('')
      setLoadingSuggestions(true)
      try {
        const response = await api.ai.remix.getSuggestions(recipe.id)
        Eif (!cancelled) setSuggestions(response.suggestions)
      } catch (error) {
        if (!cancelled) {
          console.error('Failed to load remix suggestions:', error)
          handleQuotaError(error, 'Failed to load suggestions')
        }
      } finally {
        Eif (!cancelled) setLoadingSuggestions(false)
      }
    })()
    return () => { cancelled = true }
  }, [isOpen, recipe.id])
 
  const handleSuggestionClick = (suggestion: string) => {
    setSelectedSuggestions((prev) =>
      toggleSuggestion(prev, suggestion, () => setCustomInput(''))
    )
  }
 
  const handleCustomInputChange = (value: string) => {
    setCustomInput(value)
    Eif (value.trim()) setSelectedSuggestions([])
  }
 
  const canSubmit = !creating && (selectedSuggestions.length > 0 || customInput.trim() !== '')
 
  const handleCreateRemix = async () => {
    const modification = getModification(customInput, selectedSuggestions)
    Iif (!modification) return
    setCreating(true)
    try {
      const remix = await api.ai.remix.create(recipe.id, modification, profileId)
      toast.success(`Created "${remix.title}"`)
      onRemixCreated(remix.id)
      onClose()
    } catch (error) {
      console.error('Failed to create remix:', error)
      handleQuotaError(error, 'Failed to create remix')
    } finally {
      setCreating(false)
    }
  }
 
  if (!isOpen) return null
 
  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4">
      <div className="relative w-full max-w-md rounded-xl bg-card shadow-2xl">
        <RemixHeader onClose={onClose} disabled={creating} />
        <div className="px-6 py-5">
          <p className="mb-4 text-sm text-muted-foreground">
            Choose one or more modifications, or describe your own remix of "{recipe.title}"
          </p>
          <SuggestionSelector
            suggestions={suggestions}
            selectedSuggestions={selectedSuggestions}
            loadingSuggestions={loadingSuggestions}
            disabled={creating}
            onSuggestionClick={handleSuggestionClick}
          />
          <CustomRemixInput
            value={customInput}
            onChange={handleCustomInputChange}
            disabled={creating}
          />
          <RemixCreateButton creating={creating} enabled={canSubmit} onClick={handleCreateRemix} />
        </div>
      </div>
    </div>
  )
}
 
← Back to Dashboard