- Reorganized utility modules from `src/utils` to `src/lib` for clearer separation of concerns. - Introduced new, dedicated type definitions in `src/types` for improved type safety in configuration and TTS API interactions. - Replaced `src/types/appConfig.ts` with `src/types/config.ts`. - Added `src/types/tts.ts` for TTS request payloads, error structures, and retry options. - Updated module imports across several contexts (`Config`, `EPUB`, `HTML`, `PDF`, `TTS`) and components to reflect the new `lib` and `types` locations. - Enhanced TTS API request and error handling in `src/app/api/tts/route.ts` and TTS-consuming contexts with explicit types. - Simplified `ProgressCard`, `ProgressPopup`, and `AudiobookExportModal` components by removing the `isProcessing` prop, centralizing processing state management. - Streamlined `HTMLContext` by removing `createFullAudioBook` and `isAudioCombining` properties, focusing its scope.
50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
'use client';
|
|
|
|
import { useCallback } from 'react';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { useLiveQuery } from 'dexie-react-hooks';
|
|
import { db } from '@/lib/dexie';
|
|
import type { PDFDocument } from '@/types/documents';
|
|
|
|
export function usePDFDocuments() {
|
|
const documents = useLiveQuery(
|
|
() => db['pdf-documents'].toArray(),
|
|
[],
|
|
undefined,
|
|
);
|
|
|
|
const isLoading = documents === undefined;
|
|
|
|
const addDocument = useCallback(async (file: File): Promise<string> => {
|
|
const id = uuidv4();
|
|
const arrayBuffer = await file.arrayBuffer();
|
|
|
|
const newDoc: PDFDocument = {
|
|
id,
|
|
type: 'pdf',
|
|
name: file.name,
|
|
size: file.size,
|
|
lastModified: file.lastModified,
|
|
data: arrayBuffer,
|
|
};
|
|
|
|
await db['pdf-documents'].add(newDoc);
|
|
return id;
|
|
}, []);
|
|
|
|
const removeDocument = useCallback(async (id: string): Promise<void> => {
|
|
await db['pdf-documents'].delete(id);
|
|
}, []);
|
|
|
|
const clearDocuments = useCallback(async (): Promise<void> => {
|
|
await db['pdf-documents'].clear();
|
|
}, []);
|
|
|
|
return {
|
|
documents: documents ?? [],
|
|
isLoading,
|
|
addDocument,
|
|
removeDocument,
|
|
clearDocuments,
|
|
};
|
|
}
|