news/app/store/newsStore.ts

184 lines
5.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { create } from "zustand";
import type { IconType } from 'react-icons';
import {
FaUserGraduate,
FaUserShield,
FaLaptopCode,
FaMicrophoneLines,
FaEnvelopeOpenText,
FaWrench,
FaRegFileLines,
FaSpellCheck,
FaChartPie,
FaGlobe,
FaBookOpenReader,
FaPenRuler,
} from 'react-icons/fa6';
import { persist, createJSONStorage } from 'zustand/middleware'
//文化符号徽章
interface Logo {
id: number;
src: string;
alt: string;
}
//新闻列表
interface NewsItem {
id: string;
content: string;
url: string;
}
//综合服务徽章
interface ServiceItem {
icon: IconType; // 图标组件
label: string; // 显示标签
href: string; // 链接地址
};
//导航栏项
export interface MenuItem {
label: string;
key: string;
sectionId:string;
}
type NewsStore = {
logos: Logo[];
newsList: NewsItem[];
booksList: NewsItem[];
services: ServiceItem[];
menuItems: MenuItem[];
// 添加 setter 方法
setLogos: (logos: Logo[]) => void;
setNewsList: (news: NewsItem[]) => void;
setBooksList: (books: NewsItem[]) => void;
setServices: (services: ServiceItem[]) => void;
setMenuItems: (items: MenuItem[]) => void;
};
// 初始数据
const initialData = {
logos: [
{ id: 1, src: "/public/logo/1.png", alt: "Logo 1" },
{ id: 2, src: "/public/logo/1.png", alt: "Logo 2" },
{ id: 3, src: "/public/logo/1.png", alt: "Logo 3" },
{ id: 4, src: "/public/logo/1.png", alt: "Logo 4" },
{ id: 5, src: "/public/logo/1.png", alt: "Logo 5" },
{ id: 6, src: "/public/logo/1.png", alt: "Logo 6" },
{ id: 7, src: "/public/logo/1.png", alt: "Logo 7" },
{ id: 8, src: "/public/logo/1.png", alt: "Logo 8" },
{ id: 9, src: "/public/logo/1.png", alt: "Logo 9" },
{ id: 10, src: "/public/logo/1.png", alt: "Logo 10" },
{ id: 11, src: "/public/logo/1.png", alt: "Logo 11" },
{ id: 12, src: "/public/logo/1.png", alt: "Logo 12" },
{ id: 13, src: "/public/logo/1.png", alt: "Logo 13" },
{ id: 14, src: "/public/logo/1.png", alt: "Logo 14" },
{ id: 15, src: "/public/logo/1.png", alt: "Logo 15" },
{ id: 16, src: "/public/logo/1.png", alt: "Logo 16" },
], //文化符号徽章
newsList: [
{ id: "1", content: "中华人民共和国监察法", url: "https://www.baidu.com" },
{
id: "2",
content: "2024年国办印发意见部门工作人员党听全国两...",
url: "https://www.baidu.com",
},
{
id: "3",
content: "十四届全国人大二次会议闭幕贺词",
url: "https://www.baidu.com",
},
{
id: "4",
content: '7天人代会"小片段"折射民主"大全景"',
url: "https://www.baidu.com",
},
{
id: "5",
content: "全国政协十四届二次会议共收到提案5800多件",
url: "https://www.baidu.com",
},
{
id: "6",
content: "两会观察丨从两会八个高频词看中国",
url: "https://www.baidu.com",
},
{
id: "7",
content: '两会"清单"上新这些民生发展温度',
url: "https://www.baidu.com",
},
{
id: "8",
content: '"选择中国"——世界从中国两会读出心动机号',
url: "https://www.baidu.com",
},
{
id: "9",
content: "中国经济信心说丨新玛合信心从哪里来",
url: "https://www.baidu.com",
},
],
booksList: [
{
id: "1",
content: "《中国特色社会主义》",
url: "https://www.baidu.com",
},
{
id: "2",
content: "《中国特色社会主义》",
url: "https://www.baidu.com",
},
{
id: "3",
content: "《中国特色社会主义》",
url: "https://www.baidu.com",
},
],
services:[
{ icon: FaUserGraduate, label: '警队自考', href: '/study' },
{ icon: FaUserShield, label: '警队教育', href: '/scholarship' },
{ icon: FaLaptopCode, label: '常用软件', href: '/software' },
{ icon: FaMicrophoneLines, label: '智能语音', href: '/voice' },
{ icon: FaEnvelopeOpenText, label: '蓝天邮局', href: '/mail' },
{ icon: FaWrench, label: '策划工具', href: '/plan' },
{ icon: FaRegFileLines, label: '办公模板', href: '/office' },
{ icon: FaSpellCheck, label: '智能校对', href: '/ai-check' },
{ icon: FaChartPie, label: '警情调研', href: '/survey' },
{ icon: FaGlobe, label: '上网助手', href: '/net' },
{ icon: FaBookOpenReader, label: '考试平台', href: '/exam' },
{ icon: FaPenRuler, label: '学习平台', href: '/study' },
],
menuItems: [
{ label: '首页', key: 'home', sectionId: 'home' },
{ label: '烽火动态', key: 'news', sectionId: 'fireNews' },
{ label: '烽火铸魂', key: 'soul', sectionId: 'soul' },
{ label: '烽火训练', key: 'training', sectionId: 'training' },
{ label: '联系热线', key: 'hotline', sectionId: 'hotline' },
{ label: '综合服务', key: 'service', sectionId: 'service' },
],
};
export const useNewsStore = create<NewsStore>()(persist((set) => ({
...initialData,
// Setter 方法实现
setLogos: (logos) => set({ logos }),
setNewsList: (newsList) => set({ newsList }),
setBooksList: (booksList) => set({ booksList }),
setServices: (services) => set({ services }),
setMenuItems: (menuItems) => set({ menuItems }),
}), {
name: 'news-store',
storage: createJSONStorage(() => localStorage),
}));
// 初始化时强制写入 localStorage仅在首次加载时
if (typeof window !== 'undefined') {
const stored = localStorage.getItem('news-store');
if (!stored) {
// 如果 localStorage 中没有数据,手动触发一次保存
useNewsStore.setState(initialData);
}
}