diff --git a/src/components/achievements/AchievementsView.tsx b/src/components/achievements/AchievementsView.tsx index b4979006..6c7940a7 100644 --- a/src/components/achievements/AchievementsView.tsx +++ b/src/components/achievements/AchievementsView.tsx @@ -1,5 +1,5 @@ import { ILinkEventTracker } from '@nitrots/nitro-renderer'; -import { FC, useEffect, useMemo, useState } from 'react'; +import { FC, useEffect, useState } from 'react'; import { AchievementUtilities, AddEventLinkTracker, LocalizeText, RemoveLinkEventTracker } from '../../api'; import { Base, Column, LayoutImage, LayoutProgressBar, NitroCardContentView, NitroCardHeaderView, NitroCardSubHeaderView, NitroCardView, Text } from '../../common'; import { useAchievements } from '../../hooks'; @@ -9,14 +9,7 @@ import { AchievementsCategoryListView } from './views/category-list/Achievements export const AchievementsView: FC<{}> = props => { const [ isVisible, setIsVisible ] = useState(false); - const { achievementCategories = [], selectedCategoryCode = null, setSelectedCategoryCode = null, selectedAchievementId = -1, setSelectedAchievementId = null, achievementScore = 0, getProgress = 0, getMaxProgress = 0, setAchievementSeen = null } = useAchievements(); - - const selectedCategory = useMemo(() => - { - if(selectedCategoryCode === null) return null; - - return achievementCategories.find(category => (category.code === selectedCategoryCode)); - }, [ achievementCategories, selectedCategoryCode ]); + const { achievementCategories = [], selectedCategoryCode = null, setSelectedCategoryCode = null, achievementScore = 0, getProgress = 0, getMaxProgress = 0, selectedCategory = null } = useAchievements(); useEffect(() => { @@ -72,7 +65,7 @@ export const AchievementsView: FC<{}> = props => } { selectedCategory && - } + } ); diff --git a/src/components/achievements/views/AchievementCategoryView.tsx b/src/components/achievements/views/AchievementCategoryView.tsx index d5834c7c..8774d208 100644 --- a/src/components/achievements/views/AchievementCategoryView.tsx +++ b/src/components/achievements/views/AchievementCategoryView.tsx @@ -1,48 +1,35 @@ -import { Dispatch, FC, SetStateAction, useEffect, useMemo } from 'react'; +import { FC, useEffect } from 'react'; import { AchievementCategory } from '../../../api'; import { Column } from '../../../common'; -import { AchievementListView } from './achievement-list/AchievementListView'; +import { useAchievements } from '../../../hooks'; +import { AchievementListView } from './achievement-list'; import { AchievementDetailsView } from './AchievementDetailsView'; interface AchievementCategoryViewProps { category: AchievementCategory; - selectedAchievementId: number; - setSelectedAchievementId: Dispatch>; - setAchievementSeen: (code: string, achievementId: number) => void; } export const AchievementCategoryView: FC = props => { - const { category = null, selectedAchievementId = -1, setSelectedAchievementId = null, setAchievementSeen = null } = props; - - const selectedAchievement = useMemo(() => - { - if(selectedAchievementId === -1) return null; - - return category.achievements.find(achievement => (achievement.achievementId === selectedAchievementId)); - }, [ category, selectedAchievementId ]); + const { category = null } = props; + const { selectedAchievement = null, setSelectedAchievementId = null } = useAchievements(); useEffect(() => { + if(!category) return; + if(!selectedAchievement) { - if(category.achievements.length) setSelectedAchievementId(category.achievements[0].achievementId); + setSelectedAchievementId(category?.achievements?.[0]?.achievementId); } - }, [ selectedAchievement, category, setSelectedAchievementId ]); - - useEffect(() => - { - if(!selectedAchievement) return; - - setAchievementSeen(category.code, selectedAchievement.achievementId); - }, [ selectedAchievement, category, setAchievementSeen ]); + }, [ category, selectedAchievement, setSelectedAchievementId ]); if(!category) return null; return ( - + { !!selectedAchievement && } diff --git a/src/components/achievements/views/AchievementDetailsView.tsx b/src/components/achievements/views/AchievementDetailsView.tsx index 6e978250..b04fa183 100644 --- a/src/components/achievements/views/AchievementDetailsView.tsx +++ b/src/components/achievements/views/AchievementDetailsView.tsx @@ -1,5 +1,5 @@ import { AchievementData } from '@nitrots/nitro-renderer'; -import { FC, PropsWithChildren } from 'react'; +import { FC } from 'react'; import { AchievementUtilities, LocalizeBadgeDescription, LocalizeBadgeName, LocalizeText } from '../../../api'; import { Column, Flex, LayoutCurrencyIcon, LayoutProgressBar, Text } from '../../../common'; import { AchievementBadgeView } from './AchievementBadgeView'; @@ -9,14 +9,14 @@ interface AchievementDetailsViewProps achievement: AchievementData; } -export const AchievementDetailsView: FC> = props => +export const AchievementDetailsView: FC = props => { - const { achievement = null, children = null, ...rest } = props; + const { achievement = null } = props; if(!achievement) return null; return ( - + @@ -48,7 +48,6 @@ export const AchievementDetailsView: FC } } - { children } ) } diff --git a/src/components/achievements/views/achievement-list/AchievementListItemView.tsx b/src/components/achievements/views/achievement-list/AchievementListItemView.tsx index bf686088..9da632bd 100644 --- a/src/components/achievements/views/achievement-list/AchievementListItemView.tsx +++ b/src/components/achievements/views/achievement-list/AchievementListItemView.tsx @@ -1,25 +1,24 @@ import { AchievementData } from '@nitrots/nitro-renderer'; -import { Dispatch, FC, PropsWithChildren, SetStateAction } from 'react'; +import { FC } from 'react'; import { LayoutGridItem } from '../../../../common'; +import { useAchievements } from '../../../../hooks'; import { AchievementBadgeView } from '../AchievementBadgeView'; interface AchievementListItemViewProps { achievement: AchievementData; - selectedAchievementId: number; - setSelectedAchievementId: Dispatch>; } -export const AchievementListItemView: FC> = props => +export const AchievementListItemView: FC = props => { - const { achievement = null, selectedAchievementId = -1, setSelectedAchievementId = null, children = null, ...rest } = props; + const { achievement = null } = props; + const { selectedAchievement = null, setSelectedAchievementId = null } = useAchievements(); if(!achievement) return null; return ( - 0) } onClick={ event => setSelectedAchievementId(achievement.achievementId) } { ...rest }> + 0) } onClick={ event => setSelectedAchievementId(achievement.achievementId) }> - { children } ); } diff --git a/src/components/achievements/views/achievement-list/AchievementListView.tsx b/src/components/achievements/views/achievement-list/AchievementListView.tsx index 341af280..f0095817 100644 --- a/src/components/achievements/views/achievement-list/AchievementListView.tsx +++ b/src/components/achievements/views/achievement-list/AchievementListView.tsx @@ -1,23 +1,20 @@ import { AchievementData } from '@nitrots/nitro-renderer'; -import { Dispatch, FC, PropsWithChildren, SetStateAction } from 'react'; +import { FC } from 'react'; import { AutoGrid } from '../../../../common'; import { AchievementListItemView } from './AchievementListItemView'; interface AchievementListViewProps { achievements: AchievementData[]; - selectedAchievementId: number; - setSelectedAchievementId: Dispatch>; } -export const AchievementListView: FC> = props => +export const AchievementListView: FC = props => { - const { achievements = null, selectedAchievementId = -1, setSelectedAchievementId = null, children = null, ...rest } = props; + const { achievements = null } = props; return ( - - { achievements && (achievements.length > 0) && achievements.map((achievement, index) => ) } - { children } + + { achievements && (achievements.length > 0) && achievements.map((achievement, index) => ) } ); } diff --git a/src/components/achievements/views/achievement-list/index.ts b/src/components/achievements/views/achievement-list/index.ts new file mode 100644 index 00000000..87ccb432 --- /dev/null +++ b/src/components/achievements/views/achievement-list/index.ts @@ -0,0 +1,2 @@ +export * from './AchievementListItemView'; +export * from './AchievementListView'; diff --git a/src/components/achievements/views/category-list/AchievementsCategoryListItemView.tsx b/src/components/achievements/views/category-list/AchievementsCategoryListItemView.tsx index 2af18b96..91b96c6e 100644 --- a/src/components/achievements/views/category-list/AchievementsCategoryListItemView.tsx +++ b/src/components/achievements/views/category-list/AchievementsCategoryListItemView.tsx @@ -1,4 +1,4 @@ -import { Dispatch, FC, PropsWithChildren, SetStateAction } from 'react'; +import { Dispatch, FC, SetStateAction } from 'react'; import { AchievementUtilities, IAchievementCategory, LocalizeText } from '../../../../api'; import { LayoutBackgroundImage, LayoutGridItem, Text } from '../../../../common'; @@ -9,9 +9,9 @@ interface AchievementCategoryListItemViewProps setSelectedCategoryCode: Dispatch>; } -export const AchievementsCategoryListItemView: FC> = props => +export const AchievementsCategoryListItemView: FC = props => { - const { category = null, selectedCategoryCode = null, setSelectedCategoryCode = null, children = null, ...rest } = props; + const { category = null, selectedCategoryCode = null, setSelectedCategoryCode = null } = props; if(!category) return null; @@ -21,12 +21,11 @@ export const AchievementsCategoryListItemView: FC setSelectedCategoryCode(category.code) } { ...rest }> + setSelectedCategoryCode(category.code) }> { LocalizeText(`quests.${ category.code }.name`) } { progress } / { maxProgress } - { children } ); } diff --git a/src/components/achievements/views/category-list/AchievementsCategoryListView.tsx b/src/components/achievements/views/category-list/AchievementsCategoryListView.tsx index 231cd4f4..ca362968 100644 --- a/src/components/achievements/views/category-list/AchievementsCategoryListView.tsx +++ b/src/components/achievements/views/category-list/AchievementsCategoryListView.tsx @@ -1,4 +1,4 @@ -import { Dispatch, FC, PropsWithChildren, SetStateAction } from 'react'; +import { Dispatch, FC, SetStateAction } from 'react'; import { IAchievementCategory } from '../../../../api'; import { AutoGrid } from '../../../../common'; import { AchievementsCategoryListItemView } from './AchievementsCategoryListItemView'; @@ -10,14 +10,13 @@ interface AchievementsCategoryListViewProps setSelectedCategoryCode: Dispatch>; } -export const AchievementsCategoryListView: FC> = props => +export const AchievementsCategoryListView: FC = props => { - const { categories = null, selectedCategoryCode = null, setSelectedCategoryCode = null, children = null, ...rest } = props; + const { categories = null, selectedCategoryCode = null, setSelectedCategoryCode = null } = props; return ( - + { categories && (categories.length > 0) && categories.map((category, index) => ) } - { children } ); }; diff --git a/src/components/achievements/views/category-list/index.ts b/src/components/achievements/views/category-list/index.ts new file mode 100644 index 00000000..5a367f85 --- /dev/null +++ b/src/components/achievements/views/category-list/index.ts @@ -0,0 +1,2 @@ +export * from './AchievementsCategoryListItemView'; +export * from './AchievementsCategoryListView'; diff --git a/src/components/achievements/views/index.ts b/src/components/achievements/views/index.ts new file mode 100644 index 00000000..576c6357 --- /dev/null +++ b/src/components/achievements/views/index.ts @@ -0,0 +1,5 @@ +export * from './achievement-list'; +export * from './AchievementBadgeView'; +export * from './AchievementCategoryView'; +export * from './AchievementDetailsView'; +export * from './category-list'; diff --git a/src/hooks/achievements/useAchievements.ts b/src/hooks/achievements/useAchievements.ts index 8c3dceee..29f10de4 100644 --- a/src/hooks/achievements/useAchievements.ts +++ b/src/hooks/achievements/useAchievements.ts @@ -1,5 +1,6 @@ import { AchievementData, AchievementEvent, AchievementsEvent, AchievementsScoreEvent, RequestAchievementsMessageComposer } from '@nitrots/nitro-renderer'; -import { useEffect, useMemo, useState } from 'react'; +import { useCallback, useEffect, useMemo, useState } from 'react'; +import { useBetween } from 'use-between'; import { AchievementCategory, AchievementUtilities, CloneObject, SendMessageComposer } from '../../api'; import { useMessageEvent } from '../events'; @@ -43,7 +44,21 @@ const useAchievementsState = () => return ~~((((getProgress - 0) * (100 - 0)) / (getMaxProgress - 0)) + 0); }, [ getProgress, getMaxProgress ]); - const setAchievementSeen = (categoryCode: string, achievementId: number) => + const selectedCategory = useMemo(() => + { + if(selectedCategoryCode === null) return null; + + return achievementCategories.find(category => (category.code === selectedCategoryCode)); + }, [ achievementCategories, selectedCategoryCode ]); + + const selectedAchievement = useMemo(() => + { + if((selectedAchievementId === -1) || !selectedCategory) return null; + + return selectedCategory.achievements.find(achievement => (achievement.achievementId === selectedAchievementId)); + }, [ selectedCategory, selectedAchievementId ]); + + const setAchievementSeen = useCallback((categoryCode: string, achievementId: number) => { setAchievementCategories(prevValue => { @@ -63,7 +78,7 @@ const useAchievementsState = () => return newValue; }); - } + }, []); useMessageEvent(AchievementEvent, event => { @@ -157,7 +172,7 @@ const useAchievementsState = () => setNeedsUpdate(false); }, [ needsUpdate ]); - return { achievementCategories, selectedCategoryCode, setSelectedCategoryCode, selectedAchievementId, setSelectedAchievementId, achievementScore, getTotalUnseen, getProgress, getMaxProgress, scaledProgressPercent, setAchievementSeen }; + return { achievementCategories, selectedCategoryCode, setSelectedCategoryCode, selectedAchievementId, setSelectedAchievementId, achievementScore, getTotalUnseen, getProgress, getMaxProgress, scaledProgressPercent, selectedCategory, selectedAchievement, setAchievementSeen }; } -export const useAchievements = useAchievementsState; +export const useAchievements = () => useBetween(useAchievementsState);