Skip to content

Commit

Permalink
Add rough item filtering (by name)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiliv committed May 3, 2024
1 parent 8fa696c commit 5a18aee
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/components/DisplayMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const OPTION_KEYS = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';

export default function DisplayMenu({
target,
inventory,

width, height, magnification=1,
keyMap={
Expand All @@ -29,12 +30,17 @@ export default function DisplayMenu({
const [selected, setSelected] = useState(0);
const [info, setInfo] = useState(null);
const [events, setEvents] = useState([]);
const _inventory = useRef(inventory);
const useKeyDownRef = useRef(false);

const _viewportHeight = height - menus.length;
const _page = Math.floor(selected / _viewportHeight);
const _offset = _page * _viewportHeight;

useEffect(() => {
_inventory.current = inventory;
}, [inventory]);

// Set fresh menu on target change
useEffect(() => {
if (!target) {
Expand Down Expand Up @@ -63,10 +69,12 @@ export default function DisplayMenu({

// Set options to current menu
useEffect(() => {
const { items=null, text=null } = menus[menus.length - 1] || {};
setOptions(items);
const { filter=null, items=null, text=null } = menus[menus.length - 1] || {};
setOptions(items ? items.filter(
(item) => filter ? filter(({ item, inventory: _inventory.current })) : true
) : null);
setText(text);
}, [menus.length, menus[0], width, _viewportHeight]);
}, [events, menus.length, menus[0], width, _viewportHeight]);

// Set optionsViewport to current menu page
useEffect(() => {
Expand Down Expand Up @@ -153,9 +161,8 @@ export default function DisplayMenu({
setMenus((menus) => {
menus[menus.length - 1].selected = selected;
return [...menus, {
...option,
title: `${OPTION_KEYS[selected]}:${option.name}`,
items: option.items,
text: option.text,
}];
});
return 0;
Expand Down Expand Up @@ -230,11 +237,12 @@ export default function DisplayMenu({

// Update info label for current menu
useEffect(() => {
if (!options) {
if (!options || !options.length) {
setInfo(null);
return;
}
const { stats: { A, D }={} } = options[selected];
const { stats } = options[selected];
const { A, D } = stats || {};
if (A !== undefined) {
setInfo(`Atk ${minifyNumbers(A)}`);
} else if (D !== undefined) {
Expand Down
1 change: 1 addition & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function parseInteraction(interaction, dataFileText, { name, inventory })
if (actions[ACTIONS.BUY] !== undefined) {
Object.assign(actions[ACTIONS.BUY], {
items: Buy.parse(actions[ACTIONS.BUY]),
filter: ({ item, inventory }) => !inventory[item.kind]?.find(({ name }) => name === item.name),
text: null,
});
}
Expand Down

0 comments on commit 5a18aee

Please sign in to comment.