diff --git a/packages/website/src/components/Button/index.module.css b/packages/website/src/components/Button/index.module.css index fe8b27f02..c27a3195b 100644 --- a/packages/website/src/components/Button/index.module.css +++ b/packages/website/src/components/Button/index.module.css @@ -1,6 +1,8 @@ .button { border: none; + white-space: nowrap; + display: flex; align-items: center; justify-content: center; diff --git a/packages/website/src/components/CompareControls/index.tsx b/packages/website/src/components/CompareControls/index.tsx index 98dbd5ad9..6122e2ea2 100644 --- a/packages/website/src/components/CompareControls/index.tsx +++ b/packages/website/src/components/CompareControls/index.tsx @@ -12,7 +12,7 @@ import { Button } from "../Button"; import { Checkbox } from "../Checkbox"; import { Dialog } from "../Dialog"; import { SegmentedControl } from "../SegmentedControl"; -import { TankSearch } from "../TankSearch"; +import { TankSearchResults } from "../TankSearchResults"; import styles from "./index.module.css"; export function CompareControls() { @@ -52,7 +52,7 @@ export function CompareControls() { - { CompareEphemeral.mutate((draft) => { diff --git a/packages/website/src/components/CompareTankControl/index.tsx b/packages/website/src/components/CompareTankControl/index.tsx index 6f4c004e2..a02c9f5a8 100644 --- a/packages/website/src/components/CompareTankControl/index.tsx +++ b/packages/website/src/components/CompareTankControl/index.tsx @@ -7,7 +7,7 @@ import { useStrings } from "../../hooks/useStrings"; import { CompareEphemeral } from "../../stores/compareEphemeral"; import { Dialog } from "../Dialog"; import { IconButton } from "../IconButton"; -import { TankSearch } from "../TankSearch"; +import { TankSearchResults } from "../TankSearchResults"; import styles from "./index.module.css"; interface CompareTankControlProps { @@ -51,7 +51,7 @@ export function CompareTankControl({ index, slug }: CompareTankControlProps) { - { CompareEphemeral.mutate((draft) => { diff --git a/packages/website/src/components/MixerModuleChooser/index.tsx b/packages/website/src/components/MixerModuleChooser/index.tsx index db432b1e3..984621939 100644 --- a/packages/website/src/components/MixerModuleChooser/index.tsx +++ b/packages/website/src/components/MixerModuleChooser/index.tsx @@ -16,7 +16,7 @@ import { IconButton } from "../IconButton"; import { LazyImage } from "../LazyImage"; import { Popover } from "../Popover"; import { Skeleton } from "../Skeleton"; -import { TankSearch } from "../TankSearch"; +import { TankSearchResults } from "../TankSearchResults"; import styles from "./index.module.css"; const tankDefinitions = await api.tanks(); @@ -26,7 +26,7 @@ type MixerModuleChooserProps = MaybeSkeletonComponentProps & { onSelect: ( tank: TankDefinition, turret?: TurretDefinition, - gun?: GunDefinition + gun?: GunDefinition, ) => void; turret?: number; gun?: number; @@ -69,7 +69,7 @@ export function MixerModuleChooser({ style={{ backgroundImage: `url(${alias( "api", - `/tanks/${tank}/icons/big.webp` + `/tanks/${tank}/icons/big.webp`, )})`, }} /> @@ -87,7 +87,7 @@ export function MixerModuleChooser({ - { onSelect?.(tank); diff --git a/packages/website/src/components/RecentlyViewedTanks/index.module.css b/packages/website/src/components/RecentlyViewedTanks/index.module.css index 625d13c86..2ed68eecc 100644 --- a/packages/website/src/components/RecentlyViewedTanks/index.module.css +++ b/packages/website/src/components/RecentlyViewedTanks/index.module.css @@ -1,8 +1,22 @@ .recent { - margin-top: var(--space-4); + display: flex; + flex-direction: column; + gap: var(--space-4); + padding: var(--space-4); background-color: var(--gray-a2); border-radius: var(--space-3); + + width: fit-content; + + .list { + display: flex; + gap: var(--space-4); + + width: fit-content; + + --min-tank-card-size: 7rem; + } } diff --git a/packages/website/src/components/RecentlyViewedTanks/index.tsx b/packages/website/src/components/RecentlyViewedTanks/index.tsx index 28d05be48..41cbf7dd7 100644 --- a/packages/website/src/components/RecentlyViewedTanks/index.tsx +++ b/packages/website/src/components/RecentlyViewedTanks/index.tsx @@ -1,32 +1,34 @@ import { isEqual } from "lodash-es"; import { useMemo } from "react"; import { api } from "../../api/dynamic"; +import { withLocale } from "../../hocs/withLocale"; import { useStrings } from "../../hooks/useStrings"; import { TankFilters } from "../../stores/tankFilters"; import { TankopediaPersistent } from "../../stores/tankopediaPersistent"; import { TankSort } from "../../stores/tankopediaSort"; import { TankCard } from "../TankCard"; -import { TankCardWrapper } from "../TankCardWrapper"; import { Text } from "../Text"; import styles from "./index.module.css"; const tankDefinitions = await api.tanks(); -export function RecentlyViewedTanks() { +export const RecentlyViewedTanks = withLocale(Content); + +function Content() { const filters = TankFilters.use(); // non-reactive because it is a little weird that it updates instantly even before the page loads const recentlyViewed = TankopediaPersistent.state.recentlyViewed.filter( - (id) => id in tankDefinitions.tanks + (id) => id in tankDefinitions.tanks, ); const hasFilters = useMemo( () => Object.entries(filters).some(([key, value]) => { return !isEqual( value, - TankFilters.initial[key as keyof typeof TankFilters.initial] + TankFilters.initial[key as keyof typeof TankFilters.initial], ); }), - [filters] + [filters], ); const strings = useStrings(); @@ -37,23 +39,16 @@ export function RecentlyViewedTanks() { } return ( -
- +
+ {strings.website.common.tank_search.recent} - +
{recentlyViewed.map((id) => ( ))} - +
); } diff --git a/packages/website/src/components/Scroller/index.tsx b/packages/website/src/components/Scroller/index.tsx index c7c2094a9..6ff672cbd 100644 --- a/packages/website/src/components/Scroller/index.tsx +++ b/packages/website/src/components/Scroller/index.tsx @@ -78,6 +78,7 @@ interface Drag { axis: Axis; } +// TODO: sunset scroller export function Scroller({ children, className, diff --git a/packages/website/src/components/ScrollerCurtains/index.module.css b/packages/website/src/components/ScrollerCurtains/index.module.css new file mode 100644 index 000000000..d7e833d1e --- /dev/null +++ b/packages/website/src/components/ScrollerCurtains/index.module.css @@ -0,0 +1,29 @@ +.anchor { + display: none; +} + +.target { + --scroller-curtains-size: min(25%, var(--space-6)); + --scroller-curtains-x-start: 0; + --scroller-curtains-x-end: 0; + --scroller-curtains-y-start: 0; + --scroller-curtains-y-end: 0; + + /* Masks the element's own box, so the fade stays put while the content scrolls under it */ + mask-image: + linear-gradient( + 90deg, + rgb(0 0 0 / calc(1 - var(--scroller-curtains-x-start))), + black var(--scroller-curtains-size), + black calc(100% - var(--scroller-curtains-size)), + rgb(0 0 0 / calc(1 - var(--scroller-curtains-x-end))) + ), + linear-gradient( + 180deg, + rgb(0 0 0 / calc(1 - var(--scroller-curtains-y-start))), + black var(--scroller-curtains-size), + black calc(100% - var(--scroller-curtains-size)), + rgb(0 0 0 / calc(1 - var(--scroller-curtains-y-end))) + ); + mask-composite: intersect; +} diff --git a/packages/website/src/components/ScrollerCurtains/index.tsx b/packages/website/src/components/ScrollerCurtains/index.tsx new file mode 100644 index 000000000..5890f0dac --- /dev/null +++ b/packages/website/src/components/ScrollerCurtains/index.tsx @@ -0,0 +1,136 @@ +import { useCallback, useEffect, useRef } from "react"; +import { clamp } from "three/src/math/MathUtils.js"; +import { ScrollerOrientation } from "../Scroller"; +import styles from "./index.module.css"; + +enum Axis { + X = "x", + Y = "y", +} + +enum Side { + Start = "start", + End = "end", +} + +interface ScrollerCurtainsProps { + orientation?: ScrollerOrientation; + + // Whether the fade at each end of the scroll axis is rendered + startCurtain?: boolean; + endCurtain?: boolean; +} + +const MAX_FADE_DISTANCE = 60; +const FADE_RATIO = 0.15; + +const ORIENTATION_AXES = { + [ScrollerOrientation.Horizontal]: [Axis.X], + [ScrollerOrientation.Vertical]: [Axis.Y], + [ScrollerOrientation.Both]: [Axis.X, Axis.Y], +} satisfies Record; + +const DIMENSIONS = { + [Axis.X]: { + scroll: "scrollLeft", + scrollSize: "scrollWidth", + clientSize: "clientWidth", + }, + [Axis.Y]: { + scroll: "scrollTop", + scrollSize: "scrollHeight", + clientSize: "clientHeight", + }, +} as const; + +// Fades the edges of the parent element, which must already be scrollable +export function ScrollerCurtains({ + orientation = ScrollerOrientation.Horizontal, + startCurtain = true, + endCurtain = true, +}: ScrollerCurtainsProps) { + const anchor = useRef(null!); + + const sync = useCallback( + (element: HTMLElement) => { + for (const axis of ORIENTATION_AXES[orientation]) { + const dimension = DIMENSIONS[axis]; + const clientSize = element[dimension.clientSize]; + const position = element[dimension.scroll]; + const overflow = element[dimension.scrollSize] - clientSize; + const fadeDistance = Math.min( + MAX_FADE_DISTANCE, + clientSize * FADE_RATIO, + ); + + const start = + !startCurtain || overflow <= 0 + ? 0 + : clamp(position / fadeDistance, 0, 1); + const end = + !endCurtain || overflow <= 0 + ? 0 + : clamp((overflow - position) / fadeDistance, 0, 1); + + element.style.setProperty( + `--scroller-curtains-${axis}-${Side.Start}`, + `${start}`, + ); + element.style.setProperty( + `--scroller-curtains-${axis}-${Side.End}`, + `${end}`, + ); + } + }, + [orientation, startCurtain, endCurtain], + ); + + useEffect(() => { + let element = anchor.current.parentElement!; + + // Skips boxless wrappers like astro-island to reach the actual scroller + while (getComputedStyle(element).display === "contents") { + element = element.parentElement!; + } + + const handleSync = () => sync(element); + const resizeObserver = new ResizeObserver(handleSync); + + // Tracks children as they mount and unmount instead of re-subscribing every render + const mutationObserver = new MutationObserver((records) => { + for (const record of records) { + record.removedNodes.forEach((node) => { + if (node instanceof Element) resizeObserver.unobserve(node); + }); + record.addedNodes.forEach((node) => { + if (node instanceof Element) resizeObserver.observe(node); + }); + } + }); + + element.classList.add(styles.target); + resizeObserver.observe(element); + Array.from(element.children).forEach((child) => + resizeObserver.observe(child), + ); + mutationObserver.observe(element, { childList: true }); + element.addEventListener("scroll", handleSync, { passive: true }); + + handleSync(); + + return () => { + resizeObserver.disconnect(); + mutationObserver.disconnect(); + element.removeEventListener("scroll", handleSync); + element.classList.remove(styles.target); + + for (const axis of Object.values(Axis)) { + for (const side of Object.values(Side)) { + element.style.removeProperty(`--scroller-curtains-${axis}-${side}`); + } + } + }; + }, [sync]); + + return ; +} diff --git a/packages/website/src/components/Section/index.module.css b/packages/website/src/components/Section/index.module.css index e939431b8..0a07646d6 100644 --- a/packages/website/src/components/Section/index.module.css +++ b/packages/website/src/components/Section/index.module.css @@ -19,7 +19,6 @@ } &[data-overflow="true"] { - padding-right: 0; overflow-x: auto; } } diff --git a/packages/website/src/components/TankCard/index.module.css b/packages/website/src/components/TankCard/index.module.css index 3fa440967..5d22584ec 100644 --- a/packages/website/src/components/TankCard/index.module.css +++ b/packages/website/src/components/TankCard/index.module.css @@ -1,4 +1,6 @@ .wrapper { + min-width: var(--min-tank-card-size); + --image-height: 6rem; &[data-compact="true"] { diff --git a/packages/website/src/components/TankCard/index.tsx b/packages/website/src/components/TankCard/index.tsx index cd71fc0b6..a484a47f6 100644 --- a/packages/website/src/components/TankCard/index.tsx +++ b/packages/website/src/components/TankCard/index.tsx @@ -11,7 +11,7 @@ import { ClassIcon } from "../ClassIcon"; import { LazyImage } from "../LazyImage"; import { LinkI18nWrapper } from "../LinkI18nWrapper"; import { TankCardSkeleton } from "../TankCardSkeleton"; -import { MAX_RECENTLY_VIEWED } from "../TankSearch/constants"; +import { MAX_RECENTLY_VIEWED } from "../TankSearchResults/constants"; import { Text, type TextProps } from "../Text"; import styles from "./index.module.css"; diff --git a/packages/website/src/components/TankCardWrapper/index.module.css b/packages/website/src/components/TankCardWrapper/index.module.css index a6da1c780..7c3debbf4 100644 --- a/packages/website/src/components/TankCardWrapper/index.module.css +++ b/packages/website/src/components/TankCardWrapper/index.module.css @@ -1,13 +1,15 @@ .wrapper { - --min-size: 10rem; + --min-tank-card-size: 10rem; + + &[data-compact="true"] { + --min-tank-card-size: 7rem; + gap: var(--space-4); + } display: grid; gap: var(--space-5) var(--space-4); - grid-template-columns: repeat(auto-fill, minmax(var(--min-size), 1fr)); - - &[data-compact="true"] { - --min-size: 7rem; - - gap: var(--space-4); - } + grid-template-columns: repeat( + auto-fill, + minmax(var(--min-tank-card-size), 1fr) + ); } diff --git a/packages/website/src/components/TankSearch/constants.tsx b/packages/website/src/components/TankSearch/constants.tsx deleted file mode 100644 index 8d51acff4..000000000 --- a/packages/website/src/components/TankSearch/constants.tsx +++ /dev/null @@ -1 +0,0 @@ -export const MAX_RECENTLY_VIEWED = 14; diff --git a/packages/website/src/components/TankSearch/index.tsx b/packages/website/src/components/TankSearch/index.tsx deleted file mode 100644 index 0561143b4..000000000 --- a/packages/website/src/components/TankSearch/index.tsx +++ /dev/null @@ -1,311 +0,0 @@ -import { metaSortTank, SEARCH_KEYS } 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"; -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 { useCharacteristicRenderer } from "../../hooks/useCharacteristicRenderer"; -import { computeDefaultCharacteristics } from "../../hooks/useCharacteristics"; -import { useLocale } from "../../hooks/useLocale"; -import { useStrings } from "../../hooks/useStrings"; -import { App } from "../../stores/app"; -import { TankFilters } from "../../stores/tankFilters"; -import { - SortDirection, - TankopediaPersistent, -} from "../../stores/tankopediaPersistent"; -import { TankSort } from "../../stores/tankopediaSort"; -import { sortableCharacteristics } from "../../tankopedia/sortableCharacteristics"; -import { - CharacteristicType, - type ComputedCharacteristics, -} from "../../types/characteristics"; -import type { MaybeSkeletonComponentProps } from "../../types/maybeSkeletonComponentProps"; -import { Callout } from "../Callout"; -import { ExperimentIcon } from "../ExperimentIcon"; -import { IncrementalLoader } from "../IncrementalLoader"; -import { Link } from "../Link"; -import { RecentlyViewedTanks } from "../RecentlyViewedTanks"; -import { TankCard } from "../TankCard"; -import { TankCardWrapper } from "../TankCardWrapper"; -import { TankSearchBar } from "../TankSearchBar"; -import { TankSearchFilters } from "../TankSearchFilters"; -import { TankSearchNoResults } from "../TankSearchNoResults"; -import { Text } from "../Text"; -import { MAX_RECENTLY_VIEWED } from "./constants"; -import styles from "./index.module.css"; - -export type TankSearchProps = MaybeSkeletonComponentProps & { - compact?: boolean; - onSelect?: (tank: TankDefinition) => void; - onSelectAll?: (tanks: TankDefinition[]) => void; -}; - -// TODO: across the project, remove "definitions" -const [gameDefinitions, tankDefinitions, tankNames] = await Promise.all([ - api.game(), - api.tanks(), - awaitableTankNames, -]); -const tankDefinitionsArray = Object.values(tankDefinitions.tanks); - -// TODO: add compact mode -export const TankSearch = memo( - ({ compact, onSelect, onSelectAll, skeleton }) => { - const strings = useStrings(); - const locale = useLocale(); - - const wargaming = App.use((state) => state.logins.wargaming); - - const tankopediaSort = TankSort.use(); - const tankFilters = TankFilters.use(); - - const defaultCharacteristics = useMemo(() => { - const computed = new Map(); - - for (const tank of tankDefinitionsArray) { - computed.set(tank.id, computeDefaultCharacteristics(tank)); - } - - return computed; - }, []); - - const filtered = usePromise( - async () => { - let filtered = tankDefinitionsArray; - - if (tankopediaSort.by !== null) { - filtered = filtered.filter((tank) => - defaultCharacteristics.get(tank.id)!.has(tankopediaSort.by!), - ); - } - - 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]); - } - - return filtered; - }, - // react-promise-suspense has awful type annotations - [tankFilters, tankopediaSort.by] as any, - ); - - const sorted = useMemo(() => { - if (tankFilters.search !== null) return filtered; - - if ( - tankopediaSort.by === null || - !sortableCharacteristics.has(tankopediaSort.by) - ) { - const byMeta = metaSortTank([...filtered], gameDefinitions); - - return tankopediaSort.direction === SortDirection.Ascending - ? byMeta - : byMeta.reverse(); - } - - const values = new Map(); - const by = tankopediaSort.by; - const characteristic = characteristics[by]; - - for (const tank of filtered) { - const computedCharacteristics = defaultCharacteristics.get(tank.id)!; - if (!computedCharacteristics.has(by)) { - values.set(tank.id, undefined); - continue; - } - - const value = computedCharacteristics.get(by); - - let sortValue: number | undefined; - - switch (characteristic.type) { - case CharacteristicType.Number: { - sortValue = value as number; - - break; - } - - case CharacteristicType.Interval: { - const [low, high] = value as Vector2Tuple; - - sortValue = (low + high) / 2; - - break; - } - - case CharacteristicType.NumberList: { - const values = value as number[]; - if (values.length === 0) { - sortValue = undefined; - break; - } - - let sum = 0; - - for (const item of values) { - sum += item; - } - - sortValue = sum / values.length; - - break; - } - - default: - throw new Error( - `Unsortable characteristic type ${characteristic.type}`, - ); - } - - values.set(tank.id, sortValue); - } - - const direction = - tankopediaSort.direction === SortDirection.Ascending ? 1 : -1; - - return [...filtered].sort((a, b) => { - const aValue = values.get(a.id); - const bValue = values.get(b.id); - - if (aValue === undefined) return bValue === undefined ? 0 : 1; - if (bValue === undefined) return -1; - - if (aValue === bValue) return 0; - - return (aValue < bValue ? -1 : 1) * direction; - }); - }, [tankFilters, tankopediaSort]); - - const sortConfig = - tankopediaSort.by !== null && - sortableCharacteristics.has(tankopediaSort.by) - ? tankopediaSort.by - : undefined; - - const renderCharacteristic = useCharacteristicRenderer(); - - const data = sorted.map((tank) => { - const tankDefaultCharacteristics = defaultCharacteristics.get(tank.id)!; - const shouldRenderDiscriminator = - tankopediaSort.by !== null && - tankDefaultCharacteristics.has(tankopediaSort.by); - - return { - key: `${tank.id}`, - tank, - discriminator: shouldRenderDiscriminator - ? renderCharacteristic( - tankDefaultCharacteristics.get(tankopediaSort.by!)!, - characteristicRenderConfigs.get(tankopediaSort.by!)!, - ) - : null, - onTankSelect: onSelect, - }; - }); - - return ( -
- - - {!tankFilters.search && } - - {!skeleton && !compact && } - - {sorted.length > 0 && ( -
- - {sorted.length === 1 - ? strings.website.common.tank_search.count_singular - : literals(strings.website.common.tank_search.count_plural, { - count: sorted.length.toLocaleString(locale), - })} - - {sortConfig && ( - <> - {" - "} - {literals(strings.website.common.tank_search.sorting_by, { - name: ( - strings.tanks.characteristics.items as Record< - string, - string - > - )[sortConfig], - })} - ,{" "} - { - strings.website.common.tank_search.sort.direction[ - tankopediaSort.direction - ] - } - - )} - - {onSelectAll && ( - <> - {" "} - { - event.preventDefault(); - onSelectAll(sorted); - TankopediaPersistent.mutate((draft) => { - draft.recentlyViewed = uniq([ - ...sorted.map(({ id }) => id), - ...draft.recentlyViewed, - ]) - .filter((id) => id in tankDefinitions.tanks) - .slice(0, MAX_RECENTLY_VIEWED); - }); - }} - > - {strings.website.common.tank_search.select_all} - - - )} - -
- )} - - {tankFilters.showTesting && !tankFilters.showNonTesting && ( -
- - - {strings.website.common.warnings.test} - -
- )} - - - - - - {!skeleton && sorted.length === 0 && ( - - )} -
- ); - }, -); diff --git a/packages/website/src/components/TankSearchBar/index.tsx b/packages/website/src/components/TankSearchBar/index.tsx index 10b927549..e6ecdb3e3 100644 --- a/packages/website/src/components/TankSearchBar/index.tsx +++ b/packages/website/src/components/TankSearchBar/index.tsx @@ -6,22 +6,20 @@ import { useEffect, useRef, } from "react"; +import { withLocale } from "../../hocs/withLocale"; import { useStrings } from "../../hooks/useStrings"; import { TankFilters } from "../../stores/tankFilters"; -import type { MaybeSkeletonComponentProps } from "../../types/maybeSkeletonComponentProps"; import { TankSearchSort } from "../TankSearchSort"; import { TextField } from "../TextField"; -type SearchBarProps = MaybeSkeletonComponentProps & { +type SearchBarProps = { topResult?: TankDefinition; onSelect?: (tank: TankDefinition) => void; }; -export function TankSearchBar({ - topResult, - skeleton, - onSelect, -}: SearchBarProps) { +export const TankSearchBar = withLocale(Content); + +function Content({ topResult, onSelect }: SearchBarProps) { const strings = useStrings(); const search = TankFilters.use((state) => state.search); @@ -56,13 +54,12 @@ export function TankSearchBar({ window.location.href = `/tanks/${topResult.slug}`; } }, - [topResult] + [topResult], ); return (
@@ -310,7 +313,10 @@ function NationsFilter() {
@@ -609,7 +615,9 @@ function IndividualShellFilter({ )} @@ -934,7 +942,9 @@ function ProvisionsFilter() { {unwrap(provisionDefinition.name!)} - + ); })} diff --git a/packages/website/src/components/TankSearchResults/constants.tsx b/packages/website/src/components/TankSearchResults/constants.tsx new file mode 100644 index 000000000..408238d67 --- /dev/null +++ b/packages/website/src/components/TankSearchResults/constants.tsx @@ -0,0 +1 @@ +export const MAX_RECENTLY_VIEWED = 28; diff --git a/packages/website/src/components/TankSearch/index.module.css b/packages/website/src/components/TankSearchResults/index.module.css similarity index 100% rename from packages/website/src/components/TankSearch/index.module.css rename to packages/website/src/components/TankSearchResults/index.module.css diff --git a/packages/website/src/components/TankSearchResults/index.tsx b/packages/website/src/components/TankSearchResults/index.tsx new file mode 100644 index 000000000..b9ed4e9e9 --- /dev/null +++ b/packages/website/src/components/TankSearchResults/index.tsx @@ -0,0 +1,296 @@ +import { metaSortTank, SEARCH_KEYS } 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"; +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 { withLocale } from "../../hocs/withLocale"; +import { useCharacteristicRenderer } from "../../hooks/useCharacteristicRenderer"; +import { computeDefaultCharacteristics } from "../../hooks/useCharacteristics"; +import { useLocale } from "../../hooks/useLocale"; +import { useStrings } from "../../hooks/useStrings"; +import { App } from "../../stores/app"; +import { TankFilters } from "../../stores/tankFilters"; +import { + SortDirection, + TankopediaPersistent, +} from "../../stores/tankopediaPersistent"; +import { TankSort } from "../../stores/tankopediaSort"; +import { sortableCharacteristics } from "../../tankopedia/sortableCharacteristics"; +import { + CharacteristicType, + type ComputedCharacteristics, +} from "../../types/characteristics"; +import { Callout } from "../Callout"; +import { ExperimentIcon } from "../ExperimentIcon"; +import { IncrementalLoader } from "../IncrementalLoader"; +import { Link } from "../Link"; +import { TankCard } from "../TankCard"; +import { TankCardWrapper } from "../TankCardWrapper"; +import { TankSearchNoResults } from "../TankSearchNoResults"; +import { Text } from "../Text"; +import { MAX_RECENTLY_VIEWED } from "./constants"; +import styles from "./index.module.css"; + +export type TankSearchResultsProps = { + compact?: boolean; + onSelect?: (tank: TankDefinition) => void; + onSelectAll?: (tanks: TankDefinition[]) => void; +}; + +// TODO: across the project, remove "definitions" +const [gameDefinitions, tankDefinitions, tankNames] = await Promise.all([ + api.game(), + api.tanks(), + awaitableTankNames, +]); +const tankDefinitionsArray = Object.values(tankDefinitions.tanks); + +// TODO: add compact mode +export const TankSearchResults = withLocale( + memo(Content), +); + +function Content({ onSelect, onSelectAll }: TankSearchResultsProps) { + const strings = useStrings(); + const locale = useLocale(); + + const wargaming = App.use((state) => state.logins.wargaming); + + const tankopediaSort = TankSort.use(); + const tankFilters = TankFilters.use(); + + const defaultCharacteristics = useMemo(() => { + const computed = new Map(); + + for (const tank of tankDefinitionsArray) { + computed.set(tank.id, computeDefaultCharacteristics(tank)); + } + + return computed; + }, []); + + const filtered = usePromise( + async () => { + let filtered = tankDefinitionsArray; + + if (tankopediaSort.by !== null) { + filtered = filtered.filter((tank) => + defaultCharacteristics.get(tank.id)!.has(tankopediaSort.by!), + ); + } + + 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]); + } + + return filtered; + }, + // react-promise-suspense has awful type annotations + [tankFilters, tankopediaSort.by] as any, + ); + + const sorted = useMemo(() => { + if (tankFilters.search !== null) return filtered; + + if ( + tankopediaSort.by === null || + !sortableCharacteristics.has(tankopediaSort.by) + ) { + const byMeta = metaSortTank([...filtered], gameDefinitions); + + return tankopediaSort.direction === SortDirection.Ascending + ? byMeta + : byMeta.reverse(); + } + + const values = new Map(); + const by = tankopediaSort.by; + const characteristic = characteristics[by]; + + for (const tank of filtered) { + const computedCharacteristics = defaultCharacteristics.get(tank.id)!; + if (!computedCharacteristics.has(by)) { + values.set(tank.id, undefined); + continue; + } + + const value = computedCharacteristics.get(by); + + let sortValue: number | undefined; + + switch (characteristic.type) { + case CharacteristicType.Number: { + sortValue = value as number; + + break; + } + + case CharacteristicType.Interval: { + const [low, high] = value as Vector2Tuple; + + sortValue = (low + high) / 2; + + break; + } + + case CharacteristicType.NumberList: { + const values = value as number[]; + if (values.length === 0) { + sortValue = undefined; + break; + } + + let sum = 0; + + for (const item of values) { + sum += item; + } + + sortValue = sum / values.length; + + break; + } + + default: + throw new Error( + `Unsortable characteristic type ${characteristic.type}`, + ); + } + + values.set(tank.id, sortValue); + } + + const direction = + tankopediaSort.direction === SortDirection.Ascending ? 1 : -1; + + return [...filtered].sort((a, b) => { + const aValue = values.get(a.id); + const bValue = values.get(b.id); + + if (aValue === undefined) return bValue === undefined ? 0 : 1; + if (bValue === undefined) return -1; + + if (aValue === bValue) return 0; + + return (aValue < bValue ? -1 : 1) * direction; + }); + }, [tankFilters, tankopediaSort]); + + const sortConfig = + tankopediaSort.by !== null && sortableCharacteristics.has(tankopediaSort.by) + ? tankopediaSort.by + : undefined; + + const renderCharacteristic = useCharacteristicRenderer(); + + const data = sorted.map((tank) => { + const tankDefaultCharacteristics = defaultCharacteristics.get(tank.id)!; + const shouldRenderDiscriminator = + tankopediaSort.by !== null && + tankDefaultCharacteristics.has(tankopediaSort.by); + + return { + key: `${tank.id}`, + tank, + discriminator: shouldRenderDiscriminator + ? renderCharacteristic( + tankDefaultCharacteristics.get(tankopediaSort.by!)!, + characteristicRenderConfigs.get(tankopediaSort.by!)!, + ) + : null, + onTankSelect: onSelect, + }; + }); + + return ( +
+ {sorted.length > 0 && ( +
+ + {sorted.length === 1 + ? strings.website.common.tank_search.count_singular + : literals(strings.website.common.tank_search.count_plural, { + count: sorted.length.toLocaleString(locale), + })} + + {sortConfig && ( + <> + {" - "} + {literals(strings.website.common.tank_search.sorting_by, { + name: ( + strings.tanks.characteristics.items as Record< + string, + string + > + )[sortConfig], + })} + ,{" "} + { + strings.website.common.tank_search.sort.direction[ + tankopediaSort.direction + ] + } + + )} + + {onSelectAll && ( + <> + {" "} + { + event.preventDefault(); + onSelectAll(sorted); + TankopediaPersistent.mutate((draft) => { + draft.recentlyViewed = uniq([ + ...sorted.map(({ id }) => id), + ...draft.recentlyViewed, + ]) + .filter((id) => id in tankDefinitions.tanks) + .slice(0, MAX_RECENTLY_VIEWED); + }); + }} + > + {strings.website.common.tank_search.select_all} + + + )} + +
+ )} + + {tankFilters.showTesting && !tankFilters.showNonTesting && ( +
+ + + {strings.website.common.warnings.test} + +
+ )} + + + + + + {sorted.length === 0 && } +
+ ); +} diff --git a/packages/website/src/components/Tankopedia/CalloutsSection.tsx b/packages/website/src/components/Tankopedia/CalloutsSection.tsx index 49a410380..8a11759e9 100644 --- a/packages/website/src/components/Tankopedia/CalloutsSection.tsx +++ b/packages/website/src/components/Tankopedia/CalloutsSection.tsx @@ -8,7 +8,7 @@ import { Duel } from "../../stores/duel"; import { TankopediaPersistent } from "../../stores/tankopediaPersistent"; import { AesonPlug } from "../AesonPlug"; import { ExperimentIcon } from "../ExperimentIcon"; -import { MAX_RECENTLY_VIEWED } from "../TankSearch/constants"; +import { MAX_RECENTLY_VIEWED } from "../TankSearchResults/constants"; const tankDefinitions = await api.tanks(); diff --git a/packages/website/src/layouts/BaseLayout.astro b/packages/website/src/layouts/BaseLayout.astro index 0608adbc7..f9a18c163 100644 --- a/packages/website/src/layouts/BaseLayout.astro +++ b/packages/website/src/layouts/BaseLayout.astro @@ -40,6 +40,9 @@ const { locale, accent, solidNavbar = false, tool } = Astro.props; interpolate-size: allow-keywords; background-color: var(--gray-1); + } + + :global(*) { scrollbar-width: none; &::-webkit-scrollbar { diff --git a/packages/website/src/pages/[...chunk_default]/[...locale]/tanks/_index.tsx b/packages/website/src/pages/[...chunk_default]/[...locale]/tanks/_index.tsx deleted file mode 100644 index eb0b85432..000000000 --- a/packages/website/src/pages/[...chunk_default]/[...locale]/tanks/_index.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import { TankSearch } from "../../../../components/TankSearch"; -import { withLocale } from "../../../../hocs/withLocale"; -import type { MaybeSkeletonComponentProps } from "../../../../types/maybeSkeletonComponentProps"; - -export const Page = withLocale(({ skeleton }) => ( - -)); diff --git a/packages/website/src/pages/[...chunk_default]/[...locale]/tanks/index.astro b/packages/website/src/pages/[...chunk_default]/[...locale]/tanks/index.astro index d5bd1f892..8b1e8d196 100644 --- a/packages/website/src/pages/[...chunk_default]/[...locale]/tanks/index.astro +++ b/packages/website/src/pages/[...chunk_default]/[...locale]/tanks/index.astro @@ -1,10 +1,14 @@ --- import ColorScale from "../../../../components/ColorScale.astro"; +import { RecentlyViewedTanks } from "../../../../components/RecentlyViewedTanks"; +import { ScrollerCurtains } from "../../../../components/ScrollerCurtains"; import { Section } from "../../../../components/Section"; +import { TankSearchBar } from "../../../../components/TankSearchBar"; +import { TankSearchFilters } from "../../../../components/TankSearchFilters"; +import { TankSearchResults } from "../../../../components/TankSearchResults"; import ToolBanner from "../../../../components/ToolBanner.astro"; import { tools } from "../../../../constants/tools"; import BaseLayout from "../../../../layouts/BaseLayout.astro"; -import { Page } from "./_index"; export { getStaticPaths } from "../index.astro"; @@ -18,10 +22,21 @@ const { locale } = Astro.props; - +
+ + +
+
- - - + +
+ +
+ + +
+ +
+