Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
feat(热门内容): 热门内容添加日期选择
gaoSira716bf165412
diff --git a/src/api/hot.ts b/src/api/hot.tsindex 30dfec46..e23ec576 100644--- a/src/api/hot.ts+++ b/src/api/hot.ts@@ -64,6 +64,16 @@ export interface PlatformRanking {items?: RankingItem[];}+// 榜单日期+export interface RankingDate {+ originalQueryDate: string;+ queryDate: string;+ rankingId: string;+ showDate: string;+ spiderTime: string;+ value: string;+}+export interface Author {id: string;name: string;@@ -181,7 +191,7 @@ export const platformApi = {// 获取榜单日期getRankingDates(rankingId: string) {- return request<string[]>({+ return request<RankingDate[]>({url: `hotdata/ranking/hotinfo/${rankingId}/dates`,method: "GET",});diff --git a/src/app/[lng]/hotContent/components/HotContent/hotContent.module.scss b/src/app/[lng]/hotContent/components/HotContent/hotContent.module.scssnew file mode 100644index 00000000..e36a3cba--- /dev/null+++ b/src/app/[lng]/hotContent/components/HotContent/hotContent.module.scss@@ -0,0 +1,26 @@+.hotContent {+ :global {+ .hotContent-options {+ margin-top: 15px;+ &-select {+ display: flex;+ align-items: center;+ height: 32px;+ margin-right: 30px;+ &-label {+ height: 100%;+ display: flex;+ align-items: center;+ padding: 0 10px;+ border-radius: 3px 0 0 3px;+ background: var(--colorPrimary1);+ color: var(--colorPrimary5);+ }+ .ant-select-selector {+ border-bottom-left-radius: 0;+ border-top-left-radius: 0;+ }+ }+ }+ }+}\ No newline at end of filediff --git a/src/app/[lng]/hotContent/components/HotContent/index.tsx b/src/app/[lng]/hotContent/components/HotContent/index.tsxindex 7dc3a420..9e4ca1a1 100644--- a/src/app/[lng]/hotContent/components/HotContent/index.tsx+++ b/src/app/[lng]/hotContent/components/HotContent/index.tsx@@ -2,14 +2,15 @@ import {ForwardedRef,forwardRef,memo,+ useEffect,useMemo,- useRef,useState,} from "react";import { useHotContent } from "@/app/[lng]/hotContent/useHotContent";import { useShallow } from "zustand/react/shallow";import HotContentLabel from "@/app/[lng]/hotContent/components/HotContentLabel";-import { PlatformRanking } from "@/api/hot";+import styles from "./hotContent.module.scss";+import { Select } from "antd";export interface IHotContentRef {}@@ -25,43 +26,59 @@ const HotContent = memo(twoMenuKey: state.twoMenuKey,})),);- const lastSelectedLabelInfo = useRef<{- ranking?: PlatformRanking;- labelData?: string[];- datesData?: string[];- }>();// 当前选中的标签const [labelValue, setLabelValue] = useState("全部");+ // 当前选中的日期+ const [dateValue, setDateValue] = useState("");// 获取当前选中的标签、日期、榜单const selectedLabelInfo = useMemo(() => {const platId = twoMenuKey.split("_")[1];- const ranking = rankingData[platId] ?? {};+ return {+ ranking: rankingData[platId] ?? {},+ labelData: labelData[platId] || [],+ datesData: datesData[platId] || [],+ };+ }, [labelData, rankingData, datesData, twoMenuKey]);- // 判断 ranking 是否为空对象- const isEmptyRanking = !ranking || Object.keys(ranking).length === 0;+ useEffect(() => {+ setLabelValue("全部");+ }, [twoMenuKey]);- if (isEmptyRanking && lastSelectedLabelInfo.current) {- return lastSelectedLabelInfo.current;+ // 当平台切换时,重置日期为第一个+ useEffect(() => {+ if (selectedLabelInfo.datesData.length > 0) {+ setDateValue(selectedLabelInfo.datesData[0].value);}+ }, [selectedLabelInfo]);- const value = {- ranking,- labelData: labelData[ranking?.type] || [],- datesData: datesData[platId] || [],- };-- lastSelectedLabelInfo.current = value;- return value;- }, [labelData, rankingData, datesData, twoMenuKey]);+ // 请求列表数据+ useEffect(() => {+ if (dateValue === "") return;+ console.log("请求列表数据", { labelValue, dateValue });+ }, [labelValue, dateValue]);return (- <div>+ <div className={styles.hotContent}><HotContentLabellabels={["全部", ...(selectedLabelInfo.labelData ?? [])]}value={labelValue}onChange={(value) => setLabelValue(value)}/>++ <div className="hotContent-options">+ <div className="hotContent-options-select">+ <div className="hotContent-options-select-label">日榜</div>+ <Select+ options={selectedLabelInfo.datesData.map((v) => ({+ label: v.showDate,+ value: v.value,+ }))}+ value={dateValue}+ onChange={(e) => setDateValue(e)}+ ></Select>+ </div>+ </div></div>);}),diff --git a/src/app/[lng]/hotContent/useHotContent.ts b/src/app/[lng]/hotContent/useHotContent.tsindex 6d5ef028..bc17c279 100644--- a/src/app/[lng]/hotContent/useHotContent.ts+++ b/src/app/[lng]/hotContent/useHotContent.ts@@ -1,7 +1,7 @@import { create } from "zustand";import { combine } from "zustand/middleware";import lodash from "lodash";-import { Platform, platformApi, PlatformRanking } from "@/api/hot";+import { Platform, platformApi, PlatformRanking, RankingDate } from "@/api/hot";import { HotType } from "@/app/[lng]/hotContent/hotContent.enum";export interface IHotContentStore {@@ -27,7 +27,7 @@ export interface IHotContentStore {};// 日期数据 key=平台ID,value=日期数据datesData: {- [key: string]: string[];+ [key: string]: RankingDate[];};}@@ -120,14 +120,14 @@ export const useHotContent = create(const rankingItem = get().rankingData[platformId];// 获取标签和日期数据- if (!get().labelData[rankingItem.type]) {+ if (!get().labelData[platformId]) {await Promise.all([platformApi.getRankingLabel(rankingItem.id),platformApi.getRankingDates(rankingItem.id),]).then(([labelRes, dateRes]) => {const newLabelData = {...get().labelData,- [rankingItem.type]: labelRes!.data,+ [platformId]: labelRes!.data,};const newDatesData = {...get().datesData,