feat: subscribe form
This commit is contained in:
+36
-11
@@ -14,38 +14,63 @@ import { Input } from "@renderer/components/ui/input"
|
||||
import { useEffect } from "react"
|
||||
|
||||
const formSchema = z.object({
|
||||
url: z.string().min(1),
|
||||
keyword: z.string().min(1),
|
||||
})
|
||||
|
||||
export function FollowUserForm() {
|
||||
const info: {
|
||||
[key: string]: {
|
||||
label: string
|
||||
prefix?: string
|
||||
}
|
||||
} = {
|
||||
general: {
|
||||
label: "Any URL or Keyword",
|
||||
},
|
||||
rss: {
|
||||
label: "RSS URL",
|
||||
},
|
||||
rsshub: {
|
||||
label: "RSSHub Route",
|
||||
prefix: "rsshub://",
|
||||
},
|
||||
user: {
|
||||
label: "User Handle",
|
||||
prefix: "follow://",
|
||||
},
|
||||
}
|
||||
|
||||
export function SubscribeForm({ type }: { type: string }) {
|
||||
const prefix = info[type].prefix
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues: {
|
||||
url: "",
|
||||
keyword: prefix,
|
||||
},
|
||||
})
|
||||
function onSubmit(values: z.infer<typeof formSchema>) {
|
||||
console.log(values)
|
||||
}
|
||||
|
||||
const url = form.watch("url")
|
||||
const keyword = form.watch("keyword")
|
||||
useEffect(() => {
|
||||
if (!url.startsWith("follow://")) {
|
||||
form.setValue("url", "follow://")
|
||||
} else if (url.startsWith("follow://follow://")) {
|
||||
form.setValue("url", url.slice(9))
|
||||
if (prefix) {
|
||||
if (!keyword.startsWith(prefix)) {
|
||||
form.setValue("keyword", prefix)
|
||||
} else if (keyword.startsWith(`${prefix}${prefix}`)) {
|
||||
form.setValue("keyword", keyword.slice(prefix.length))
|
||||
}
|
||||
}
|
||||
}, [url])
|
||||
}, [keyword])
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="url"
|
||||
name="keyword"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>User Handle</FormLabel>
|
||||
<FormLabel>{info[type]?.label}</FormLabel>
|
||||
<FormControl>
|
||||
<Input {...field} />
|
||||
</FormControl>
|
||||
@@ -1,50 +0,0 @@
|
||||
import { z } from "zod"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { Button } from "@renderer/components/ui/button"
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from "@renderer/components/ui/form"
|
||||
import { Input } from "@renderer/components/ui/input"
|
||||
|
||||
const formSchema = z.object({
|
||||
url: z.string().min(1),
|
||||
})
|
||||
|
||||
export function RSSForm() {
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues: {
|
||||
url: "",
|
||||
},
|
||||
})
|
||||
function onSubmit(values: z.infer<typeof formSchema>) {
|
||||
console.log(values)
|
||||
}
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="url"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>URL or Keyword</FormLabel>
|
||||
<FormControl>
|
||||
<Input {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<Button type="submit">Search</Button>
|
||||
</form>
|
||||
</Form>
|
||||
)
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
import { z } from "zod"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { Button } from "@renderer/components/ui/button"
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from "@renderer/components/ui/form"
|
||||
import { Input } from "@renderer/components/ui/input"
|
||||
import { useEffect } from "react"
|
||||
|
||||
const formSchema = z.object({
|
||||
url: z.string().min(1),
|
||||
})
|
||||
|
||||
export function RSSHubForm() {
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues: {
|
||||
url: "rsshub://",
|
||||
},
|
||||
})
|
||||
function onSubmit(values: z.infer<typeof formSchema>) {
|
||||
console.log(values)
|
||||
}
|
||||
|
||||
const url = form.watch("url")
|
||||
useEffect(() => {
|
||||
if (!url.startsWith("rsshub://")) {
|
||||
form.setValue("url", "rsshub://")
|
||||
} else if (url.startsWith("rsshub://rsshub://")) {
|
||||
form.setValue("url", url.slice(9))
|
||||
}
|
||||
}, [url])
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="url"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>RSSHub Route</FormLabel>
|
||||
<FormControl>
|
||||
<Input {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<Button type="submit">Search</Button>
|
||||
</form>
|
||||
</Form>
|
||||
)
|
||||
}
|
||||
@@ -5,42 +5,48 @@ import {
|
||||
TabsTrigger,
|
||||
} from "@renderer/components/ui/tabs"
|
||||
import { Recommendations } from "@renderer/components/subscribe/recommendations"
|
||||
import { RSSForm } from "@renderer/components/subscribe/rss-form"
|
||||
import { RSSHubForm } from "@renderer/components/subscribe/rsshub-form"
|
||||
import { FollowUserForm } from "@renderer/components/subscribe/follow-user-form"
|
||||
import { SubscribeForm } from "@renderer/components/subscribe/form"
|
||||
|
||||
export function Component() {
|
||||
const tabs = [
|
||||
{
|
||||
name: "General",
|
||||
value: "general",
|
||||
},
|
||||
{
|
||||
name: "RSS",
|
||||
content: <RSSForm />,
|
||||
value: "rss",
|
||||
},
|
||||
{
|
||||
name: "RSSHub",
|
||||
content: <RSSHubForm />,
|
||||
value: "rsshub",
|
||||
},
|
||||
{
|
||||
name: "Follow User",
|
||||
content: <FollowUserForm />,
|
||||
value: "user",
|
||||
},
|
||||
{
|
||||
name: "RSS3",
|
||||
value: "rss3",
|
||||
disabled: true,
|
||||
},
|
||||
{
|
||||
name: "Email",
|
||||
value: "email",
|
||||
disabled: true,
|
||||
},
|
||||
]
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-8 items-center justify-center w-full">
|
||||
<div className="text-2xl font-bold">Subscribe</div>
|
||||
<Tabs defaultValue="RSS">
|
||||
<Tabs defaultValue="General">
|
||||
<TabsList>
|
||||
{tabs.map((tab) => (
|
||||
<TabsTrigger
|
||||
key={tab.name}
|
||||
value={tab.name}
|
||||
disabled={!tab.content}
|
||||
disabled={tab.disabled}
|
||||
>
|
||||
{tab.name}
|
||||
</TabsTrigger>
|
||||
@@ -48,8 +54,8 @@ export function Component() {
|
||||
</TabsList>
|
||||
{tabs.map((tab) => (
|
||||
<TabsContent key={tab.name} value={tab.name} className="h-96 mt-8">
|
||||
{tab.content || <span className="text-zinc-500">Comming Soon</span>}
|
||||
{tab.content && <Recommendations type={tab.name} />}
|
||||
<SubscribeForm type={tab.value} />
|
||||
<Recommendations type={tab.value} />
|
||||
</TabsContent>
|
||||
))}
|
||||
</Tabs>
|
||||
|
||||
Reference in New Issue
Block a user