aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2023-06-14 11:52:54 +0200
committerSimon Ser <contact@emersion.fr>2023-06-14 11:52:54 +0200
commit3a95fd5ba40dcb72d21c40f28ea64b02e93d289e (patch)
tree084f748c134b021add8a7a834382a35d89eb5e92
parent44a064274df432f8df7573de830b61c82aba606a (diff)
downloadgamja-3a95fd5ba40dcb72d21c40f28ea64b02e93d289e.tar.gz
gamja-3a95fd5ba40dcb72d21c40f28ea64b02e93d289e.zip
components/switcher-form: match topics and realnames
-rw-r--r--components/switcher-form.js42
1 files changed, 36 insertions, 6 deletions
diff --git a/components/switcher-form.js b/components/switcher-form.js
index 8838191..d1b706c 100644
--- a/components/switcher-form.js
+++ b/components/switcher-form.js
@@ -1,5 +1,6 @@
import { html, Component } from "../lib/index.js";
import { BufferType, getBufferURL, getServerName } from "../state.js";
+import * as irc from "../lib/irc.js";
class SwitcherItem extends Component {
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 {
state = {
query: "",
@@ -51,19 +72,28 @@ export default class SwitcherForm extends Component {
let query = this.state.query.toLowerCase();
let l = [];
+ let scores = new Map();
for (let buf of this.props.buffers.values()) {
if (buf.type === BufferType.SERVER) {
continue;
}
- if (query !== "" && !buf.name.toLowerCase().includes(query)) {
- continue;
+ let score = 0;
+ 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);
- 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) {