refactor: enhance inbox functionality with type guards and API service integration

- Introduced type guards for metadata in InboxSidebar to ensure safe access and improve type safety.
- Refactored the useInbox hook to utilize the new notifications API service for fetching notifications, enhancing validation and error handling.
- Added new API request/response schemas for notifications, improving structure and clarity.
- Updated logic for loading and marking notifications as read, ensuring consistent state management and user experience.
This commit is contained in:
Anish Sarkar 2026-01-22 18:32:25 +05:30
parent c98cfac49f
commit 00596f991d
4 changed files with 294 additions and 99 deletions

View file

@ -0,0 +1,94 @@
import {
type GetNotificationsRequest,
type GetNotificationsResponse,
type MarkAllNotificationsReadResponse,
type MarkNotificationReadRequest,
type MarkNotificationReadResponse,
getNotificationsRequest,
getNotificationsResponse,
markAllNotificationsReadResponse,
markNotificationReadRequest,
markNotificationReadResponse,
} from "@/contracts/types/inbox.types";
import { ValidationError } from "../error";
import { baseApiService } from "./base-api.service";
class NotificationsApiService {
/**
* Get notifications with pagination
*/
getNotifications = async (
request: GetNotificationsRequest
): Promise<GetNotificationsResponse> => {
const parsedRequest = getNotificationsRequest.safeParse(request);
if (!parsedRequest.success) {
console.error("Invalid request:", parsedRequest.error);
const errorMessage = parsedRequest.error.issues.map((issue) => issue.message).join(", ");
throw new ValidationError(`Invalid request: ${errorMessage}`);
}
const { queryParams } = parsedRequest.data;
// Build query string from params
const params = new URLSearchParams();
if (queryParams.search_space_id !== undefined) {
params.append("search_space_id", String(queryParams.search_space_id));
}
if (queryParams.type) {
params.append("type", queryParams.type);
}
if (queryParams.before_date) {
params.append("before_date", queryParams.before_date);
}
if (queryParams.limit !== undefined) {
params.append("limit", String(queryParams.limit));
}
if (queryParams.offset !== undefined) {
params.append("offset", String(queryParams.offset));
}
const queryString = params.toString();
return baseApiService.get(
`/api/v1/notifications${queryString ? `?${queryString}` : ""}`,
getNotificationsResponse
);
};
/**
* Mark a single notification as read
*/
markAsRead = async (
request: MarkNotificationReadRequest
): Promise<MarkNotificationReadResponse> => {
const parsedRequest = markNotificationReadRequest.safeParse(request);
if (!parsedRequest.success) {
console.error("Invalid request:", parsedRequest.error);
const errorMessage = parsedRequest.error.issues.map((issue) => issue.message).join(", ");
throw new ValidationError(`Invalid request: ${errorMessage}`);
}
const { notificationId } = parsedRequest.data;
return baseApiService.patch(
`/api/v1/notifications/${notificationId}/read`,
markNotificationReadResponse
);
};
/**
* Mark all notifications as read
*/
markAllAsRead = async (): Promise<MarkAllNotificationsReadResponse> => {
return baseApiService.patch(
"/api/v1/notifications/read-all",
markAllNotificationsReadResponse
);
};
}
export const notificationsApiService = new NotificationsApiService();