|
| 1 | +import PlusIcon from '@/assets/plus' |
| 2 | +import SyncIcon from '@/assets/sync' |
| 3 | +import { API_URL } from '@/env' |
| 4 | +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' |
| 5 | +import { formatDistance } from 'date-fns' |
| 6 | +import { useEffect, useState } from 'react' |
| 7 | +import { TSubscription } from '../types' |
| 8 | +import Header from './Header' |
| 9 | +import Layout from './Layout' |
| 10 | +import { NewSubscription, Subscription } from './NewsSource' |
| 11 | +import Spinner from './Spinner' |
| 12 | + |
| 13 | +const mutationFn = async ({ |
| 14 | + methodName, |
| 15 | + params, |
| 16 | +}: { |
| 17 | + methodName: string |
| 18 | + params?: { [key: string]: string | number } |
| 19 | +}) => { |
| 20 | + if (!window.Telegram.WebApp.initData) { |
| 21 | + throw new Error('Telegram is not available') |
| 22 | + } |
| 23 | + const response = await fetch(API_URL, { |
| 24 | + method: 'POST', |
| 25 | + headers: { |
| 26 | + Authorization: `Bearer ${window.Telegram.WebApp.initData}`, |
| 27 | + 'Content-Type': 'application/json', |
| 28 | + }, |
| 29 | + body: JSON.stringify({ |
| 30 | + jsonrpc: '2.0', |
| 31 | + method: methodName, |
| 32 | + params: params ?? {}, |
| 33 | + id: 1, |
| 34 | + }), |
| 35 | + }) |
| 36 | + if (!response.ok) { |
| 37 | + throw new Error('Network response was not ok') |
| 38 | + } |
| 39 | + const data = await response.json() |
| 40 | + return data.result |
| 41 | +} |
| 42 | + |
| 43 | +const queryFn = (name: string, params?: { [key: string]: string | number }) => async () => { |
| 44 | + if (!window.Telegram.WebApp.initData) { |
| 45 | + throw new Error('Telegram is not available') |
| 46 | + } |
| 47 | + const response = await fetch(API_URL, { |
| 48 | + method: 'POST', |
| 49 | + headers: { |
| 50 | + Authorization: `Bearer ${window.Telegram.WebApp.initData}`, |
| 51 | + 'Content-Type': 'application/json', |
| 52 | + }, |
| 53 | + body: JSON.stringify({ |
| 54 | + jsonrpc: '2.0', |
| 55 | + method: name, |
| 56 | + params: params ?? {}, |
| 57 | + id: 1, |
| 58 | + }), |
| 59 | + }) |
| 60 | + if (!response.ok) { |
| 61 | + throw new Error('Network response was not ok') |
| 62 | + } |
| 63 | + const data = await response.json() |
| 64 | + return data.result |
| 65 | +} |
| 66 | + |
| 67 | +const NewsMonitor = () => { |
| 68 | + const queryClient = useQueryClient() |
| 69 | + const [showNewSubscriptionForm, setShowNewSubscriptionForm] = useState(false) |
| 70 | + |
| 71 | + const { data: subscriptions, isPending } = useQuery<{ items: TSubscription[]; total: number }>({ |
| 72 | + queryKey: ['subscriptions'], |
| 73 | + queryFn: queryFn('getSubscriptions', { |
| 74 | + offset: 0, |
| 75 | + limit: 10, |
| 76 | + }), |
| 77 | + }) |
| 78 | + |
| 79 | + const { data: nextScanOfSubscriptions, isPending: isPendingNextScanOfSubscriptions } = useQuery<{ |
| 80 | + nextScanAt: string |
| 81 | + }>({ |
| 82 | + queryKey: ['nextScanOfSubscriptions'], |
| 83 | + queryFn: queryFn('getNextScanOfSubscriptions'), |
| 84 | + }) |
| 85 | + |
| 86 | + const handleUpdateSubscription = useMutation({ |
| 87 | + mutationFn, |
| 88 | + onSuccess: () => { |
| 89 | + queryClient.invalidateQueries({ queryKey: ['subscriptions'] }) |
| 90 | + queryClient.invalidateQueries({ queryKey: ['nextScanOfSubscriptions'] }) |
| 91 | + }, |
| 92 | + }) |
| 93 | + |
| 94 | + const onUpdate = () => handleUpdateSubscription.mutate({ methodName: 'scanSubscriptions' }) |
| 95 | + |
| 96 | + useEffect(onUpdate, []) |
| 97 | + |
| 98 | + return ( |
| 99 | + <Layout> |
| 100 | + <Header /> |
| 101 | + <div className="z-1 flex w-full flex-col items-center justify-between gap-2.5 rounded-xl border border-[#f8f9ff66] p-2.5 backdrop-blur-3xl backdrop-opacity-80"> |
| 102 | + <div className="my-1.5 flex w-full items-center justify-between"> |
| 103 | + <div className="flex flex-col items-start justify-start"> |
| 104 | + <h1 className="text-center text-2xl font-bold">News Monitor</h1> |
| 105 | + <div className="flex items-center justify-center gap-1 text-[12px]/[150%] text-[#7A818B]"> |
| 106 | + Next scan:{' '} |
| 107 | + <button |
| 108 | + className="flex items-center justify-center font-semibold text-(--my-primary)" |
| 109 | + onClick={onUpdate} |
| 110 | + disabled={isPendingNextScanOfSubscriptions || handleUpdateSubscription.isPending} |
| 111 | + > |
| 112 | + {nextScanOfSubscriptions?.nextScanAt |
| 113 | + ? formatDistance(new Date(nextScanOfSubscriptions.nextScanAt), new Date(), { |
| 114 | + addSuffix: true, |
| 115 | + }) |
| 116 | + : '-'} |
| 117 | + <div |
| 118 | + className={`${isPendingNextScanOfSubscriptions || handleUpdateSubscription.isPending ? 'animate-spin-back' : ''} flex cursor-pointer items-center justify-between p-1.5 transition`} |
| 119 | + > |
| 120 | + <SyncIcon /> |
| 121 | + </div> |
| 122 | + </button> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + <button |
| 126 | + className="mr-3.5 flex cursor-pointer items-center justify-center p-1.5 text-[#7A818B] transition hover:not-disabled:text-(--color-main-text)" |
| 127 | + onClick={() => setShowNewSubscriptionForm(true)} |
| 128 | + disabled={showNewSubscriptionForm} |
| 129 | + > |
| 130 | + {isPending ? <Spinner /> : <PlusIcon />} |
| 131 | + </button> |
| 132 | + </div> |
| 133 | + {showNewSubscriptionForm ? ( |
| 134 | + <NewSubscription onClose={() => setShowNewSubscriptionForm(false)} /> |
| 135 | + ) : null} |
| 136 | + {subscriptions?.items.map((newsSource) => ( |
| 137 | + <Subscription key={newsSource.id} subscription={newsSource} /> |
| 138 | + ))} |
| 139 | + </div> |
| 140 | + </Layout> |
| 141 | + ) |
| 142 | +} |
| 143 | + |
| 144 | +export default NewsMonitor |
0 commit comments