Skip to content

Commit 1993c76

Browse files
authored
Merge pull request #21 from AndyOGo/feat/filter-frames-by-full-text-search
feat: add full-text search
2 parents 6148654 + 6f27d4d commit 1993c76

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"devDependencies": {
1414
"@types/chrome": "^0.0.128",
1515
"@types/classnames": "^2.2.11",
16+
"@types/flexsearch": "^0.7.2",
1617
"@types/react": "^16.14.2",
1718
"@types/react-dom": "^16.9.10",
1819
"@types/react-fontawesome": "^1.6.4",
@@ -40,6 +41,7 @@
4041
},
4142
"dependencies": {
4243
"classnames": "^2.2.6",
44+
"flexsearch": "^0.7.21",
4345
"mobx": "^6.1.6",
4446
"mobx-react-lite": "^3.2.0",
4547
"react": "^16.14.0",

src/viewer/App.tsx

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import {
1919
} from "rsocket-core";
2020
import {Encoders} from "rsocket-core/RSocketEncoding";
2121
import Protocol from "devtools-protocol";
22-
import {action, makeObservable, observable} from "mobx";
22+
import {action, makeObservable, observable, computed} from "mobx";
2323
import {observer} from "mobx-react-lite";
24+
import {Index, IndexSearchResult} from "flexsearch";
2425
import WebSocketFrameReceivedEvent = Protocol.Network.WebSocketFrameReceivedEvent;
2526
import WebSocketFrameSentEvent = Protocol.Network.WebSocketFrameSentEvent;
2627
import WebSocketFrame = Protocol.Network.WebSocketFrame;
@@ -282,6 +283,7 @@ interface WsConnectionState {
282283
id: string,
283284
url?: string,
284285
frames: WsFrameState[];
286+
index: Index;
285287
activeFrame?: number
286288
}
287289

@@ -293,12 +295,16 @@ export class AppStateStore {
293295
issueWallTime?: number = undefined;
294296
connections = new Map<string, WsConnectionState>();
295297
activeConnection?: string = undefined;
298+
searchValue: string = "";
296299

297300
constructor(handlers: ChromeHandlers) {
298301
makeObservable(this, {
299302
connections: observable,
300303
activeConnection: observable,
301304
selectConnection: action.bound,
305+
search: action.bound,
306+
searchValue: observable,
307+
searchResult: computed,
302308
frameSent: action.bound,
303309
frameReceived: action.bound,
304310
webSocketCreated: action.bound,
@@ -320,6 +326,8 @@ export class AppStateStore {
320326
return;
321327
}
322328
connection.frames = []
329+
connection.index = new Index({tokenize: "full"})
330+
this.searchValue = ""
323331
}
324332

325333
selectFrame(id?: number) {
@@ -337,6 +345,24 @@ export class AppStateStore {
337345
this.activeConnection = value;
338346
}
339347

348+
search(value: string) {
349+
this.searchValue = value;
350+
}
351+
352+
get searchResult(): IndexSearchResult {
353+
if (!this.activeConnection) {
354+
return []
355+
}
356+
357+
const connection = this.connections.get(this.activeConnection);
358+
if (!connection) {
359+
return []
360+
}
361+
362+
const result = connection.index.search(this.searchValue);
363+
return result
364+
}
365+
340366
webSocketCreated(event: WebSocketCreatedEvent) {
341367
const {requestId, url} = event;
342368
if (this.connections.get(requestId)) {
@@ -347,6 +373,7 @@ export class AppStateStore {
347373
id: requestId,
348374
url: url,
349375
frames: [],
376+
index: new Index({tokenize: "full"}),
350377
activeFrame: undefined,
351378
});
352379
}
@@ -376,7 +403,9 @@ export class AppStateStore {
376403
frame.binary = stringToBuffer(response.payloadData);
377404
}
378405
const connection = this.ensureConnection(requestId);
406+
const rSocketFrame = tryDeserializeFrame(frame.payload);
379407
connection.frames.push(frame)
408+
connection.index.add(frame.id, (rSocketFrame as any)?.data ?? frame.text ?? frame.payload)
380409
this.activeConnection = requestId;
381410
}
382411
}
@@ -389,6 +418,7 @@ export class AppStateStore {
389418
const newConnection = {
390419
id: requestId,
391420
frames: [],
421+
index: new Index({tokenize: "full"}),
392422
activeFrame: undefined,
393423
}
394424
this.connections.set(requestId, newConnection);
@@ -405,7 +435,7 @@ export class AppStateStore {
405435
}
406436

407437
export const App = observer(({store}: { store: AppStateStore }) => {
408-
const {connections, activeConnection} = store;
438+
const {connections, activeConnection, searchValue, searchResult} = store;
409439
if (!activeConnection) {
410440
return <div>No active WebSocket connections</div>
411441
}
@@ -415,9 +445,10 @@ export const App = observer(({store}: { store: AppStateStore }) => {
415445
}
416446
const {frames, activeFrame} = connection;
417447
const active = frames.find(f => f.id === activeFrame);
448+
const filteredFrames = searchResult.length ? frames.filter((frame) => searchResult.includes(frame.id)) : frames;
418449

419-
const Row = ({ index, style, data }: ListChildComponentProps) => {
420-
const frame = data[index];
450+
const Row = ({ index, style }: ListChildComponentProps) => {
451+
const frame = filteredFrames[index];
421452

422453
return (
423454
<FrameEntry
@@ -450,12 +481,19 @@ export const App = observer(({store}: { store: AppStateStore }) => {
450481
</option>)
451482
}
452483
</select>
484+
<input
485+
type="text"
486+
placeholder="Search frame..."
487+
style={{width: "100%"}}
488+
value={searchValue}
489+
onChange={e => store.search(e.target.value)}
490+
/>
453491
</div>
454492

455493
<div className="frame-list">
456494
<AutoSizer disableWidth>
457495
{({ height }) => (
458-
<FixedSizeList height={height} width="100%" itemCount={frames.length} itemSize={18} innerElementType="ul">
496+
<FixedSizeList height={height} width="100%" itemCount={filteredFrames.length} itemSize={18} innerElementType="ul">
459497
{Row}
460498
</FixedSizeList>
461499
)}

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@
7373
resolved "https://registry.yarnpkg.com/@types/filewriter/-/filewriter-0.0.28.tgz#c054e8af4d9dd75db4e63abc76f885168714d4b3"
7474
integrity sha1-wFTor02d11205jq8dviFFocU1LM=
7575

76+
"@types/flexsearch@^0.7.2":
77+
version "0.7.2"
78+
resolved "https://registry.yarnpkg.com/@types/flexsearch/-/flexsearch-0.7.2.tgz#05d982d292e70fcb9fc59347a4a4f98c4ecd9e56"
79+
integrity sha512-Nq0CSpOCyUhaF7tAXSvMtoyBMPGlhNyF+uElhIrrgSiXDmX/bnn9jUX7Us3l81Hzowb9rcgNISke0Nj+3xhd3g==
80+
7681
"@types/glob@^7.1.1":
7782
version "7.1.3"
7883
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.3.tgz#e6ba80f36b7daad2c685acd9266382e68985c183"
@@ -1832,6 +1837,11 @@ find-up@^4.0.0:
18321837
locate-path "^5.0.0"
18331838
path-exists "^4.0.0"
18341839

1840+
flexsearch@^0.7.21:
1841+
version "0.7.21"
1842+
resolved "https://registry.yarnpkg.com/flexsearch/-/flexsearch-0.7.21.tgz#0f5ede3f2aae67ddc351efbe3b24b69d29e9d48b"
1843+
integrity sha512-W7cHV7Hrwjid6lWmy0IhsWDFQboWSng25U3VVywpHOTJnnAZNPScog67G+cVpeX9f7yDD21ih0WDrMMT+JoaYg==
1844+
18351845
flush-write-stream@^1.0.0:
18361846
version "1.1.1"
18371847
resolved "https://registry.yarnpkg.com/flush-write-stream/-/flush-write-stream-1.1.1.tgz#8dd7d873a1babc207d94ead0c2e0e44276ebf2e8"

0 commit comments

Comments
 (0)