Skip to content

Commit 1c46236

Browse files
Ni-2alsakhaev
authored andcommitted
feat: adding Telegram subscriptions
1 parent 5b804f3 commit 1c46236

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed
Lines changed: 3 additions & 0 deletions
Loading

apps/xen-tg-app/src/components/NewsSource.tsx

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import EmptyIcon from '@/assets/empty.svg'
12
import FilePlusIcon from '@/assets/file-plus'
23
import PlusIcon from '@/assets/plus'
34
import RedditIcon from '@/assets/reddit.svg'
@@ -36,34 +37,71 @@ const mutationFn = async ({
3637
throw new Error('Network response was not ok')
3738
}
3839
const data = await response.json()
40+
if (data.error) {
41+
throw new Error(data.error.message)
42+
}
3943
return data.result
4044
}
4145

46+
const getIcon = (source: TSubscription['source'] | null) => {
47+
switch (source) {
48+
case 'telegram':
49+
return TelegramIcon
50+
case 'reddit':
51+
return RedditIcon
52+
default:
53+
return EmptyIcon
54+
}
55+
}
56+
4257
export const NewSubscription: FC<{ onClose: () => void }> = ({ onClose }) => {
4358
const inputRef = useRef<HTMLInputElement>(null)
59+
4460
const [newLink, setNewLink] = useState('')
4561
const [showWrongSubscriptionNameMessage, setShowWrongSubscriptionNameMessage] = useState(false)
62+
const [source, setSource] = useState<'reddit' | 'telegram' | null>(null)
63+
const [isValid, setIsValid] = useState(false)
64+
4665
const queryClient = useQueryClient()
4766
const addSubscription = useMutation({
4867
mutationFn,
4968
onSuccess: () => {
5069
queryClient.invalidateQueries({ queryKey: ['subscriptions'] }).then(onClose)
5170
},
71+
onError: () => {
72+
setShowWrongSubscriptionNameMessage(true)
73+
},
5274
})
5375

5476
useEffect(() => {
5577
if (inputRef.current) inputRef.current.focus()
5678
}, [inputRef])
5779

80+
useEffect(() => {
81+
if (/^r\/[a-zA-Z0-9_]+$/.test(newLink)) {
82+
setSource('reddit')
83+
setIsValid(true)
84+
} else if (
85+
/^https:\/\/t\.me\/[a-zA-Z0-9_]+$/.test(newLink) ||
86+
/^@[a-zA-Z0-9_]+$/.test(newLink)
87+
) {
88+
setSource('telegram')
89+
setIsValid(true)
90+
} else {
91+
setSource(null)
92+
setIsValid(false)
93+
}
94+
}, [newLink])
95+
5896
const onSubmit = () => {
59-
const isValidated = /^r\/[a-zA-Z0-9_]+$/.test(newLink) // ToDo: hardcoded for Reddit
60-
if (!isValidated) {
97+
if (!source || !newLink) return
98+
if (!isValid) {
6199
setShowWrongSubscriptionNameMessage(true)
62100
} else {
63101
addSubscription.mutate({
64102
methodName: 'addSubscription',
65103
params: {
66-
source: 'reddit', // ToDo: hardcoded
104+
source,
67105
link: newLink,
68106
},
69107
})
@@ -73,7 +111,7 @@ export const NewSubscription: FC<{ onClose: () => void }> = ({ onClose }) => {
73111
return (
74112
<div className="flex w-full flex-col items-center justify-between gap-2 rounded-[10px] bg-[#ffffff] px-2.5 py-1.5 dark:bg-[#f8f9ff4c]">
75113
<form className="flex w-full items-center justify-between gap-3.5" action={onSubmit}>
76-
<img src={RedditIcon} alt="Reddit icon" />
114+
<img src={getIcon(source)} alt="Source icon" />
77115

78116
<div className="flex flex-1 flex-col gap-0.5 overflow-hidden">
79117
<input
@@ -122,9 +160,16 @@ export const NewSubscription: FC<{ onClose: () => void }> = ({ onClose }) => {
122160
</form>
123161
{showWrongSubscriptionNameMessage ? (
124162
<div className="text-destructive flex w-full items-center gap-1 ps-10 text-xs">
125-
<p>
126-
<b>Error:</b> Subscription name is invalid. It's supposed to look like <b>r/beatles</b>
127-
</p>
163+
{addSubscription.isError ? (
164+
<p>
165+
<b>Error:</b> {addSubscription.error.message}
166+
</p>
167+
) : (
168+
<p>
169+
<b>Error:</b> Subscription name is invalid. It's supposed to look like{' '}
170+
<b>r/beatles</b> for Reddit, <b>@durov</b> or <b>https://t.me/durov</b> for Telegram.
171+
</p>
172+
)}
128173
</div>
129174
) : null}
130175
</div>

0 commit comments

Comments
 (0)