Fix token handling, modal behavior, and pagination

Multiple fixes and improvements:

- src/app/api/user/hardcover-shelves/[id]/route.ts: Make token testing more robust by using the existing shelf.apiToken when no new token is provided, attempt decryption only when needed, and gracefully fall back on decryption errors.
- src/components/ui/AddShelfModal.tsx: Simplify token handling by passing the trimmed token directly to addHardcover (remove client-side 'Bearer ' stripping).
- src/components/ui/ManageShelfModal.tsx: Stabilize form reset effect by depending on shelf?.id to avoid unnecessary re-renders when the shelf object changes identity.
- src/components/ui/Modal.tsx: Simplify modal rendering by removing the mounted state and createPortal usage, cleaning up imports and rendering directly.
- src/lib/services/hardcover-api.service.ts: Add a logger, introduce a MAX_PAGES cap and page counters to prevent unbounded pagination loops, and log/break when the API returns errors during pagination.

These changes improve reliability (token handling and pagination safety), reduce unnecessary renders, and simplify modal lifecycle.
This commit is contained in:
kikootwo
2026-03-04 10:55:37 -05:00
parent 7f706e806f
commit c29cfa3a07
5 changed files with 31 additions and 30 deletions
@@ -106,13 +106,16 @@ export async function PATCH(
// Validate token/listId by fetching the list before saving
if (cleanedToken || newListId) {
const encryptionService = getEncryptionService();
const tokenToTest = cleanedToken || (() => {
let tokenToTest = cleanedToken || shelf.apiToken;
if (!cleanedToken) {
try {
return encryptionService.isEncryptedFormat(shelf.apiToken)
? encryptionService.decrypt(shelf.apiToken)
: shelf.apiToken;
} catch { return shelf.apiToken; }
})();
if (encryptionService.isEncryptedFormat(shelf.apiToken)) {
tokenToTest = encryptionService.decrypt(shelf.apiToken);
}
} catch {
// Decryption failed, fall back to raw token
}
}
const listIdToTest = newListId || shelf.listId;
try {