Working on notifications channels.

This commit is contained in:
charlesgauthereau
2025-11-29 23:12:06 +01:00
parent ac951378e8
commit 8980e6d4a8
10 changed files with 83 additions and 24 deletions
@@ -9,6 +9,7 @@ import {Button} from "@/components/ui/button";
import {NotifierForm} from "@/components/wrappers/dashboard/common/notifier/notifier-form/notifier-form"; import {NotifierForm} from "@/components/wrappers/dashboard/common/notifier/notifier-form/notifier-form";
import {OrganizationWithMembers} from "@/db/schema/03_organization"; import {OrganizationWithMembers} from "@/db/schema/03_organization";
import {NotificationChannel} from "@/db/schema/09_notification-channel"; import {NotificationChannel} from "@/db/schema/09_notification-channel";
import {useIsMobile} from "@/hooks/use-mobile";
type OrganizationNotifierAddModalProps = { type OrganizationNotifierAddModalProps = {
notificationChannel?: NotificationChannel notificationChannel?: NotificationChannel
@@ -24,6 +25,7 @@ export const NotifierAddEditModal = ({
open, open,
onOpenChangeAction onOpenChangeAction
}: OrganizationNotifierAddModalProps) => { }: OrganizationNotifierAddModalProps) => {
const isMobile = useIsMobile();
const isCreate = !Boolean(notificationChannel); const isCreate = !Boolean(notificationChannel);
@@ -32,7 +34,7 @@ export const NotifierAddEditModal = ({
<DialogTrigger asChild> <DialogTrigger asChild>
{isCreate ? {isCreate ?
<Button> <Button>
<Plus/> Add notification channel <Plus/>{!isMobile && `Add notification channel` }
</Button> </Button>
: :
<Button <Button
@@ -9,12 +9,14 @@ import {
} from "@/components/wrappers/dashboard/common/notifier/notifier-form/notifier-form.action"; } from "@/components/wrappers/dashboard/common/notifier/notifier-form/notifier-form.action";
import {toast} from "sonner"; import {toast} from "sonner";
import {useState} from "react"; import {useState} from "react";
import {Organization, OrganizationWithMembers} from "@/db/schema/03_organization";
export type EditNotifierButtonProps = { export type EditNotifierButtonProps = {
notificationChannel: NotificationChannel; notificationChannel: NotificationChannel;
organization? : OrganizationWithMembers;
}; };
export const EditNotifierButton = ({notificationChannel}: EditNotifierButtonProps) => { export const EditNotifierButton = ({notificationChannel, organization}: EditNotifierButtonProps) => {
const router = useRouter(); const router = useRouter();
const [isAddModalOpen, setIsAddModalOpen] = useState(false); const [isAddModalOpen, setIsAddModalOpen] = useState(false);
@@ -51,6 +53,7 @@ export const EditNotifierButton = ({notificationChannel}: EditNotifierButtonProp
}} }}
/> />
<NotifierAddEditModal <NotifierAddEditModal
organization={organization}
notificationChannel={notificationChannel} notificationChannel={notificationChannel}
open={isAddModalOpen} open={isAddModalOpen}
onOpenChangeAction={setIsAddModalOpen} onOpenChangeAction={setIsAddModalOpen}
@@ -9,11 +9,11 @@ import {
DeleteNotifierButton DeleteNotifierButton
} from "@/components/wrappers/dashboard/common/notifier/notifier-card/button-delete-notifier"; } from "@/components/wrappers/dashboard/common/notifier/notifier-card/button-delete-notifier";
import {EditNotifierButton} from "@/components/wrappers/dashboard/common/notifier/notifier-card/button-edit-notifier"; import {EditNotifierButton} from "@/components/wrappers/dashboard/common/notifier/notifier-card/button-edit-notifier";
import {Organization} from "@/db/schema/03_organization"; import {Organization, OrganizationWithMembers} from "@/db/schema/03_organization";
export type NotifierCardProps = { export type NotifierCardProps = {
data: NotificationChannel; data: NotificationChannel;
organization?: Organization; organization?: OrganizationWithMembers;
}; };
export const NotifierCard = (props: NotifierCardProps) => { export const NotifierCard = (props: NotifierCardProps) => {
@@ -40,7 +40,9 @@ export const NotifierCard = (props: NotifierCardProps) => {
</div> </div>
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<EditNotifierButton notificationChannel={data}/> <EditNotifierButton
organization={organization}
notificationChannel={data}/>
<DeleteNotifierButton <DeleteNotifierButton
organizationId={organization?.id} organizationId={organization?.id}
notificationChannelId={data.id} notificationChannelId={data.id}
@@ -136,7 +136,7 @@ export const NotifierForm = ({onSuccessAction, organization, defaultValues}: Not
<div className="flex justify-between"> <div className="flex justify-between">
<div> <div>
{defaultValues && ( {defaultValues && (
<NotifierTestChannelButton notificationChannel={defaultValues}/> <NotifierTestChannelButton organizationId={organization?.id} notificationChannel={defaultValues}/>
)} )}
</div> </div>
<div className="flex gap-2"> <div className="flex gap-2">
@@ -6,13 +6,17 @@ import {dispatchNotification} from "@/features/notifications/dispatch";
import {EventPayload} from "@/features/notifications/types"; import {EventPayload} from "@/features/notifications/types";
import {Send} from "lucide-react"; import {Send} from "lucide-react";
import {toast} from "sonner"; import {toast} from "sonner";
import {useIsMobile} from "@/hooks/use-mobile";
import {cn} from "@/lib/utils";
type NotifierTestChannelButtonProps = { type NotifierTestChannelButtonProps = {
notificationChannel: NotificationChannel; notificationChannel: NotificationChannel;
organizationId?: string;
} }
export const NotifierTestChannelButton = ({notificationChannel}: NotifierTestChannelButtonProps) => { export const NotifierTestChannelButton = ({notificationChannel, organizationId}: NotifierTestChannelButtonProps) => {
const isMobile = useIsMobile()
const mutation = useMutation({ const mutation = useMutation({
mutationFn: async () => { mutationFn: async () => {
@@ -23,6 +27,8 @@ export const NotifierTestChannelButton = ({notificationChannel}: NotifierTestCha
// data: {host: 'db-prod-01', error: 'connection timeout'}, // data: {host: 'db-prod-01', error: 'connection timeout'},
// }; // };
console.log("organizationId ici", organizationId);
const payload: EventPayload = { const payload: EventPayload = {
title: 'Test Channel', title: 'Test Channel',
message: `We are testing channel ${notificationChannel.name}`, message: `We are testing channel ${notificationChannel.name}`,
@@ -30,7 +36,7 @@ export const NotifierTestChannelButton = ({notificationChannel}: NotifierTestCha
// data: {host: 'db-prod-01', error: 'connection timeout'}, // data: {host: 'db-prod-01', error: 'connection timeout'},
}; };
const result = await dispatchNotification(payload, undefined, notificationChannel.id); const result = await dispatchNotification(payload, undefined, notificationChannel.id, organizationId);
if (result.success) { if (result.success) {
toast.success(result.message); toast.success(result.message);
@@ -51,14 +57,13 @@ export const NotifierTestChannelButton = ({notificationChannel}: NotifierTestCha
> >
{mutation.isPending ? ( {mutation.isPending ? (
<> <>
<div className="mr-2 h-4 w-4 animate-spin rounded-full border-2 border-white/30 border-t-white"/> <div className={cn(" h-4 w-4 animate-spin rounded-full border-2 border-white/30 border-t-white", !isMobile && "mr-2")}/>
Sending... {!isMobile && `Sending...`}
</> </>
) : ( ) : (
<> <div className="flex flex-row justify-center items-center">
<Send className="mr-2 h-4 w-4"/> <Send className={cn("h-4 w-4", !isMobile && "mr-2")}/>{!isMobile && ` Test Channel`}
Test Channel </div>
</>
)} )}
</Button> </Button>
) )
@@ -63,17 +63,16 @@ export const OrganizationNotifiersTab = ({
<div className="flex flex-col gap-y-6 h-full py-4"> <div className="flex flex-col gap-y-6 h-full py-4">
<div className="h-full flex flex-col gap-y-6"> <div className="h-full flex flex-col gap-y-6">
<div className={cn("hidden flex-row justify-between items-start", hasNotifiers && "flex")}> <div className={cn("hidden flex-row justify-between items-start", hasNotifiers && "flex")}>
<div className="max-w-2xl"> <div className="max-w-2xl ">
<h3 className="text-xl font-semibold text-balance mb-1"> <h3 className="text-xl font-semibold text-balance mb-1">
Notification Settings Notification Settings
</h3> </h3>
<p className="text-muted-foreground leading-relaxed"> <p className="text-muted-foreground leading-relaxed ">
Configure how and when you receive alerts about your services and Configure how and when you receive alerts about your services and
infrastructure. infrastructure.
</p> </p>
</div> </div>
<NotifierAddEditModal <NotifierAddEditModal
organization={organization} organization={organization}
open={isAddModalOpen} open={isAddModalOpen}
onOpenChangeAction={setIsAddModalOpen} onOpenChangeAction={setIsAddModalOpen}
+4 -1
View File
@@ -11,8 +11,10 @@ export async function dispatchNotification(
payload: EventPayload, payload: EventPayload,
policyId?: string, policyId?: string,
channelId?: string, channelId?: string,
organizationId?: string,
): Promise<DispatchResult> { ): Promise<DispatchResult> {
// // 1. Get policy + channel // // 1. Get policy + channel
// const policy = await db // const policy = await db
// .select({ // .select({
@@ -66,7 +68,7 @@ export async function dispatchNotification(
.values({ .values({
channelId: channel.id, channelId: channel.id,
// policyId: policy.id, // policyId: policy.id,
// organizationId: organizationId || null, organizationId: organizationId || null,
title: payload.title, title: payload.title,
message: payload.message, message: payload.message,
level: payload.level, level: payload.level,
@@ -74,6 +76,7 @@ export async function dispatchNotification(
success: result.success, success: result.success,
error: result.success ? null : result.error, error: result.success ? null : result.error,
providerResponse: result.response || null, providerResponse: result.response || null,
}) })
.returning({id: notificationLog.id}); .returning({id: notificationLog.id});
@@ -1,13 +1,13 @@
"use server" "use server"
import type { ProviderKind, EventPayload, DispatchResult } from '../types'; import type {ProviderKind, EventPayload, DispatchResult} from '../types';
// import { sendSlack } from './slack'; import {sendSlack} from './slack';
import { sendSmtp } from './smtp'; import {sendSmtp} from './smtp';
const handlers: Record< const handlers: Record<
ProviderKind, ProviderKind,
(config: any, payload: EventPayload) => Promise<DispatchResult> (config: any, payload: EventPayload) => Promise<DispatchResult>
> = { > = {
// slack: sendSlack, slack: sendSlack,
smtp: sendSmtp, smtp: sendSmtp,
}; };
@@ -0,0 +1,46 @@
import type {EventPayload, DispatchResult} from '../types';
export async function sendSlack(
config: { slackWebhook: string },
payload: EventPayload
): Promise<DispatchResult> {
const {slackWebhook: webhookUrl} = config;
const text = `*${payload.title}*\n${payload.message}`;
const blocks = [
{
type: 'section',
text: {type: 'mrkdwn', text: `*${payload.title}*`},
},
{type: 'section', text: {type: 'mrkdwn', text: payload.message}},
payload.data
? {
type: 'context',
elements: [{type: 'mrkdwn', text: `*Data:* \`\`\`${JSON.stringify(payload.data, null, 2)}\`\`\``}],
}
: null,
].filter(Boolean);
const body = {
text,
blocks,
};
const res = await fetch(webhookUrl, {
method: 'POST',
body: JSON.stringify(body),
headers: {'Content-Type': 'application/json'},
});
if (!res.ok) {
const err = await res.text();
throw new Error(`Slack error: ${res.status} ${err}`);
}
return {
success: true,
provider: 'slack',
message: 'Sent to Slack',
response: await res.text(),
};
}
+1 -2
View File
@@ -1,5 +1,4 @@
// export type ProviderKind = 'slack' | 'smtp'; export type ProviderKind = 'slack' | 'smtp';
export type ProviderKind = 'smtp';
export interface DispatchResult { export interface DispatchResult {
success: boolean; success: boolean;