Skip to content

Commit 58fd405

Browse files
committed
Search other games if search produces no results
1 parent e100f84 commit 58fd405

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

src/components/Docs/ContentList.tsx

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
import React, { useContext, useState } from "react";
1+
import React, { useContext, useMemo, useState } from "react";
22
import { useNavigate } from "react-router-dom";
33
import { styled } from "@linaria/react";
44
import { ContentWrapper, ListItem, TextMessage } from "../layout/Content";
55
import { LazyList, ScrollableList } from "../Lists";
6-
import { useFilteredData } from "./utils/filtering";
6+
import { useFilteredData, useParsedSearch, doSearch } from "./utils/filtering";
77
import { SchemaClassView } from "./SchemaClass";
88
import { SchemaEnumView } from "./SchemaEnum";
99
import { ClassTree } from "./ClassTree";
1010
import { Declaration } from "./api";
11-
import { DeclarationsContext, declarationKey } from "./DeclarationsContext";
12-
import { GAMES } from "../../games";
11+
import {
12+
DeclarationsContext,
13+
DeclarationsContextType,
14+
declarationKey,
15+
} from "./DeclarationsContext";
16+
import { GAMES, GameId } from "../../games";
1317

1418
const CardBlock = styled.div`
1519
max-width: 560px;
@@ -159,6 +163,65 @@ const OffsetsNote = styled.div`
159163
padding: 8px 4px;
160164
`;
161165

166+
const OtherGameHeader = styled.div`
167+
display: flex;
168+
align-items: center;
169+
gap: 6px;
170+
padding: 12px 0 4px;
171+
font-size: 15px;
172+
color: var(--text-dim);
173+
174+
svg {
175+
width: 18px;
176+
height: 18px;
177+
border-radius: 3px;
178+
}
179+
`;
180+
181+
function OtherGamesResults() {
182+
const ctx = useContext(DeclarationsContext);
183+
const parsed = useParsedSearch();
184+
185+
const gameResults = useMemo(() => {
186+
const result: { gameId: GameId; declarations: Declaration[] }[] = [];
187+
for (const [gameId, lookup] of ctx.otherGamesLookup) {
188+
if (gameId === ctx.game) continue;
189+
const decls = Array.from(lookup.values());
190+
const found = doSearch(decls, parsed);
191+
if (found.length > 0) {
192+
result.push({ gameId, declarations: found });
193+
}
194+
}
195+
return result;
196+
}, [ctx.game, ctx.otherGamesLookup, parsed]);
197+
198+
if (gameResults.length === 0) return null;
199+
200+
return (
201+
<>
202+
{gameResults.map(({ gameId, declarations }) => {
203+
const gameInfo = GAMES.find((g) => g.id === gameId);
204+
const overrideCtx: DeclarationsContextType = {
205+
...ctx,
206+
game: gameId,
207+
root: `/${gameId}`,
208+
declarations,
209+
};
210+
return (
211+
<React.Fragment key={gameId}>
212+
<OtherGameHeader>
213+
{gameInfo?.icon} {gameInfo?.name}
214+
</OtherGameHeader>
215+
<DeclarationsContext.Provider value={overrideCtx}>
216+
<LazyList data={declarations} render={renderItem} />
217+
</DeclarationsContext.Provider>
218+
</React.Fragment>
219+
);
220+
})}
221+
</>
222+
);
223+
}
224+
162225
function renderItem(declaration: Declaration) {
163226
let children: React.JSX.Element;
164227
switch (declaration.kind) {
@@ -189,7 +252,10 @@ export function ContentList() {
189252
<ScrollableList data={data} render={renderItem} />
190253
)
191254
) : isSearching ? (
192-
<TextMessage>No results found</TextMessage>
255+
<>
256+
<TextMessage>No results found</TextMessage>
257+
<OtherGamesResults />
258+
</>
193259
) : error ? (
194260
<TextMessage>{`Failed to load schemas: ${error}`}</TextMessage>
195261
) : loading ? (

src/components/Docs/utils/filtering.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export function filterItems<T extends HasNameAndMetadata>(
160160
};
161161
}
162162

163-
function doSearch(declarations: api.Declaration[], parsed: ParsedSearch): api.Declaration[] {
163+
export function doSearch(declarations: api.Declaration[], parsed: ParsedSearch): api.Declaration[] {
164164
const { nameWords, moduleWords, offsets: offsetSet, metadataKeys, metadataValues } = parsed;
165165

166166
function filterModule(declaration: api.Declaration): boolean {

src/components/Search/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ function SearchTagPopup({
233233
activeIndex,
234234
onSelect,
235235
}: {
236-
tags: (typeof SEARCH_TAGS)[number][];
236+
tags: readonly (typeof SEARCH_TAGS)[number][];
237237
activeIndex: number;
238238
onSelect: (tag: string) => void;
239239
}) {

0 commit comments

Comments
 (0)