Files
Folo/apps/renderer/src/modules/discover/DiscoverFeedForm.tsx
T
edd4f9e5a6 feat: mobile design (#1568)
* init

Signed-off-by: Innei <i@innei.in>

* feat: feed column

Signed-off-by: Innei <i@innei.in>

* feat: float bar

Signed-off-by: Innei <tukon479@gmail.com>

* feat: profile button

Signed-off-by: Innei <tukon479@gmail.com>

* chore: comment

Signed-off-by: Innei <tukon479@gmail.com>

* feat: layout

Signed-off-by: Innei <tukon479@gmail.com>

* feat: split mobile entry content

Signed-off-by: Innei <tukon479@gmail.com>

* feat: add history stack

Signed-off-by: Innei <tukon479@gmail.com>

* feat: return back

Signed-off-by: Innei <tukon479@gmail.com>

* fix: return back position

Signed-off-by: Innei <tukon479@gmail.com>

* feat: entry list header

Signed-off-by: Innei <tukon479@gmail.com>

* feat: sync selector

Signed-off-by: Innei <tukon479@gmail.com>

* feat: modal stack mobile

Signed-off-by: Innei <tukon479@gmail.com>

* fix: modal selector

Signed-off-by: Innei <tukon479@gmail.com>

* cleanup

Signed-off-by: Innei <tukon479@gmail.com>

* fix: sheet and optimize entry readers

Signed-off-by: Innei <tukon479@gmail.com>

* fix: scroller

Signed-off-by: Innei <tukon479@gmail.com>

* feat: entry contents

Signed-off-by: Innei <tukon479@gmail.com>

* feat: entry content

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* feat: user profile sheet

Signed-off-by: Innei <tukon479@gmail.com>

* fix: entry content header

Signed-off-by: Innei <tukon479@gmail.com>

* fix: entry header

Signed-off-by: Innei <tukon479@gmail.com>

* chore: auto-fix linting and formatting issues

* fix: return back

Signed-off-by: Innei <tukon479@gmail.com>

* feat: image view

Signed-off-by: Innei <tukon479@gmail.com>

* feat: audio

Signed-off-by: Innei <tukon479@gmail.com>

* feat: inline player

Signed-off-by: Innei <tukon479@gmail.com>

* feat: pull to refresh

Signed-off-by: Innei <tukon479@gmail.com>

* cleanup

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* chore: auto-fix linting and formatting issues

* chore: fix import, remove no support display

* chore: auto-fix linting and formatting issues

* feat: achievement modal in mobile

Signed-off-by: Innei <tukon479@gmail.com>

* fix: subview mobile

Signed-off-by: Innei <tukon479@gmail.com>

* feat: power

Signed-off-by: Innei <tukon479@gmail.com>

* feat: wallet mobile design

Signed-off-by: Innei <tukon479@gmail.com>

* feat: pwa support (#1575)

Co-authored-by: hyoban <hyoban@users.noreply.github.com>

* chore: auto-fix linting and formatting issues

* fix import

* discover

* fix: border color

Signed-off-by: Innei <tukon479@gmail.com>

* fix: modal async

Signed-off-by: Innei <tukon479@gmail.com>

* feat: mobile setting

Signed-off-by: Innei <tukon479@gmail.com>

* fix: entry header width

Signed-off-by: Innei <tukon479@gmail.com>

* fix: remove unused hide filter

* feat: settings

Signed-off-by: Innei <tukon479@gmail.com>

* feat: setting done

Signed-off-by: Innei <tukon479@gmail.com>

* action card

* fix discover feed form

* popular filter

* inbox table

* fix: picture masonry pull to refresh

Signed-off-by: Innei <tukon479@gmail.com>

* fix: scroll element

Signed-off-by: Innei <tukon479@gmail.com>

* fix: radio in mobile

Signed-off-by: Innei <tukon479@gmail.com>

* feat: to scientific notation

Signed-off-by: Innei <tukon479@gmail.com>

* fix: login

Signed-off-by: Innei <tukon479@gmail.com>

* ResponsiveSelect for action card

* chore: remove theme color

Signed-off-by: Innei <tukon479@gmail.com>

* type check

* type check

* fix: setting import circular

Signed-off-by: Innei <tukon479@gmail.com>

* refactor: remove hijack pushState

Signed-off-by: Innei <tukon479@gmail.com>

* fix: float bar

Signed-off-by: Innei <tukon479@gmail.com>

* fix: scroll end trigger

Signed-off-by: Innei <tukon479@gmail.com>

* fix: dark theme color

Signed-off-by: Innei <tukon479@gmail.com>

* fix: overscroll-behavior only in destop

Signed-off-by: Innei <tukon479@gmail.com>

* fix: ux

Signed-off-by: Innei <tukon479@gmail.com>

* fix: color

Signed-off-by: Innei <tukon479@gmail.com>

* fix: list scroll area

Signed-off-by: Innei <tukon479@gmail.com>

* update

Signed-off-by: Innei <tukon479@gmail.com>

* merge

* update

* update favicon link

* fix: reload prompt on mobile

* test

Signed-off-by: Innei <i@innei.in>

* test

Signed-off-by: Innei <i@innei.in>

* fix: ios

Signed-off-by: Innei <i@innei.in>

* try patch precaching

* try

* try

* build

* try

* try

* feat: ctx menu on safari

Signed-off-by: Innei <tukon479@gmail.com>

* try

* clear timeout

Signed-off-by: Innei <tukon479@gmail.com>

* try

* fix

* deny og

* fix patch

* pre cache exclude

* feat: add all

Signed-off-by: Innei <tukon479@gmail.com>

* i18n

Signed-off-by: Innei <tukon479@gmail.com>

* feat: slide

Signed-off-by: Innei <tukon479@gmail.com>

* feat: ico

Signed-off-by: Innei <tukon479@gmail.com>

* fix: build

Signed-off-by: Innei <tukon479@gmail.com>

* test network first

Signed-off-by: Innei <tukon479@gmail.com>

* test

Signed-off-by: Innei <tukon479@gmail.com>

* fix: icon padding

* test

Signed-off-by: Innei <tukon479@gmail.com>

* use react-ios-pwa-prompt

* fix: icon path

* update

* fix: prompt

Signed-off-by: Innei <tukon479@gmail.com>

* feat: redesign toc

Signed-off-by: Innei <tukon479@gmail.com>

* ctx

Signed-off-by: Innei <tukon479@gmail.com>

* ready to prod

Signed-off-by: Innei <tukon479@gmail.com>

* feat: startup screen

Signed-off-by: Innei <tukon479@gmail.com>

* feat: floatbar

Signed-off-by: Innei <tukon479@gmail.com>

* fix: switch view

Signed-off-by: Innei <tukon479@gmail.com>

* fix: keep scroll map

Signed-off-by: Innei <tukon479@gmail.com>

* enable hoverOnlyWhenSupported

* fix: count

Signed-off-by: Innei <tukon479@gmail.com>

* fix: hoverOnlyWhenSupported only in web

Signed-off-by: Innei <tukon479@gmail.com>

* fix

Signed-off-by: Innei <tukon479@gmail.com>

* chore: auto-fix linting and formatting issues

---------

Signed-off-by: Innei <i@innei.in>
Signed-off-by: Innei <tukon479@gmail.com>
Co-authored-by: Innei <Innei@users.noreply.github.com>
Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com>
Co-authored-by: hyoban <hyoban@users.noreply.github.com>
Co-authored-by: lawvs <18554747+lawvs@users.noreply.github.com>
Co-authored-by: lawvs <lawvs@users.noreply.github.com>
2024-11-22 19:32:39 +08:00

373 lines
12 KiB
TypeScript

import { Button } from "@follow/components/ui/button/index.js"
import { Form, FormItem, FormLabel } from "@follow/components/ui/form/index.jsx"
import { Input } from "@follow/components/ui/input/index.js"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@follow/components/ui/select/index.jsx"
import type { FeedViewType } from "@follow/constants"
import { nextFrame } from "@follow/utils/dom"
import { cn } from "@follow/utils/utils"
import { zodResolver } from "@hookform/resolvers/zod"
import { omit } from "es-toolkit/compat"
import type { FC } from "react"
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef } from "react"
import type { UseFormReturn } from "react-hook-form"
import { useForm } from "react-hook-form"
import { Trans, useTranslation } from "react-i18next"
import { toast } from "sonner"
import { z } from "zod"
import { getSidebarActiveView } from "~/atoms/sidebar"
import { CopyButton } from "~/components/ui/code-highlighter"
import { Markdown } from "~/components/ui/markdown/Markdown"
import { useCurrentModal, useIsTopModal, useModalStack } from "~/components/ui/modal/stacked/hooks"
import {
MissingOptionalParamError,
parseFullPathParams,
parseRegexpPathParams,
regexpPathToPath,
} from "~/lib/path-parser"
import { getViewFromRoute } from "~/lib/utils"
import { FeedForm } from "./feed-form"
import type { RSSHubRoute } from "./types"
import { normalizeRSSHubParameters } from "./utils"
const FeedMaintainers = ({ maintainers }: { maintainers?: string[] }) => {
if (!maintainers || maintainers.length === 0) {
return null
}
return (
<div className="mb-2 flex flex-col gap-x-1 text-sm text-theme-foreground/80">
<Trans
i18nKey="discover.feed_maintainers"
components={{
maintainers: (
<span className="inline-flex flex-wrap items-center gap-2">
{maintainers.map((maintainer) => (
<a
href={`https://github.com/${maintainer}`}
key={maintainer}
target="_blank"
rel="noreferrer noopener"
className="inline-flex cursor-pointer items-center text-theme-foreground/50 duration-200 hover:text-accent"
>
@{maintainer}
<i className="i-mgc-external-link-cute-re ml-0.5" />
</a>
))}
</span>
),
}}
/>
</div>
)
}
const FeedDescription = ({ description }: { description?: string }) => {
const { t } = useTranslation()
if (!description) {
return null
}
return (
<>
<p>{t("discover.feed_description")}</p>
<Markdown className="w-full max-w-full cursor-text select-text break-all prose-p:my-1">
{description}
</Markdown>
</>
)
}
const routeParamsKeyPrefix = "route-params-"
export type RouteParams = Record<
string,
{
description: string
default?: string
}
>
export const DiscoverFeedForm = ({
route,
routePrefix,
noDescription,
submitButtonClassName,
routeParams,
}: {
route: RSSHubRoute
routePrefix: string
noDescription?: boolean
submitButtonClassName?: string
routeParams?: RouteParams
}) => {
const { t } = useTranslation()
const keys = useMemo(
() =>
parseRegexpPathParams(route.path, {
excludeNames: [
"routeParams",
"functionalFlag",
"fulltext",
"disableEmbed",
"date",
"language",
"lang",
"sort",
],
forceExcludeNames: routeParams ? ["routeParams"] : [],
}),
[route.path, routeParams],
)
const formPlaceholder = useMemo<Record<string, string>>(() => {
if (!route.example) return {}
return parseFullPathParams(route.example.replace(`/${routePrefix}`, ""), route.path)
}, [route.example, route.path, routePrefix])
const dynamicFormSchema = useMemo(
() =>
z.object({
...Object.fromEntries(
keys
.map((keyItem) => [
keyItem.name,
keyItem.optional ? z.string().optional().nullable() : z.string().min(1),
])
.concat(
routeParams
? Object.entries(routeParams).map(([key]) => [
`${routeParamsKeyPrefix}${key}`,
z.string(),
])
: [],
),
),
}),
[keys, routeParams],
)
const defaultValue = useMemo(() => {
const ret = {}
if (!route.parameters) return ret
for (const key in route.parameters) {
const params = normalizeRSSHubParameters(route.parameters[key])
if (!params) continue
ret[key] = params.default
}
return ret
}, [route.parameters])
const form = useForm<z.infer<typeof dynamicFormSchema>>({
resolver: zodResolver(dynamicFormSchema),
defaultValues: defaultValue,
mode: "all",
}) as UseFormReturn<any>
const { present, dismissAll } = useModalStack()
const onSubmit = useCallback(
(_data: Record<string, string>) => {
const data = Object.fromEntries(
Object.entries(_data).filter(([key]) => !key.startsWith(routeParamsKeyPrefix)),
)
try {
const routeParamsPath = encodeURIComponent(
Object.entries(_data)
.filter(([key, value]) => key.startsWith(routeParamsKeyPrefix) && value)
.map(([key, value]) => [key.slice(routeParamsKeyPrefix.length), value])
.map(([key, value]) => `${key}=${value}`)
.join("&"),
)
const fillRegexpPath = regexpPathToPath(
routeParams && routeParamsPath
? route.path.slice(0, route.path.indexOf("/:routeParams"))
: route.path,
data,
)
const url = `rsshub://${routePrefix}${fillRegexpPath}`
const finalUrl = routeParams && routeParamsPath ? `${url}/${routeParamsPath}` : url
const defaultView = getViewFromRoute(route) || (getSidebarActiveView() as FeedViewType)
present({
title: "Add Feed",
content: () => (
<FeedForm
asWidget
url={finalUrl}
defaultValues={{
view: defaultView.toString(),
}}
onSuccess={dismissAll}
/>
),
})
} catch (err: unknown) {
if (err instanceof MissingOptionalParamError) {
toast.error(err.message)
const idx = keys.findIndex((item) => item.name === err.param)
form.setFocus(keys[idx === 0 ? 0 : idx - 1].name, {
shouldSelect: true,
})
}
}
},
[dismissAll, form, keys, present, route, routeParams, routePrefix],
)
const formElRef = useRef<HTMLFormElement>(null)
const isTop = useIsTopModal()
useLayoutEffect(() => {
if (!isTop) return
const $form = formElRef.current
if (!$form) return
$form.querySelectorAll("input")[0]?.focus()
}, [formElRef, isTop])
const modal = useCurrentModal()
useEffect(() => {
modal.setClickOutSideToDismiss(!form.formState.isDirty)
}, [form.formState.isDirty, modal])
return (
<Form {...form}>
{!noDescription && (
<PreviewUrl watch={form.watch} path={route.path} routePrefix={`rsshub://${routePrefix}`} />
)}
<form className="flex flex-col gap-4" onSubmit={form.handleSubmit(onSubmit)} ref={formElRef}>
{keys.map((keyItem) => {
const parameters = normalizeRSSHubParameters(route.parameters[keyItem.name])
const formRegister = form.register(keyItem.name)
return (
<FormItem key={keyItem.name} className="flex flex-col space-y-2">
<FormLabel className="capitalize">
{keyItem.name}
{!keyItem.optional && <sup className="ml-1 align-sub text-red-500">*</sup>}
</FormLabel>
{parameters?.options ? (
<Select
{...omit(formRegister, "ref")}
onValueChange={(value) => {
form.setValue(keyItem.name, value)
}}
defaultValue={parameters.default || void 0}
>
{/* Select focused ref on `SelectTrigger` or `Select` */}
<SelectTrigger ref={formRegister.ref}>
<SelectValue placeholder={t("discover.select_placeholder")} />
</SelectTrigger>
<SelectContent>
{parameters.options.map((option) => (
<SelectItem key={option.value} value={option.value || ""}>
{option.label}
{parameters.default === option.value && t("discover.default_option")}
</SelectItem>
))}
</SelectContent>
</Select>
) : (
<Input
{...formRegister}
onBlur={(e) => {
// Fix #535
nextFrame(() => {
formRegister.onBlur(e)
})
}}
placeholder={
(parameters?.default ?? formPlaceholder[keyItem.name])
? `e.g. ${formPlaceholder[keyItem.name]}`
: void 0
}
/>
)}
{!!parameters && (
<Markdown className="w-full max-w-full text-xs text-theme-foreground/50">
{parameters.description}
</Markdown>
)}
</FormItem>
)
})}
{routeParams && (
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
{Object.entries(routeParams).map(([key, value]) => (
<FormItem key={`${routeParamsKeyPrefix}${key}`} className="flex flex-col space-y-2">
<FormLabel className="capitalize">{key}</FormLabel>
<Input
{...form.register(`${routeParamsKeyPrefix}${key}`)}
placeholder={value.default}
className="grow-0"
/>
{!!value.description && (
<Markdown className="w-full max-w-full text-xs text-theme-foreground/50">
{value.description}
</Markdown>
)}
</FormItem>
))}
</div>
)}
{!noDescription && (
<>
<FeedDescription description={route.description} />
<FeedMaintainers maintainers={route.maintainers} />
</>
)}
<div
className={cn(
"sticky bottom-0 -mt-4 mb-1 flex w-full justify-end py-3",
submitButtonClassName,
)}
>
<Button type="submit">{t("discover.preview")}</Button>
</div>
</form>
</Form>
)
}
const PreviewUrl: FC<{
watch: UseFormReturn<any>["watch"]
path: string
routePrefix: string
}> = ({ watch, path, routePrefix }) => {
const data = watch()
const fullPath = useMemo(() => {
try {
return regexpPathToPath(path, data)
} catch (err: unknown) {
console.info((err as Error).message)
return path
}
}, [path, data])
const renderedPath = `${routePrefix}${fullPath}`
return (
<div className="group relative min-w-0 pb-4">
<pre className="w-full whitespace-pre-line break-words text-xs text-theme-foreground/40">
{renderedPath}
</pre>
<CopyButton
value={renderedPath}
className="absolute right-0 top-0 opacity-0 duration-200 group-hover:opacity-100"
/>
</div>
)
}