78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
|
|
/**
|
||
|
|
* Notification hooks: list, unread count, mark read, delete, types, preferences.
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||
|
|
import { apiGet, apiPatch, apiDelete } from './client';
|
||
|
|
import { PaginatedResponse } from './types';
|
||
|
|
|
||
|
|
export interface NotificationTypeItem {
|
||
|
|
type_key: string;
|
||
|
|
plugin_name: string;
|
||
|
|
category: string;
|
||
|
|
label: string;
|
||
|
|
description: string | null;
|
||
|
|
is_enabled_by_default: boolean;
|
||
|
|
is_enabled: boolean;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useNotifications() {
|
||
|
|
return useQuery({
|
||
|
|
queryKey: ['notifications'],
|
||
|
|
queryFn: () => apiGet<PaginatedResponse<any>>('/notifications'),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useUnreadNotificationCount() {
|
||
|
|
return useQuery({
|
||
|
|
queryKey: ['notifications', 'unread-count'],
|
||
|
|
queryFn: () => apiGet<number>('/notifications/unread-count'),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useMarkNotificationRead() {
|
||
|
|
const queryClient = useQueryClient();
|
||
|
|
return useMutation({
|
||
|
|
mutationFn: (id: string) => apiPatch(`/notifications/${id}/read`),
|
||
|
|
onSuccess: () => {
|
||
|
|
queryClient.invalidateQueries({ queryKey: ['notifications'] });
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useDeleteNotification() {
|
||
|
|
const queryClient = useQueryClient();
|
||
|
|
return useMutation({
|
||
|
|
mutationFn: (id: string) => apiDelete(`/notifications/${id}`),
|
||
|
|
onSuccess: () => {
|
||
|
|
queryClient.invalidateQueries({ queryKey: ['notifications'] });
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useNotificationTypes() {
|
||
|
|
return useQuery({
|
||
|
|
queryKey: ['notification-types'],
|
||
|
|
queryFn: () => apiGet<{ items: NotificationTypeItem[] }>('/notifications/types'),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useNotificationPreferences() {
|
||
|
|
return useQuery({
|
||
|
|
queryKey: ['notification-preferences'],
|
||
|
|
queryFn: () => apiGet<{ items: { type_key: string; is_enabled: boolean }[] }>('/notifications/preferences'),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useUpdateNotificationPreference() {
|
||
|
|
const queryClient = useQueryClient();
|
||
|
|
return useMutation({
|
||
|
|
mutationFn: ({ typeKey, isEnabled }: { typeKey: string; isEnabled: boolean }) =>
|
||
|
|
apiPatch(`/notifications/preferences/${typeKey}`, { is_enabled: isEnabled }),
|
||
|
|
onSuccess: () => {
|
||
|
|
queryClient.invalidateQueries({ queryKey: ['notification-preferences'] });
|
||
|
|
queryClient.invalidateQueries({ queryKey: ['notification-types'] });
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|