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 | 1x 18x 7x 7x 18x 18x 18x 6x 18x 18x 18x 18x 6x 6x 18x 45x 45x 45x 45x 45x 1x 33x 6x 32x 1x 1x 10x 10x 10x 10x 10x 10x 1x 4x | import {useEffect, useState} from 'react'
import {Outlet, useLocation, useNavigate} from 'react-router-dom'
import {useTranslation} from 'react-i18next'
import {ChevronRightIcon, PencilSquareIcon, XMarkIcon} from '@heroicons/react/24/outline'
import {Button} from '../components/Button'
import {RecipeCard} from '../components/RecipeCard'
import {TagSelector} from '../components/TagSelector'
import {tagsById} from '../recipeFormat'
import {localizeTagLabel} from '../locales/recipeTagLabels'
import {usePressPulse} from '../usePressPulse'
import {useApi} from '../useApi'
import {useRecipeGeneration} from '../recipeGeneration'
const VIEW_ORDER = {options: 0, results: 1, recipe: 2} as const
function viewName(pathname: string): keyof typeof VIEW_ORDER {
if (pathname.startsWith('/generate/results')) return 'results'
Iif (pathname.startsWith('/generate/recipe')) return 'recipe'
return 'options'
}
export function GenerateFlow() {
const apiFetch = useApi()
const {pathname} = useLocation()
// Confirm the session is still valid
useEffect(() => {
// on failure, this will automatically redirect to /login
apiFetch('/users/profile')
}, [apiFetch])
const view = viewName(pathname)
const [prevView, setPrevView] = useState(view)
const [slideDirectionBack, setSlideDirectionBack] = useState(false)
if (view !== prevView) {
setSlideDirectionBack(VIEW_ORDER[view] < VIEW_ORDER[prevView])
setPrevView(view)
}
return (
<div
key={view}
className={`flex flex-col gap-4 ${slideDirectionBack ? 'animate-slide-from-left' : 'animate-slide-from-right'}`}
>
<Outlet/>
</div>
)
}
export function GeneratePage() {
const {t} = useTranslation()
const navigate = useNavigate()
const {
prompt,
setPrompt,
selectedTags,
setSelectedTags,
recipes,
loading,
generate
} = useRecipeGeneration()
const [generateBtnRef, pulseGenerate] = usePressPulse<HTMLButtonElement>()
return (
<>
<div className="flex items-center justify-between gap-3">
<h2 className="text-lg font-bold">{t('generate.heading')}</h2>
{recipes.length > 0 && (
<button
type="button"
className="flex shrink-0 items-center gap-1 text-sm text-gray-500 dark:text-neutral-400 cursor-pointer transition-transform duration-100 hover:scale-98"
onClick={() => navigate('/generate/results')}
>
{t('generate.viewRecipes')}
<ChevronRightIcon className="h-4 w-4"/>
</button>
)}
</div>
<textarea
className="w-full min-h-32 border border-gray-300 dark:border-neutral-600 rounded p-3"
placeholder={t('generate.placeholder')}
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
onFocus={(e) => e.target.select()}
onKeyDown={(e) => {
Iif (e.key === 'Enter' && !e.shiftKey) {
// on Enter: directly submit instead of adding a new line
e.preventDefault()
if (!loading && (prompt.trim() !== '' || selectedTags.length > 0)) {
pulseGenerate()
generate()
}
}
}}
/>
<TagSelector selectedTags={selectedTags} onChange={setSelectedTags}/>
<div className="flex flex-col items-center gap-3">
{/* Clear selection button */}
<button
type="button"
className="flex items-center gap-1 text-sm text-gray-500 dark:text-neutral-400 cursor-pointer transition-transform duration-100 hover:scale-98 disabled:cursor-default disabled:opacity-40 disabled:hover:scale-100"
onClick={() => {
setPrompt('')
setSelectedTags([])
}}
disabled={prompt.trim() === '' && selectedTags.length === 0}
>
<XMarkIcon className="h-4 w-4"/>
{t('generate.clear')}
</button>
{/* Generate button */}
<Button
ref={generateBtnRef}
type="button"
onClick={generate}
disabled={loading || (prompt.trim() === '' && selectedTags.length === 0)}
>
{loading ? t('generate.generating') : t('generate.generate')}
</Button>
</div>
</>
)
}
export function GenerateResultsPage() {
const {t, i18n} = useTranslation()
const navigate = useNavigate()
const {recipes, status, setRecipes} = useRecipeGeneration()
// read from sessionStorage instead of context so unsaved edits are not shown the results header
const lastPrompt = sessionStorage.getItem('recipe_prompt') ?? ''
const lastTags = JSON.parse(sessionStorage.getItem('recipe_tags') ?? '[]') as string[]
function handleSavedIdChange(index: number, newId: number | undefined) {
setRecipes((prev) => prev.map((prevRecipe, prevIndex) => (prevIndex === index ? {
...prevRecipe,
id: newId
} : prevRecipe)))
}
return (
<>
<div className="flex items-start justify-between gap-3 rounded-lg border border-gray-200 dark:border-neutral-700 bg-gray-50 dark:bg-neutral-800 p-3">
<div className="flex min-w-0 flex-col gap-2">
{lastPrompt.trim() !== '' && <p className="text-sm text-gray-700 dark:text-neutral-200 line-clamp-2">{lastPrompt}</p>}
{lastTags.length > 0 && (
<div className="flex flex-wrap gap-1.5">
{lastTags.map((id) => (
<span key={id}
className="rounded-full border border-gray-200 dark:border-neutral-700 bg-white dark:bg-neutral-800 px-2 py-0.5 text-xs text-gray-600 dark:text-neutral-300">
{localizeTagLabel(id, tagsById.get(id)?.label ?? id, i18n.language)}
</span>
))}
</div>
)}
{lastPrompt.trim() === '' && lastTags.length === 0 && (
<p className="text-sm text-gray-400 dark:text-neutral-500">{t('generate.noOptions')}</p>
)}
</div>
<button
type="button"
className="flex shrink-0 items-center gap-1 self-start text-sm text-gray-500 dark:text-neutral-400 cursor-pointer transition-transform duration-100 hover:scale-98"
onClick={() => navigate('/generate')}
>
<PencilSquareIcon className="h-4 w-4"/>
{t('generate.edit')}
</button>
</div>
{status && <p className="text-gray-600 dark:text-neutral-300">{status}</p>}
{recipes.map((recipe, index) => (
<RecipeCard
key={index}
recipe={recipe}
recipeId={recipe.id}
onSavedIdChange={(newId) => handleSavedIdChange(index, newId)}
onOpen={() => navigate('/generate/recipe', {state: {index}})}
/>
))}
</>
)
}
|