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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | 1x 42x 42x 42x 42x 42x 23x 42x 23x 22x 22x 22x 81x 21x 60x 22x 42x 4x 4x 2x 2x 2x 42x 2x 1x 42x 2x 2x 2x 2x 2x 2x 42x 4x 4x 4x 4x 12x 4x 2x 2x 2x 4x 4x 4x 4x 42x 1x 41x 1x 79x 4x 114x 1x 1x 39x 1x 1x | // src/features/shopping/ShoppingList.tsx
import React, { useState, useMemo, useCallback } from 'react';
import type { ShoppingList, ShoppingListItem, User } from '../../types';
import { UserIcon } from '../../components/icons/UserIcon';
import { ListBulletIcon } from '../../components/icons/ListBulletIcon';
import { LoadingSpinner } from '../../components/LoadingSpinner';
import { TrashIcon } from '../../components/icons/TrashIcon';
import { SpeakerWaveIcon } from '../../components/icons/SpeakerWaveIcon';
import { generateSpeechFromText } from '../../services/aiApiClient';
import { decode, decodeAudioData } from '../../utils/audioUtils';
import { logger } from '../../services/logger.client';
interface ShoppingListComponentProps {
user: User | null;
lists: ShoppingList[];
activeListId: number | null;
onSelectList: (listId: number) => void;
onCreateList: (name: string) => Promise<void>;
onDeleteList: (listId: number) => Promise<void>;
onAddItem: (item: { customItemName: string }) => Promise<void>;
onUpdateItem: (itemId: number, updates: Partial<ShoppingListItem>) => Promise<void>;
onRemoveItem: (itemId: number) => Promise<void>;
}
export const ShoppingListComponent: React.FC<ShoppingListComponentProps> = ({
user,
lists,
activeListId,
onSelectList,
onCreateList,
onDeleteList,
onAddItem,
onUpdateItem,
onRemoveItem,
}) => {
const [isCreatingList, setIsCreatingList] = useState(false);
const [customItemName, setCustomItemName] = useState('');
const [isAddingCustom, setIsAddingCustom] = useState(false);
const [isReadingAloud, setIsReadingAloud] = useState(false);
const activeList = useMemo(
() => lists.find((list) => list.shopping_list_id === activeListId),
[lists, activeListId],
);
const { neededItems, purchasedItems } = useMemo(() => {
if (!activeList) return { neededItems: [], purchasedItems: [] };
const neededItems: ShoppingListItem[] = [];
const purchasedItems: ShoppingListItem[] = [];
activeList.items.forEach((item) => {
if (item.is_purchased) {
purchasedItems.push(item);
} else {
neededItems.push(item);
}
});
return { neededItems, purchasedItems };
}, [activeList]);
const handleCreateList = async () => {
const name = prompt('Enter a name for your new shopping list:');
if (name && name.trim()) {
setIsCreatingList(true);
await onCreateList(name.trim());
setIsCreatingList(false);
}
};
const handleDeleteList = async () => {
if (
activeList &&
window.confirm(
`Are you sure you want to delete the "${activeList.name}" list? This cannot be undone.`,
)
) {
await onDeleteList(activeList.shopping_list_id);
}
};
const handleAddCustomItem = async (e: React.FormEvent) => {
e.preventDefault();
Iif (!customItemName.trim()) return;
setIsAddingCustom(true);
await onAddItem({ customItemName: customItemName.trim() });
setCustomItemName('');
setIsAddingCustom(false);
};
const handleReadAloud = useCallback(async () => {
Iif (!activeList || neededItems.length === 0) return;
setIsReadingAloud(true);
try {
const listText =
'Here is your shopping list: ' +
neededItems
.map((item) => item.custom_item_name || item.master_item?.name)
.filter(Boolean)
.join(', ');
const response = await generateSpeechFromText(listText);
const base64Audio: string = await response.json();
// Play the audio
const audioContext = new window.AudioContext();
const audioBuffer = await decodeAudioData(decode(base64Audio), audioContext, 24000, 1);
const source = audioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(audioContext.destination);
source.start();
} catch (e) {
// This is a type-safe way to handle errors. We check if the caught
// object is an instance of Error before accessing its message property.
const errorMessage =
e instanceof Error ? e.message : 'An unknown error occurred while generating audio.';
logger.error('Failed to read list aloud', { error: e });
alert(`Could not read list aloud: ${errorMessage}`);
} finally {
setIsReadingAloud(false);
}
}, [activeList, neededItems]);
if (!user) {
return (
<div className="bg-white dark:bg-gray-900 rounded-lg border border-gray-200 dark:border-gray-700 p-4 text-center">
<div className="flex flex-col items-center justify-center h-full min-h-[150px]">
<UserIcon className="w-10 h-10 text-gray-400 mb-3" />
<h4 className="font-semibold text-gray-700 dark:text-gray-300">Your Shopping Lists</h4>
<p className="text-sm text-gray-500 dark:text-gray-400 mt-1">
Please log in to manage your shopping lists.
</p>
</div>
</div>
);
}
return (
<div className="bg-white dark:bg-gray-900 rounded-lg border border-gray-200 dark:border-gray-700 p-4">
<div className="flex items-center justify-between mb-3">
<h3 className="text-lg font-bold text-gray-800 dark:text-white flex items-center">
<ListBulletIcon className="w-6 h-6 mr-2 text-brand-primary" />
Shopping List
</h3>
<button
onClick={handleReadAloud}
disabled={isReadingAloud || !activeList || neededItems.length === 0}
className="p-1.5 rounded-md hover:bg-gray-100 dark:hover:bg-gray-700/50 text-gray-500 dark:text-gray-400 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
title="Read list aloud"
>
{isReadingAloud ? (
<div className="w-5 h-5">
<LoadingSpinner />
</div>
) : (
<SpeakerWaveIcon className="w-5 h-5" />
)}
</button>
</div>
<div className="space-y-3 mb-4">
{lists.length > 0 && (
<select
value={activeListId || ''}
onChange={(e) => onSelectList(Number(e.target.value))}
className="block w-full pl-3 pr-8 py-2 text-sm bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-brand-primary focus:border-brand-primary"
>
{lists.map((list) => (
<option key={list.shopping_list_id} value={list.shopping_list_id}>
{list.name}
</option>
))}
</select>
)}
<div className="flex space-x-2">
<button
onClick={handleCreateList}
disabled={isCreatingList}
className="flex-1 text-sm bg-gray-200 hover:bg-gray-300 dark:bg-gray-700 dark:hover:bg-gray-600 font-semibold py-2 px-3 rounded-md transition-colors"
>
New List
</button>
<button
onClick={handleDeleteList}
disabled={!activeList}
className="flex-1 text-sm bg-red-100 hover:bg-red-200 text-red-700 dark:bg-red-900/40 dark:hover:bg-red-900/60 dark:text-red-300 font-semibold py-2 px-3 rounded-md transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
>
Delete List
</button>
</div>
</div>
{activeList ? (
<>
<form onSubmit={handleAddCustomItem} className="flex space-x-2 mb-4">
<input
type="text"
value={customItemName}
onChange={(e) => setCustomItemName(e.target.value)}
placeholder="Add a custom item..."
className="grow block w-full px-3 py-2 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm sm:text-sm"
disabled={isAddingCustom}
/>
<button
type="submit"
disabled={isAddingCustom || !customItemName.trim()}
className="bg-brand-secondary hover:bg-brand-dark disabled:bg-gray-400 text-white font-bold py-2 px-3 rounded-lg flex items-center justify-center"
>
{isAddingCustom ? (
<div className="w-5 h-5">
<LoadingSpinner />
</div>
) : (
'Add'
)}
</button>
</form>
<div className="space-y-2 max-h-80 overflow-y-auto">
{neededItems.length > 0 ? (
neededItems.map((item) => (
<div
key={item.shopping_list_item_id}
className="group flex items-center space-x-2 text-sm"
>
<input
type="checkbox"
checked={item.is_purchased}
onChange={() =>
onUpdateItem(item.shopping_list_item_id, { is_purchased: !item.is_purchased })
}
className="h-4 w-4 rounded border-gray-300 text-brand-primary focus:ring-brand-secondary"
/>
<span className="grow text-gray-800 dark:text-gray-200">
{item.custom_item_name || item.master_item?.name}
</span>
<button
onClick={() => onRemoveItem(item.shopping_list_item_id)}
className="opacity-0 group-hover:opacity-100 text-red-500 hover:text-red-700 p-1"
>
<TrashIcon className="w-4 h-4" />
</button>
</div>
))
) : (
<p className="text-sm text-gray-500 text-center py-4">This list is empty.</p>
)}
{purchasedItems.length > 0 && (
<div className="pt-4 mt-4 border-t border-gray-200 dark:border-gray-700">
<h4 className="text-xs font-semibold text-gray-500 uppercase mb-2">Purchased</h4>
{purchasedItems.map((item) => (
<div
key={item.shopping_list_item_id}
className="group flex items-center space-x-2 text-sm"
>
<input
type="checkbox"
checked={item.is_purchased}
onChange={() =>
onUpdateItem(item.shopping_list_item_id, {
is_purchased: !item.is_purchased,
})
}
className="h-4 w-4 rounded border-gray-300 text-brand-primary focus:ring-brand-secondary"
/>
<span className="grow text-gray-500 dark:text-gray-400 line-through">
{item.custom_item_name || item.master_item?.name}
</span>
<button
onClick={() => onRemoveItem(item.shopping_list_item_id)}
className="opacity-0 group-hover:opacity-100 text-red-500 hover:text-red-700 p-1"
>
<TrashIcon className="w-4 h-4" />
</button>
</div>
))}
</div>
)}
</div>
</>
) : (
<div className="text-center py-10">
<p className="text-gray-500">No shopping lists found. Create one to get started!</p>
</div>
)}
</div>
);
};
|