refactor(discover): remove unused constants and styles

- Deleted the constants file for RSSHubCategories as it is no longer needed.
- Removed the recommendations.module.css file to clean up unused styles.
- Updated recommendations and recommendations-card components to utilize the new import path for constants.
- Enhanced the layout and styling of the Recommendations component for better user experience.

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-04-27 22:47:43 +08:00
parent d35639f57a
commit 764fc0ecf9
6 changed files with 155 additions and 149 deletions
@@ -1,25 +0,0 @@
export const RSSHubCategories = [
"all",
"social-media",
"new-media",
"traditional-media",
"bbs",
"blog",
"programming",
"design",
"live",
"multimedia",
"picture",
"anime",
"program-update",
"university",
"forecast",
"travel",
"shopping",
"game",
"reading",
"government",
"study",
"journal",
"finance",
] as const
@@ -1,4 +1,5 @@
import { Card, CardContent, CardHeader, CardTitle } from "@follow/components/ui/card/index.jsx"
import { RSSHubCategories } from "@follow/constants"
import { getDominantColor } from "@follow/utils/color"
import { clsx, cn, getUrlIcon } from "@follow/utils/utils"
import { upperFirst } from "es-toolkit/compat"
@@ -9,9 +10,7 @@ import { useTranslation } from "react-i18next"
import { useModalStack } from "~/components/ui/modal/stacked/hooks"
import { FeedIcon } from "~/modules/feed/feed-icon"
import { RSSHubCategories } from "./constants"
import { RecommendationContent } from "./recommendation-content"
import styles from "./recommendations.module.css"
import type { RSSHubRouteDeclaration } from "./types"
interface RecommendationCardProps {
@@ -44,7 +43,7 @@ export const RecommendationCard: FC<RecommendationCardProps> = memo(
}, [data])
return (
<Card className={styles["recommendations-card"]}>
<Card className="shadow-background border-border flex flex-col overflow-hidden rounded-lg border">
<CardHeader className="@container relative p-5 pb-3">
<div className="@[280px]:h-[80px] absolute left-0 top-0 h-[50px] w-full overflow-hidden dark:brightness-75">
<span className="pointer-events-none opacity-50">
@@ -62,14 +61,14 @@ export const RecommendationCard: FC<RecommendationCardProps> = memo(
href={`https://${data.url}`}
target="_blank"
rel="noreferrer"
className="ml-2 translate-y-1.5"
className="text-title2 ml-2 line-clamp-1 translate-y-1.5"
>
{data.name}
</a>
</CardTitle>
</CardHeader>
<CardContent className="flex grow flex-col p-5 pt-0">
<ul className="text-text grow columns-2 gap-2 space-y-1 text-sm">
<ul className="text-text grow gap-2 space-y-1 text-sm">
{Object.keys(data.routes).map((route) => {
const routeData = data.routes[route]!
// some routes have multiple paths, like `huxiu`
@@ -87,7 +86,7 @@ export const RecommendationCard: FC<RecommendationCardProps> = memo(
>
<button
type="button"
className="before:bg-accent group-hover:bg-material-medium relative rounded p-0.5 px-1 text-left duration-200 before:absolute before:inset-y-0 before:-left-2 before:mb-auto before:mt-2 before:size-1.5 before:rounded-full before:content-['']"
className="before:bg-accent text-headline group-hover:bg-material-medium relative ml-3 rounded p-0.5 px-1 text-left duration-200 before:absolute before:inset-y-0 before:-left-3 before:mb-auto before:mt-2 before:size-1.5 before:rounded-full before:content-['']"
onClick={() => {
present({
id: `recommendation-content-${route}`,
@@ -115,7 +114,7 @@ export const RecommendationCard: FC<RecommendationCardProps> = memo(
<div className="flex flex-1 items-center text-sm">
<i className="i-mingcute-hammer-line mr-1 shrink-0 translate-y-0.5 self-start" />
<span className="flex flex-wrap gap-1">
<span className="text-body flex flex-wrap gap-1">
{maintainers.map((m) => (
<a
key={m}
@@ -130,8 +129,8 @@ export const RecommendationCard: FC<RecommendationCardProps> = memo(
</span>
</div>
<div className="flex flex-1 items-center text-sm">
<i className="i-mingcute-tag-2-line mr-1 shrink-0 translate-y-1 self-start" />
<span className="flex flex-wrap gap-1">
<i className="i-mingcute-tag-2-line mr-1 shrink-0 translate-y-0.5 self-start" />
<span className="text-body flex flex-wrap gap-1">
{categories.map((c) => (
<button
onClick={() => {
@@ -1,24 +0,0 @@
.recommendations-grid {
--rows: 4;
--max-item-width: 280px;
--gap: 1em;
display: grid;
grid-template-columns: repeat(
auto-fill,
minmax(
max(var(--max-item-width), calc((100% - var(--gap) * (var(--rows) - 1)) / var(--rows))),
1fr
)
);
gap: 1em;
}
.recommendations-card {
border-radius: 8px;
box-shadow: 0 0 1px 1px rgba(0, 0, 0, 0.06) inset;
border: 0;
overflow: hidden;
display: flex;
flex-direction: column;
}
@@ -1,21 +1,25 @@
import { isMobile } from "@follow/components/hooks/useMobile.js"
import { ActionButton } from "@follow/components/ui/button/action-button.js"
import { Card, CardContent } from "@follow/components/ui/card/index.jsx"
import { Divider } from "@follow/components/ui/divider/Divider.js"
import { LoadingCircle } from "@follow/components/ui/loading/index.js"
import { ScrollArea } from "@follow/components/ui/scroll-area/index.js"
import { Masonry } from "@follow/components/ui/masonry/index.js"
import { ScrollArea } from "@follow/components/ui/scroll-area/ScrollArea.js"
import { ResponsiveSelect } from "@follow/components/ui/select/responsive.js"
import { Tabs, TabsList, TabsTrigger } from "@follow/components/ui/tabs/index.js"
import { cn, isASCII } from "@follow/utils/utils"
import { CategoryMap, RSSHubCategories } from "@follow/constants"
import { isASCII } from "@follow/utils/utils"
import { keepPreviousData } from "@tanstack/react-query"
import { useMemo, useRef, useState } from "react"
import { useMemo, useState } from "react"
import { flushSync } from "react-dom"
import { useTranslation } from "react-i18next"
import { useEventCallback } from "usehooks-ts"
import { useGeneralSettingKey } from "~/atoms/settings/general"
import { DrawerModalLayout } from "~/components/ui/modal/stacked/custom-modal"
import { useCurrentModal, useModalStack } from "~/components/ui/modal/stacked/hooks"
import { useAuthQuery } from "~/hooks/common"
import { Queries } from "~/queries"
import { TrendingButton } from "../trending"
import { RSSHubCategories } from "./constants"
import styles from "./recommendations.module.css"
import { RecommendationCard } from "./recommendations-card"
const LanguageOptions = [
@@ -44,17 +48,10 @@ const fetchRsshubPopular = (category: DiscoverCategories, lang: Language) => {
})
}
let firstLoad = true
export function Recommendations({
hideTitle,
className,
headerClassName,
}: {
hideTitle?: boolean
className?: string
headerClassName?: string
}) {
export function Recommendations() {
const { t } = useTranslation()
const lang = useGeneralSettingKey("language")
const { present } = useModalStack()
const defaultLang = !lang || ["zh-CN", "zh-HK", "zh-TW"].includes(lang) ? "all" : "en"
const [category, setCategory] = useState<DiscoverCategories>("all")
@@ -111,25 +108,24 @@ export function Recommendations({
rsshubPopular.refetch()
}
const tabRef = useRef<HTMLDivElement>(null)
const handleChangeCategoryAndJumpTo = useEventCallback((category: DiscoverCategories) => {
handleCategoryChange(category)
const handleShowCategoryContent = (selectedCategory: DiscoverCategories) => {
present({
id: `category-content-${selectedCategory}`,
CustomModalComponent: !isMobile() ? DrawerModalLayout : undefined,
if (tabRef.current) {
const tab = tabRef.current.querySelector(`[data-value="${category}"]`)
if (tab) {
const tabRect = tab.getBoundingClientRect()
const containerRect = tabRef.current.getBoundingClientRect()
const scrollLeft =
tabRect.left -
containerRect.left +
tabRef.current.scrollLeft -
(containerRect.width - tabRect.width) / 2
tabRef.current.scrollTo({ left: scrollLeft, behavior: "smooth" })
}
}
})
content: () => {
return (
<RecommendationDrawerContent
keys={keys}
data={data}
selectedCategory={selectedCategory}
handleCategoryChange={handleCategoryChange}
/>
)
},
title: t(`discover.category.${selectedCategory}` as any, { ns: "common" }),
})
}
if (isLoading) {
return null
@@ -140,68 +136,128 @@ export function Recommendations({
}
return (
<div className={cn(!hideTitle && "mt-8 w-full max-w-[1200px]")}>
{!hideTitle && (
<div className="center relative flex text-lg font-bold">
<span>{t("discover.popular")}</span>
<div className="absolute bottom-0 right-0">
<TrendingButton language={selectedLang} />
</div>
{isFetching && (
<div className="pointer-events-none absolute inset-x-0 top-0 flex items-center justify-center gap-8">
<span className="opacity-0" aria-hidden>
{t("discover.popular")}
</span>
<LoadingCircle size="small" className="center flex" />
</div>
)}
<div className="mt-12 w-full max-w-[1200px]">
<div className="center relative flex">
<div className="absolute bottom-0 right-0 flex items-center gap-2">
<span className="text-headline text-text font-medium">Preferred language:</span>
<ResponsiveSelect
value={selectedLang}
onValueChange={handleLangChange}
triggerClassName="w-32 h-8 rounded"
size="sm"
items={LanguageOptions as any}
/>
<TrendingButton language={selectedLang} />
</div>
)}
{isFetching && (
<div className="pointer-events-none absolute inset-x-0 top-0 flex items-center justify-center gap-8">
<span className="opacity-0" aria-hidden>
{t("discover.popular")}
</span>
<div
className={cn(
"bg-background z-[9] my-3 flex w-full flex-col items-end gap-4 sm:flex-row sm:items-center",
headerClassName,
<LoadingCircle size="small" className="center flex" />
</div>
)}
>
<ScrollArea.ScrollArea
ref={tabRef}
orientation="horizontal"
rootClassName="grow min-w-0 max-w-full overflow-visible"
scrollbarClassName="-mb-3 z-[100]"
>
<Tabs value={category} onValueChange={handleCategoryChange}>
<TabsList>
{RSSHubCategories.map((category) => (
<TabsTrigger data-value={category} key={category} value={category}>
{t(`discover.category.${category}`, { ns: "common" })}
</TabsTrigger>
))}
</TabsList>
</Tabs>
</ScrollArea.ScrollArea>
<ResponsiveSelect
value={selectedLang}
onValueChange={handleLangChange}
triggerClassName="w-32"
size="sm"
items={LanguageOptions as any}
/>
</div>
<div className={cn(styles["recommendations-grid"], "mt-6", className)}>
{keys.map((key) => (
<RecommendationCard
key={key}
data={data[key]}
routePrefix={key}
setCategory={handleChangeCategoryAndJumpTo}
/>
<div className="mt-4 grid grid-cols-2 gap-4 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6">
{RSSHubCategories.map((cat) => (
<Card
key={cat}
className="cursor-pointer transition-all duration-200 hover:scale-[102%] hover:shadow-lg"
style={{
backgroundImage: `linear-gradient(-135deg, ${CategoryMap[cat]?.color}80, ${CategoryMap[cat]?.color})`,
}}
onClick={() => handleShowCategoryContent(cat)}
>
<CardContent className="flex min-h-[100px] flex-col items-center justify-center p-4 text-center">
<div className="mb-2 text-2xl">{CategoryMap[cat]?.emoji}</div>
<div className="font-medium text-white">
{t(`discover.category.${cat}`, { ns: "common" })}
</div>
</CardContent>
</Card>
))}
</div>
</div>
)
}
const RecommendationDrawerContent = ({
keys,
data,
selectedCategory,
handleCategoryChange,
}: {
keys: string[]
data: any
selectedCategory: string
handleCategoryChange: (category: string) => void
}) => {
const { t } = useTranslation()
const filteredItems = keys
.map((key) => {
if (selectedCategory !== "all") {
const categories = new Set<string>()
for (const route in data![key].routes) {
const routeData = data![key].routes[route]!
if (routeData.categories) {
routeData.categories.forEach((c) => categories.add(c))
}
}
if (!categories.has(selectedCategory as string)) {
return null
}
}
return {
key,
data: data![key],
routePrefix: key,
}
})
.filter(Boolean)
const { dismiss } = useCurrentModal()
return (
<div className="absolute inset-0 flex flex-col">
<h1 className="text-title2 mt-4 pl-8 font-semibold">
{t(`discover.category.${selectedCategory}` as any, { ns: "common" })}
</h1>
{/* Close */}
<ActionButton
className="absolute right-4 top-4"
onClick={dismiss}
tooltip={t("words.close", { ns: "common" })}
>
<i className="i-mgc-close-cute-re" />
</ActionButton>
<Divider className="m-4" />
<ScrollArea rootClassName="w-full" viewportClassName="px-8">
<div className="w-full">
<Masonry
items={filteredItems}
columnGutter={16}
columnWidth={280}
columnCount={isMobile() ? 1 : 2}
overscanBy={2}
render={({ data: itemData }) => {
if (!itemData) return null
return (
<RecommendationCard
key={itemData.key}
data={itemData.data}
routePrefix={itemData.routePrefix}
setCategory={handleCategoryChange}
/>
)
}}
/>
</div>
</ScrollArea>
</div>
)
}
@@ -39,7 +39,7 @@ export function RSSHubGuide({ categories, lang }: { categories?: string; lang?:
rootClassName="overflow-visible"
>
<div className="space-y-3">
<Recommendations hideTitle headerClassName="sticky top-0" />
<Recommendations />
</div>
</ScrollArea.ScrollArea>
</AppErrorBoundary>
@@ -42,7 +42,7 @@ export const TrendingButton = ({ language, className }: TrendingProps & { classN
CustomModalComponent: !isMobile() ? DrawerModalLayout : undefined,
})
}}
buttonClassName={cn("px-2", className)}
buttonClassName={cn("rounded px-2 h-8", className)}
>
<i className="i-mgc-trending-up-cute-re mr-1" />
{t("words.trending")}