-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(moves & oracles): Changed filtering & enabled the new view
- Loading branch information
1 parent
62df549
commit 9d81545
Showing
12 changed files
with
331 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 46 additions & 38 deletions
84
src/components/features/charactersAndCampaigns/MovesSection/useFilterMoves.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,70 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useSearch } from "hooks/useSearch"; | ||
import { orderedCategories } from "data/moves"; | ||
import { Move, MoveCategory } from "dataforged"; | ||
import { useCustomMoves } from "./useCustomMoves"; | ||
import { useStore } from "stores/store"; | ||
import { useMemo, useState } from "react"; | ||
import { useMoves } from "./useMoves"; | ||
|
||
export enum CATEGORY_VISIBILITY { | ||
HIDDEN, | ||
SOME, | ||
ALL, | ||
} | ||
|
||
export function useFilterMoves() { | ||
const { search, setSearch, debouncedSearch } = useSearch(); | ||
const [filteredMoves, setFilteredMoves] = useState(orderedCategories); | ||
const { customMoveCategories } = useCustomMoves(); | ||
|
||
const showDelveMoves = useStore( | ||
(store) => store.settings.delve.showDelveMoves | ||
); | ||
|
||
useEffect(() => { | ||
const results: MoveCategory[] = []; | ||
|
||
const allCategories = [ | ||
...orderedCategories, | ||
...customMoveCategories, | ||
].filter( | ||
(category) => | ||
showDelveMoves || category.Source.Title !== "Ironsworn: Delve" | ||
); | ||
|
||
allCategories.forEach((category) => { | ||
const [search, setSearch] = useState(""); | ||
|
||
const moveCategories = useMoves(); | ||
const { visibleMoveCategoryIds, visibleMoveIds, isEmpty } = useMemo(() => { | ||
const visibleCategories: Record<string, CATEGORY_VISIBILITY> = {}; | ||
const visibleMoves: Record<string, boolean> = {}; | ||
let isEmpty: boolean = true; | ||
|
||
moveCategories.forEach((category) => { | ||
if ( | ||
category.Title.Standard.toLocaleLowerCase().includes( | ||
debouncedSearch.toLocaleLowerCase() | ||
!search || | ||
(category.Title.Standard.toLocaleLowerCase().includes( | ||
search.toLocaleLowerCase() | ||
) && | ||
Object.keys(category.Moves).length > 0 | ||
Object.keys(category.Moves).length > 0) | ||
) { | ||
results.push(category); | ||
visibleCategories[category.$id] = CATEGORY_VISIBILITY.ALL; | ||
isEmpty = false; | ||
return; | ||
} | ||
|
||
const Moves: { [key: string]: Move } = {}; | ||
let hasMove = false; | ||
|
||
Object.keys(category.Moves).forEach((moveId) => { | ||
const move = category.Moves[moveId]; | ||
|
||
if ( | ||
move.Title.Standard.toLocaleLowerCase().includes( | ||
debouncedSearch.toLocaleLowerCase() | ||
search.toLocaleLowerCase() | ||
) | ||
) { | ||
Moves[moveId] = move; | ||
hasMove = true; | ||
visibleMoves[move.$id] = true; | ||
} | ||
}); | ||
|
||
if (Object.keys(Moves).length > 0) { | ||
results.push({ ...category, Moves }); | ||
if (hasMove) { | ||
isEmpty = false; | ||
visibleCategories[category.$id] = CATEGORY_VISIBILITY.SOME; | ||
} else { | ||
visibleCategories[category.$id] = CATEGORY_VISIBILITY.HIDDEN; | ||
} | ||
}); | ||
|
||
setFilteredMoves(results); | ||
}, [debouncedSearch, customMoveCategories, showDelveMoves]); | ||
return { | ||
visibleMoveCategoryIds: visibleCategories, | ||
visibleMoveIds: visibleMoves, | ||
isEmpty, | ||
}; | ||
}, [moveCategories, search]); | ||
|
||
return { setSearch, filteredMoves, isSearchActive: !!search }; | ||
return { | ||
moveCategories, | ||
setSearch, | ||
visibleMoveCategoryIds, | ||
visibleMoveIds, | ||
isSearchActive: !!search, | ||
isEmpty, | ||
}; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/components/features/charactersAndCampaigns/MovesSection/useMoves.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useMemo } from "react"; | ||
import { orderedCategories } from "data/moves"; | ||
import { useCustomMoves } from "./useCustomMoves"; | ||
import { useStore } from "stores/store"; | ||
|
||
export function useMoves() { | ||
const { customMoveCategories } = useCustomMoves(); | ||
|
||
const showDelveMoves = useStore( | ||
(store) => store.settings.delve.showDelveMoves | ||
); | ||
|
||
const moveCategories = useMemo(() => { | ||
return [...orderedCategories, ...customMoveCategories].filter( | ||
(category) => | ||
showDelveMoves || category.Source.Title !== "Ironsworn: Delve" | ||
); | ||
}, [customMoveCategories, showDelveMoves]); | ||
|
||
return moveCategories; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.