mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-12-05 07:43:49 +01:00
enter to search tank
This commit is contained in:
3 files changed
+37
-18
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import type { TankDefinition } from "@blitzkit/protos";
|
||||
import locales from "@blitzkit/i18n/locales.json";
|
||||
import { MagnifyingGlassIcon } from "@radix-ui/react-icons";
|
||||
import {
|
||||
type KeyboardEventHandler,
|
||||
@@ -6,21 +7,23 @@ import {
|
||||
useEffect,
|
||||
useRef,
|
||||
} from "react";
|
||||
import { searchTanks } from "../../core/blitzkit/searchTanks";
|
||||
import { withLocale } from "../../hocs/withLocale";
|
||||
import { useLocale } from "../../hooks/useLocale";
|
||||
import { useStrings } from "../../hooks/useStrings";
|
||||
import { TankFilters } from "../../stores/tankFilters";
|
||||
import { TankSearchSort } from "../TankSearchSort";
|
||||
import { TextField } from "../TextField";
|
||||
|
||||
type SearchBarProps = {
|
||||
topResult?: TankDefinition;
|
||||
interface SearchBarProps {
|
||||
onSelect?: (tank: TankDefinition) => void;
|
||||
};
|
||||
}
|
||||
|
||||
export const TankSearchBar = withLocale(Content);
|
||||
|
||||
function Content({ topResult, onSelect }: SearchBarProps) {
|
||||
function Content({ onSelect }: SearchBarProps) {
|
||||
const strings = useStrings();
|
||||
const locale = useLocale();
|
||||
|
||||
const search = TankFilters.use((state) => state.search);
|
||||
|
||||
@@ -49,21 +52,26 @@ function Content({ topResult, onSelect }: SearchBarProps) {
|
||||
|
||||
previousSearch.current = search;
|
||||
}, [search]);
|
||||
|
||||
const handleKeyDown = useCallback<KeyboardEventHandler>(
|
||||
(event) => {
|
||||
if (event.key !== "Enter" || !topResult || !TankFilters.state.search) {
|
||||
return;
|
||||
}
|
||||
const { search } = TankFilters.state;
|
||||
if (event.key !== "Enter" || search === null) return;
|
||||
|
||||
const [topResult] = searchTanks(search);
|
||||
if (!topResult) return;
|
||||
|
||||
event.preventDefault();
|
||||
|
||||
if (onSelect) {
|
||||
onSelect(topResult);
|
||||
} else {
|
||||
window.location.href = `/tanks/${topResult.slug}`;
|
||||
return;
|
||||
}
|
||||
|
||||
const prefix = locale === locales.default ? "" : `/${locale}`;
|
||||
window.location.href = `${prefix}/tanks/${topResult.slug}`;
|
||||
},
|
||||
[topResult],
|
||||
[onSelect, locale],
|
||||
);
|
||||
|
||||
return (
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { metaSortTanks, SEARCH_KEYS } from "@blitzkit/core";
|
||||
import { metaSortTanks } from "@blitzkit/core";
|
||||
import { literals } from "@blitzkit/i18n/src/literals";
|
||||
import type { TankDefinition } from "@blitzkit/protos";
|
||||
import fuzzysort from "fuzzysort";
|
||||
import { uniq } from "lodash-es";
|
||||
import { memo, useMemo } from "react";
|
||||
import usePromise from "react-promise-suspense";
|
||||
@@ -9,8 +8,8 @@ import type { Vector2Tuple } from "three";
|
||||
import { api } from "../../api/dynamic";
|
||||
import { characteristics } from "../../config/characteristics";
|
||||
import { characteristicRenderConfigs } from "../../config/characteristicsGroups";
|
||||
import { awaitableTankNames } from "../../core/awaitables/tankNames";
|
||||
import { filterTanks } from "../../core/blitzkit/filterTanks";
|
||||
import { searchTanks } from "../../core/blitzkit/searchTanks";
|
||||
import { withLocale } from "../../hocs/withLocale";
|
||||
import { useCharacteristicRenderer } from "../../hooks/useCharacteristicRenderer";
|
||||
import { computeDefaultCharacteristics } from "../../hooks/useCharacteristics";
|
||||
@@ -46,10 +45,9 @@ export type TankSearchResultsProps = {
|
||||
};
|
||||
|
||||
// TODO: across the project, remove "definitions"
|
||||
const [gameDefinitions, tankDefinitions, tankNames] = await Promise.all([
|
||||
const [gameDefinitions, tankDefinitions] = await Promise.all([
|
||||
api.game(),
|
||||
api.tanks(),
|
||||
awaitableTankNames,
|
||||
]);
|
||||
const tankDefinitionsArray = Object.values(tankDefinitions.tanks);
|
||||
|
||||
@@ -90,9 +88,7 @@ function Content({ onSelect, onSelectAll }: TankSearchResultsProps) {
|
||||
if (tankFilters.search === null) {
|
||||
filtered = await filterTanks(tankFilters, filtered, wargaming?.id);
|
||||
} else {
|
||||
filtered = fuzzysort
|
||||
.go(tankFilters.search, tankNames, { keys: SEARCH_KEYS })
|
||||
.map((result) => tankDefinitions.tanks[result.obj.id]);
|
||||
filtered = searchTanks(tankFilters.search);
|
||||
}
|
||||
|
||||
return filtered;
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { SEARCH_KEYS } from "@blitzkit/core";
|
||||
import fuzzysort from "fuzzysort";
|
||||
import { api } from "../../api/dynamic";
|
||||
import { awaitableTankNames } from "../awaitables/tankNames";
|
||||
|
||||
const [tankDefinitions, tankNames] = await Promise.all([
|
||||
api.tanks(),
|
||||
awaitableTankNames,
|
||||
]);
|
||||
|
||||
export function searchTanks(search: string) {
|
||||
return fuzzysort
|
||||
.go(search, tankNames, { keys: SEARCH_KEYS })
|
||||
.map((result) => tankDefinitions.tanks[result.obj.id]);
|
||||
}
|
||||
Reference in new issue
Block a user