import React, { useState } from 'react' import { createFileRoute } from '@tanstack/react-router' import { ChevronLeft } from 'lucide-react' import { AppContainer, p as MyText, pInput as MyTextInput } from 'web-components' import { trpc } from '@/lib/trpc-client' import type { UserMiniInfo } from '@packages/shared' import { MultiSelect } from '../components/MultiSelect' export const Route = createFileRoute('/dashboard/notifications')({ component: SendNotifications, }) type User = Omit & { name: string | null isEligibleForNotif: boolean } function SendNotifications() { const [selectedUserIds, setSelectedUserIds] = useState([]) const [title, setTitle] = useState('') const [message, setMessage] = useState('') const [searchQuery, setSearchQuery] = useState('') const { data: usersData, isLoading: isLoadingUsers } = trpc.admin.user.getUsersForNotification.useQuery({ search: searchQuery, }) const sendNotification = trpc.admin.user.sendNotification.useMutation({ onSuccess: () => { window.alert('Success: Notification sent successfully!') setSelectedUserIds([]) setTitle('') setMessage('') }, onError: (error: any) => { window.alert(`Error: ${error.message || 'Failed to send notification'}`) }, }) const eligibleUsers = usersData?.users.filter((u: User) => u.isEligibleForNotif) || [] const dropdownOptions = eligibleUsers.map((user: User) => ({ label: `${user.mobile || 'No Mobile'}${user.name ? ` - ${user.name}` : ''}`, value: user.id, })) const handleSend = async () => { if (title.trim().length === 0) { window.alert('Error: Please enter a title') return } if (message.trim().length === 0) { window.alert('Error: Please enter a message') return } const isSendingToAll = selectedUserIds.length === 0 if (isSendingToAll) { const confirmed = window.confirm( 'Send to All Users? This will send the notification to all users with push tokens. Continue?' ) if (!confirmed) return } try { await sendNotification.mutateAsync({ userIds: selectedUserIds, title: title.trim(), text: message.trim(), }) } catch (error: any) { window.alert(`Error: ${error.message || 'Failed to send notification'}`) } } const getDisplayText = () => { if (selectedUserIds.length === 0) return 'All Users' if (selectedUserIds.length === 1) { const user = eligibleUsers.find((u: User) => u.id === selectedUserIds[0]) return user ? `${user.mobile}${user.name ? ` - ${user.name}` : ''}` : '1 user selected' } return `${selectedUserIds.length} users selected` } if (isLoadingUsers) { return (
Loading users...
) } const isSendDisabled = sendNotification.isPending || title.trim().length === 0 || message.trim().length === 0 return (
Send Notifications
Title setTitle(e.target.value)} placeholder="Enter notification title..." className="text-gray-900" />
Message setMessage(e.target.value)} placeholder="Enter notification message..." multiline numberOfLines={4} className="text-gray-900" />
Select Users (Optional) setSelectedUserIds(value as number[])} onSearch={(query: string) => setSearchQuery(query)} /> {getDisplayText()} Leave empty to send to all users
) }