Files
flyer-crawler.projectium.com/src/hooks/useDataExtraction.ts
Torben Sorensen 1480a73ab0
Some checks failed
Deploy to Test Environment / deploy-to-test (push) Failing after 58s
more compliance
2026-01-09 20:30:52 -08:00

62 lines
1.8 KiB
TypeScript

// src/hooks/useDataExtraction.ts
import { useCallback } from 'react';
import type { Flyer } from '../types';
type ExtractionType = 'store_name' | 'dates';
interface UseDataExtractionOptions {
selectedFlyer: Flyer | null;
onFlyerUpdate: (flyer: Flyer) => void;
}
interface UseDataExtractionReturn {
handleDataExtracted: (type: ExtractionType, value: string) => void;
}
/**
* A custom hook to handle data extraction from the correction tool.
* Updates the selected flyer with extracted store name or date information.
*
* Note: This currently only updates local state for immediate visual feedback.
* A production implementation should also persist changes to the database.
*
* @param options.selectedFlyer - The currently selected flyer
* @param options.onFlyerUpdate - Callback to update the flyer state
* @returns Object with handleDataExtracted callback
*
* @example
* ```tsx
* const { handleDataExtracted } = useDataExtraction({
* selectedFlyer,
* onFlyerUpdate: setSelectedFlyer,
* });
* ```
*/
export const useDataExtraction = ({
selectedFlyer,
onFlyerUpdate,
}: UseDataExtractionOptions): UseDataExtractionReturn => {
const handleDataExtracted = useCallback(
(type: ExtractionType, value: string) => {
if (!selectedFlyer) return;
// Create an updated copy of the flyer
const updatedFlyer = { ...selectedFlyer };
if (type === 'store_name') {
updatedFlyer.store = { ...updatedFlyer.store!, name: value };
} else if (type === 'dates') {
// A more robust solution would parse the date string properly.
// For now, this is a placeholder for future date extraction logic.
}
onFlyerUpdate(updatedFlyer);
},
[selectedFlyer, onFlyerUpdate],
);
return {
handleDataExtracted,
};
};