import { Trans } from "@lingui/react/macro" import { Box, Button, Group, Stack, Stepper, TextInput } from "@mantine/core" import { useForm } from "@mantine/form" import { useState } from "react" import { useAsyncCallback } from "react-async-hook" import { TbRss } from "react-icons/tb" import { client, errorToStrings } from "@/app/client" import { Constants } from "@/app/constants" import { redirectToFeed, redirectToSelectedSource } from "@/app/redirect/thunks" import { useAppDispatch } from "@/app/store" import { reloadTree } from "@/app/tree/thunks" import type { FeedInfoRequest, SubscribeRequest } from "@/app/types" import { Alert } from "@/components/Alert" import { ReceivePushNotificationsChechbox } from "@/components/ReceivePushNotificationsChechbox" import { CategorySelect } from "./CategorySelect" export function Subscribe() { const [activeStep, setActiveStep] = useState(0) const dispatch = useAppDispatch() const step0Form = useForm({ initialValues: { url: "", }, }) const step1Form = useForm({ initialValues: { url: "", title: "", categoryId: Constants.categories.all.id, pushNotificationsEnabled: false, }, }) const fetchFeed = useAsyncCallback(client.feed.fetchFeed, { onSuccess: ({ data }) => { step1Form.setFieldValue("url", data.url) step1Form.setFieldValue("title", data.title) setActiveStep(step => step + 1) }, }) const subscribe = useAsyncCallback(client.feed.subscribe, { onSuccess: sub => { dispatch(reloadTree()).then(() => dispatch(redirectToFeed(sub.data))) }, }) const previousStep = () => { if (activeStep === 0) { dispatch(redirectToSelectedSource()) } else { setActiveStep(activeStep - 1) } } const nextStep = (e: React.FormEvent) => { if (activeStep === 0) { step0Form.onSubmit(fetchFeed.execute)(e) } else if (activeStep === 1) { step1Form.onSubmit(subscribe.execute)(e) } } return ( <> {fetchFeed.error && ( )} {subscribe.error && ( )}
Analyze feed} description={Check that the feed is working} allowStepSelect={activeStep === 1} > Feed URL} placeholder="https://www.mysite.com/rss" description={ The URL for the feed you want to subscribe to. You can also use the website's url directly and CommaFeed will try to find the feed in the page. } required autoFocus {...step0Form.getInputProps("url")} /> Subscribe} description={Subscribe to the feed} allowStepSelect={false} > Feed URL} {...step1Form.getInputProps("url")} disabled /> Feed name} {...step1Form.getInputProps("title")} required autoFocus /> Category} {...step1Form.getInputProps("categoryId")} clearable /> {activeStep === 0 && ( )} {activeStep === 1 && ( )}
) }