a7e3890634
- Tags UI: TagsPage (CRUD, color picker), TagBadge, TagSelector (multi-select, inline creation) - Custom Fields Backend: model, schema, service, routes, migration 0041 - Custom Fields Frontend: CustomFieldsPage (definitions CRUD), CustomFieldRenderer (dynamic field rendering) - Custom Fields: _collect_custom_field_definitions() extended to merge DB definitions with plugin definitions - Notifications Bell: NotificationBell (30s polling, unread badge), NotificationDropdown, NotificationItem - NotificationBell integrated into TopBar - Routes: /tags, /settings/custom-fields registered - Settings nav: Custom Fields entry added - Menu items: Tags added to automation plugin manifest
102 lines
2.8 KiB
TypeScript
102 lines
2.8 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';
|
|
|
|
// ── Types matching backend schemas ──
|
|
|
|
export interface NotificationItem {
|
|
id: string;
|
|
type: string;
|
|
title: string;
|
|
body?: string | null;
|
|
read_at?: string | null;
|
|
created_at?: string | null;
|
|
}
|
|
|
|
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 interface UnreadCountResponse {
|
|
count: number;
|
|
}
|
|
|
|
export interface NotificationPreferenceItem {
|
|
type_key: string;
|
|
is_enabled: boolean;
|
|
}
|
|
|
|
export function useNotifications() {
|
|
return useQuery({
|
|
queryKey: ['notifications'],
|
|
queryFn: () => apiGet<PaginatedResponse<NotificationItem>>('/notifications'),
|
|
});
|
|
}
|
|
|
|
export function useUnreadNotificationCount(options?: { refetchInterval?: number }) {
|
|
return useQuery({
|
|
queryKey: ['notifications', 'unread-count'],
|
|
queryFn: async () => {
|
|
const data = await apiGet<UnreadCountResponse>('/notifications/unread-count');
|
|
return data.count;
|
|
},
|
|
refetchInterval: options?.refetchInterval,
|
|
});
|
|
}
|
|
|
|
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: NotificationPreferenceItem[] }>('/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'] });
|
|
},
|
|
});
|
|
}
|