freshyo/apps/admin-web/src/routes/dashboard.notifications.tsx
shafi54 5e96f7a747 Revert "enh"
This reverts commit a724cd7bff.
2026-10-28 20:02:04 +05:30

168 lines
5.8 KiB
TypeScript

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<UserMiniInfo, 'name'> & {
name: string | null
isEligibleForNotif: boolean
}
function SendNotifications() {
const [selectedUserIds, setSelectedUserIds] = useState<number[]>([])
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 (
<AppContainer>
<div className="flex flex-1 items-center justify-center">
<div className="h-8 w-8 animate-spin rounded-full border-2 border-gray-300 border-t-blue-500" />
<MyText className="mt-4 text-gray-500">Loading users...</MyText>
</div>
</AppContainer>
)
}
const isSendDisabled = sendNotification.isPending || title.trim().length === 0 || message.trim().length === 0
return (
<AppContainer>
<div className="flex-1 bg-gray-50">
<div className="flex-row flex items-center border-b border-gray-200 bg-white px-4 py-4">
<button
onClick={() => window.history.back()}
className="-ml-4 p-2"
aria-label="Go back"
>
<ChevronLeft size={24} color="#374151" />
</button>
<MyText className="ml-2 text-xl font-bold text-gray-900">Send Notifications</MyText>
</div>
<div className="flex-1 p-4">
<div className="mb-4 rounded-xl border border-gray-100 bg-white p-4 shadow-sm">
<MyText className="mb-3 text-base font-bold text-gray-900">Title</MyText>
<MyTextInput
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="Enter notification title..."
className="text-gray-900"
/>
</div>
<div className="mb-4 rounded-xl border border-gray-100 bg-white p-4 shadow-sm">
<MyText className="mb-3 text-base font-bold text-gray-900">Message</MyText>
<MyTextInput
value={message}
onChange={(e) => setMessage(e.target.value)}
placeholder="Enter notification message..."
multiline
numberOfLines={4}
className="text-gray-900"
/>
</div>
<div className="mb-4 rounded-xl border border-gray-100 bg-white p-4 shadow-sm">
<MyText className="mb-3 text-base font-bold text-gray-900">Select Users (Optional)</MyText>
<MultiSelect
label="Select Users"
value={selectedUserIds}
options={dropdownOptions}
onValueChange={(value) => setSelectedUserIds(value as number[])}
onSearch={(query: string) => setSearchQuery(query)}
/>
<MyText className="mt-2 text-sm text-gray-500">
{getDisplayText()}
</MyText>
<MyText className="mt-1 text-xs text-blue-600">
Leave empty to send to all users
</MyText>
</div>
<button
onClick={handleSend}
disabled={isSendDisabled}
className={`items-center rounded-xl py-4 shadow-sm ${
isSendDisabled ? 'bg-gray-300' : 'bg-blue-600'
}`}
>
<MyText className="text-base font-bold text-white">
{sendNotification.isPending ? 'Sending...' : selectedUserIds.length === 0 ? 'Send to All Users' : 'Send Notification'}
</MyText>
</button>
</div>
</div>
</AppContainer>
)
}