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 type { TankDefinition } from "@blitzkit/protos";
|
||||||
|
import locales from "@blitzkit/i18n/locales.json";
|
||||||
import { MagnifyingGlassIcon } from "@radix-ui/react-icons";
|
import { MagnifyingGlassIcon } from "@radix-ui/react-icons";
|
||||||
import {
|
import {
|
||||||
type KeyboardEventHandler,
|
type KeyboardEventHandler,
|
||||||
@@ -6,21 +7,23 @@ import {
|
|||||||
useEffect,
|
useEffect,
|
||||||
useRef,
|
useRef,
|
||||||
} from "react";
|
} from "react";
|
||||||
|
import { searchTanks } from "../../core/blitzkit/searchTanks";
|
||||||
import { withLocale } from "../../hocs/withLocale";
|
import { withLocale } from "../../hocs/withLocale";
|
||||||
|
import { useLocale } from "../../hooks/useLocale";
|
||||||
import { useStrings } from "../../hooks/useStrings";
|
import { useStrings } from "../../hooks/useStrings";
|
||||||
import { TankFilters } from "../../stores/tankFilters";
|
import { TankFilters } from "../../stores/tankFilters";
|
||||||
import { TankSearchSort } from "../TankSearchSort";
|
import { TankSearchSort } from "../TankSearchSort";
|
||||||
import { TextField } from "../TextField";
|
import { TextField } from "../TextField";
|
||||||
|
|
||||||
type SearchBarProps = {
|
interface SearchBarProps {
|
||||||
topResult?: TankDefinition;
|
|
||||||
onSelect?: (tank: TankDefinition) => void;
|
onSelect?: (tank: TankDefinition) => void;
|
||||||
};
|
}
|
||||||
|
|
||||||
export const TankSearchBar = withLocale(Content);
|
export const TankSearchBar = withLocale(Content);
|
||||||
|
|
||||||
function Content({ topResult, onSelect }: SearchBarProps) {
|
function Content({ onSelect }: SearchBarProps) {
|
||||||
const strings = useStrings();
|
const strings = useStrings();
|
||||||
|
const locale = useLocale();
|
||||||
|
|
||||||
const search = TankFilters.use((state) => state.search);
|
const search = TankFilters.use((state) => state.search);
|
||||||
|
|
||||||
@@ -49,21 +52,26 @@ function Content({ topResult, onSelect }: SearchBarProps) {
|
|||||||
|
|
||||||
previousSearch.current = search;
|
previousSearch.current = search;
|
||||||
}, [search]);
|
}, [search]);
|
||||||
|
|
||||||
const handleKeyDown = useCallback<KeyboardEventHandler>(
|
const handleKeyDown = useCallback<KeyboardEventHandler>(
|
||||||
(event) => {
|
(event) => {
|
||||||
if (event.key !== "Enter" || !topResult || !TankFilters.state.search) {
|
const { search } = TankFilters.state;
|
||||||
return;
|
if (event.key !== "Enter" || search === null) return;
|
||||||
}
|
|
||||||
|
const [topResult] = searchTanks(search);
|
||||||
|
if (!topResult) return;
|
||||||
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
if (onSelect) {
|
if (onSelect) {
|
||||||
onSelect(topResult);
|
onSelect(topResult);
|
||||||
} else {
|
return;
|
||||||
window.location.href = `/tanks/${topResult.slug}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const prefix = locale === locales.default ? "" : `/${locale}`;
|
||||||
|
window.location.href = `${prefix}/tanks/${topResult.slug}`;
|
||||||
},
|
},
|
||||||
[topResult],
|
[onSelect, locale],
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
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 { literals } from "@blitzkit/i18n/src/literals";
|
||||||
import type { TankDefinition } from "@blitzkit/protos";
|
import type { TankDefinition } from "@blitzkit/protos";
|
||||||
import fuzzysort from "fuzzysort";
|
|
||||||
import { uniq } from "lodash-es";
|
import { uniq } from "lodash-es";
|
||||||
import { memo, useMemo } from "react";
|
import { memo, useMemo } from "react";
|
||||||
import usePromise from "react-promise-suspense";
|
import usePromise from "react-promise-suspense";
|
||||||
@@ -9,8 +8,8 @@ import type { Vector2Tuple } from "three";
|
|||||||
import { api } from "../../api/dynamic";
|
import { api } from "../../api/dynamic";
|
||||||
import { characteristics } from "../../config/characteristics";
|
import { characteristics } from "../../config/characteristics";
|
||||||
import { characteristicRenderConfigs } from "../../config/characteristicsGroups";
|
import { characteristicRenderConfigs } from "../../config/characteristicsGroups";
|
||||||
import { awaitableTankNames } from "../../core/awaitables/tankNames";
|
|
||||||
import { filterTanks } from "../../core/blitzkit/filterTanks";
|
import { filterTanks } from "../../core/blitzkit/filterTanks";
|
||||||
|
import { searchTanks } from "../../core/blitzkit/searchTanks";
|
||||||
import { withLocale } from "../../hocs/withLocale";
|
import { withLocale } from "../../hocs/withLocale";
|
||||||
import { useCharacteristicRenderer } from "../../hooks/useCharacteristicRenderer";
|
import { useCharacteristicRenderer } from "../../hooks/useCharacteristicRenderer";
|
||||||
import { computeDefaultCharacteristics } from "../../hooks/useCharacteristics";
|
import { computeDefaultCharacteristics } from "../../hooks/useCharacteristics";
|
||||||
@@ -46,10 +45,9 @@ export type TankSearchResultsProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// TODO: across the project, remove "definitions"
|
// TODO: across the project, remove "definitions"
|
||||||
const [gameDefinitions, tankDefinitions, tankNames] = await Promise.all([
|
const [gameDefinitions, tankDefinitions] = await Promise.all([
|
||||||
api.game(),
|
api.game(),
|
||||||
api.tanks(),
|
api.tanks(),
|
||||||
awaitableTankNames,
|
|
||||||
]);
|
]);
|
||||||
const tankDefinitionsArray = Object.values(tankDefinitions.tanks);
|
const tankDefinitionsArray = Object.values(tankDefinitions.tanks);
|
||||||
|
|
||||||
@@ -90,9 +88,7 @@ function Content({ onSelect, onSelectAll }: TankSearchResultsProps) {
|
|||||||
if (tankFilters.search === null) {
|
if (tankFilters.search === null) {
|
||||||
filtered = await filterTanks(tankFilters, filtered, wargaming?.id);
|
filtered = await filterTanks(tankFilters, filtered, wargaming?.id);
|
||||||
} else {
|
} else {
|
||||||
filtered = fuzzysort
|
filtered = searchTanks(tankFilters.search);
|
||||||
.go(tankFilters.search, tankNames, { keys: SEARCH_KEYS })
|
|
||||||
.map((result) => tankDefinitions.tanks[result.obj.id]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return filtered;
|
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