forked from CringeStudios/gamja
components/switcher-form: match topics and realnames
This commit is contained in:
parent
44a064274d
commit
3a95fd5ba4
@ -1,5 +1,6 @@
|
|||||||
import { html, Component } from "../lib/index.js";
|
import { html, Component } from "../lib/index.js";
|
||||||
import { BufferType, getBufferURL, getServerName } from "../state.js";
|
import { BufferType, getBufferURL, getServerName } from "../state.js";
|
||||||
|
import * as irc from "../lib/irc.js";
|
||||||
|
|
||||||
class SwitcherItem extends Component {
|
class SwitcherItem extends Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
@ -33,6 +34,26 @@ class SwitcherItem extends Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function matchString(s, query) {
|
||||||
|
return s.toLowerCase().includes(query) ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function matchBuffer(buf, server, query) {
|
||||||
|
let score = 2 * matchString(buf.name, query);
|
||||||
|
switch (buf.type) {
|
||||||
|
case BufferType.CHANNEL:
|
||||||
|
score += matchString(buf.topic || "", query);
|
||||||
|
break;
|
||||||
|
case BufferType.NICK:
|
||||||
|
let user = server.users.get(buf.name);
|
||||||
|
if (user && user.realname && irc.isMeaningfulRealname(user.realname, buf.name)) {
|
||||||
|
score += matchString(user.realname, query);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
export default class SwitcherForm extends Component {
|
export default class SwitcherForm extends Component {
|
||||||
state = {
|
state = {
|
||||||
query: "",
|
query: "",
|
||||||
@ -51,19 +72,28 @@ export default class SwitcherForm extends Component {
|
|||||||
let query = this.state.query.toLowerCase();
|
let query = this.state.query.toLowerCase();
|
||||||
|
|
||||||
let l = [];
|
let l = [];
|
||||||
|
let scores = new Map();
|
||||||
for (let buf of this.props.buffers.values()) {
|
for (let buf of this.props.buffers.values()) {
|
||||||
if (buf.type === BufferType.SERVER) {
|
if (buf.type === BufferType.SERVER) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (query !== "" && !buf.name.toLowerCase().includes(query)) {
|
let score = 0;
|
||||||
continue;
|
if (query !== "") {
|
||||||
|
let server = this.props.servers.get(buf.server);
|
||||||
|
score = matchBuffer(buf, server, query);
|
||||||
|
if (!score) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
scores.set(buf.id, score);
|
||||||
l.push(buf);
|
l.push(buf);
|
||||||
if (l.length >= 20) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return l;
|
|
||||||
|
l.sort((a, b) => {
|
||||||
|
return scores.get(b.id) - scores.get(a.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
return l.slice(0, 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleInput(event) {
|
handleInput(event) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user