import { useCallback } from "react"; import { useTranslation } from "react-i18next"; import { useAuth } from "@/auth"; import { useApi, useApiCall, Assets, DailyForecasts } from "@/api"; import { saveFile, buildFilename } from "./utils"; import Loader, { LoaderColor } from "../Loader"; import styles from "./styles.module.css"; import { useSelector } from "react-redux"; import { selectors } from "@/store"; import { getCategoryIdByZodiacSign, getZodiacSignByDate, } from "@/services/zodiac-sign"; import { Zodiac, ZodiacParagraph } from "@/api/resources/Zodiacs"; type Forecasts = DailyForecasts.Forecast[]; type PersonalAssets = Assets.Asset[]; interface WallpaperData { assets: PersonalAssets; forecasts: Forecasts; zodiac: Zodiac; } function getZodiacParagraphs( paragraphs: ZodiacParagraph[], depth = 0 ): JSX.Element[] { depth++; if (!paragraphs) { return []; } function getTypeOfContent(content: string[] | ZodiacParagraph[]) { if (Array.isArray(content) && content.length > 0) { return typeof content[0]; } } return paragraphs.map((paragraph, index) => { const Headline = `h${depth}` as keyof JSX.IntrinsicElements; return (
{paragraph.title} {getTypeOfContent(paragraph.content) === "string" ? paragraph.content.map((content, _index) => (

{content as string}

)) : getZodiacParagraphs(paragraph.content as ZodiacParagraph[], depth)}
); }); } function WallpaperPage(): JSX.Element { const api = useApi(); const { i18n } = useTranslation(); const locale = i18n.language; const token = useSelector(selectors.selectToken); const { user, // token } = useAuth(); const birthdate = useSelector(selectors.selectBirthdate); const zodiacSign = getZodiacSignByDate(birthdate); const category = user?.profile.sign?.sign || ""; const loadData = useCallback(async () => { const { asset_categories } = await api.getAssetCategories({ locale }); const categoryId = getCategoryIdByZodiacSign(zodiacSign, asset_categories); return Promise.all([ api.getZodiacs({ zodiac: zodiacSign.toLowerCase(), token }), api.getAssets({ category: String(categoryId || "1") }), api.getDailyForecasts({ token }), ]).then(([{ zodiac }, { assets }, { user_daily_forecast }]) => ({ zodiac, assets, forecasts: user_daily_forecast.forecasts, })); }, [api, token, locale, zodiacSign]); const { data, isPending } = useApiCall(loadData); const asset = data ? data.assets.at(0) : null; const zodiacInfo = data ? data.zodiac : null; const handleClick = () => asset && saveFile(asset.url.replace("http://", "https://"), buildFilename(category)); return (
{asset ? (
) : null} {isPending ? : null}
{isPending ? null : ( <>
{zodiacInfo?.metas.map((meta, index) => (

{meta.label}

{meta.value}

))}
{getZodiacParagraphs(zodiacInfo?.paragraphs || [])}
)}
); } export default WallpaperPage;