import React, { useState, useEffect } from "react"; import { supabase } from "../../lib/supabaseClient"; import useAuthStore from "../../store/authStore"; /** * WebhookSettings — Admin component to configure Slack/Teams webhook URL. * Stores webhook URL per company tenant in Supabase. */ const WebhookSettings = () => { const { profile } = useAuthStore(); const [webhookUrl, setWebhookUrl] = useState(""); const [isEnabled, setIsEnabled] = useState(false); const [loading, setLoading] = useState(false); const [fetching, setFetching] = useState(true); const [message, setMessage] = useState({ text: "", type: "" }); const companyId = profile?.company_id; useEffect(() => { const fetchSettings = async () => { if (!companyId) return; setFetching(true); try { const { data } = await supabase .from("webhook_settings") .select("webhook_url, is_enabled") .eq("company_id", companyId) .single(); if (data) { setWebhookUrl(data.webhook_url || ""); setIsEnabled(data.is_enabled || false); } } catch { console.log("No webhook settings found — first time setup"); } finally { setFetching(false); } }; fetchSettings(); }, [companyId]); const detectPlatform = (url) => { if (url.includes("hooks.slack.com")) return "Slack"; if (url.includes("webhook.office.com") || url.includes("outlook.office.com")) return "Microsoft Teams"; return null; }; const validateUrl = (url) => { if (!url) return false; return url.startsWith("https://") && detectPlatform(url) !== null; }; const handleSave = async () => { if (!validateUrl(webhookUrl)) { setMessage({ text: "❌ Please enter a valid Slack or Microsoft Teams webhook URL.", type: "error", }); return; } setLoading(true); setMessage({ text: "", type: "" }); try { const { error } = await supabase.from("webhook_settings").upsert( { company_id: companyId, webhook_url: webhookUrl, is_enabled: isEnabled, updated_at: new Date().toISOString(), }, { onConflict: "company_id" } ); if (error) throw error; setMessage({ text: `✅ Webhook saved successfully for ${detectPlatform(webhookUrl)}!`, type: "success", }); } catch (err) { setMessage({ text: `❌ Failed to save: ${err.message}`, type: "error", }); } finally { setLoading(false); } }; const handleToggle = async () => { const newValue = !isEnabled; setIsEnabled(newValue); if (webhookUrl) { await supabase.from("webhook_settings").upsert( { company_id: companyId, webhook_url: webhookUrl, is_enabled: newValue, updated_at: new Date().toISOString(), }, { onConflict: "company_id" } ); } }; if (fetching) { return (
); } const platform = detectPlatform(webhookUrl); return (

Webhook Notifications

Get critical ticket alerts in Slack or Microsoft Teams

{/* Webhook URL Input */}
{ setWebhookUrl(e.target.value); setMessage({ text: "", type: "" }); }} placeholder="https://hooks.slack.com/... or https://outlook.office.com/webhook/..." className="w-full bg-gray-800 border border-gray-600 rounded-lg px-4 py-3 text-white text-sm placeholder-gray-500 focus:outline-none focus:border-blue-500 focus:ring-1 focus:ring-blue-500 transition-colors" /> {/* Platform Detection Badge */} {platform && (
✓ {platform} webhook detected
)}
{/* Toggle */}

Enable Notifications

Send alerts for Critical/High priority tickets

{/* Message */} {message.text && (
{message.text}
)} {/* Save Button */} {/* Info */}

ℹ️ How to get webhook URL: For Slack: Create an Incoming Webhook in your Slack App settings. For Teams: Go to channel → Connectors → Incoming Webhook.

); }; export default WebhookSettings;