2019-12-26 15:27:52 +08:00
|
|
|
/* eslint-disable import/prefer-default-export */
|
2020-11-05 15:22:59 +08:00
|
|
|
import * as React from 'react';
|
2019-12-26 15:27:52 +08:00
|
|
|
|
2022-09-27 21:50:35 +08:00
|
|
|
export interface Author {
|
|
|
|
avatar: string;
|
|
|
|
href: string;
|
|
|
|
type: 'design' | 'develop';
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Article {
|
|
|
|
title: string;
|
|
|
|
href: string;
|
|
|
|
date: string;
|
|
|
|
type: 'design' | 'develop';
|
|
|
|
author: Author['name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Recommendation {
|
|
|
|
title?: string;
|
|
|
|
img?: string;
|
|
|
|
href?: string;
|
|
|
|
popularize?: boolean;
|
|
|
|
description?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
type SourceType = 'zhihu' | 'yuque';
|
|
|
|
export interface Extra {
|
|
|
|
title: string;
|
|
|
|
description: string;
|
|
|
|
date: string;
|
|
|
|
img: string;
|
|
|
|
source: SourceType;
|
|
|
|
href: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Icon {
|
|
|
|
name: string;
|
|
|
|
href: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Articles = {
|
|
|
|
cn: Article[];
|
|
|
|
en: Article[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export type Authors = Author[];
|
|
|
|
|
|
|
|
export type Recommendations = {
|
|
|
|
cn: Recommendation[];
|
|
|
|
en: Recommendation[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export type Extras = {
|
|
|
|
cn: Extra[];
|
|
|
|
en: Extra[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export type Icons = Icon[];
|
|
|
|
|
|
|
|
export type SiteData = {
|
|
|
|
articles: Articles;
|
|
|
|
authors: Authors;
|
|
|
|
recommendations: Recommendations;
|
|
|
|
extras: Extras;
|
|
|
|
icons: Icons;
|
|
|
|
};
|
|
|
|
|
2019-12-26 15:27:52 +08:00
|
|
|
export function preLoad(list: string[]) {
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
// 图处预加载;
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.style.display = 'none';
|
|
|
|
document.body.appendChild(div);
|
2022-11-19 13:47:33 +08:00
|
|
|
list.forEach((src) => {
|
2019-12-26 15:27:52 +08:00
|
|
|
const img = new Image();
|
|
|
|
img.src = src;
|
|
|
|
div.appendChild(img);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-11-05 15:22:59 +08:00
|
|
|
|
2022-09-27 21:50:35 +08:00
|
|
|
export function useSiteData(): [Partial<SiteData>, boolean] {
|
|
|
|
const [data, setData] = React.useState<Partial<SiteData>>({});
|
2021-05-29 23:59:30 +08:00
|
|
|
const [loading, setLoading] = React.useState<boolean>(false);
|
2020-11-05 15:22:59 +08:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2022-09-27 21:50:35 +08:00
|
|
|
if (Object.keys(data ?? {}).length === 0 && typeof fetch !== 'undefined') {
|
2021-05-29 23:59:30 +08:00
|
|
|
setLoading(true);
|
2021-06-13 16:23:12 +08:00
|
|
|
fetch(`https://render.alipay.com/p/h5data/antd4-config_website-h5data.json`)
|
2022-11-19 13:47:33 +08:00
|
|
|
.then((res) => res.json())
|
|
|
|
.then((result) => {
|
2021-05-29 23:59:30 +08:00
|
|
|
setData(result);
|
|
|
|
setLoading(false);
|
2020-11-05 15:22:59 +08:00
|
|
|
});
|
|
|
|
}
|
2021-05-29 23:59:30 +08:00
|
|
|
}, []);
|
2020-11-05 15:22:59 +08:00
|
|
|
|
2021-05-29 23:59:30 +08:00
|
|
|
return [data, loading];
|
2020-11-05 15:22:59 +08:00
|
|
|
}
|