2022-09-11 13:42:41 -04:00
|
|
|
import {LitElement, html, unsafeHTML} from './lit-all.min.js';
|
|
|
|
import * as tfrpc from '/static/tfrpc.js';
|
|
|
|
import {styles} from './tf-styles.js';
|
|
|
|
|
|
|
|
class TfTabSearchElement extends LitElement {
|
|
|
|
static get properties() {
|
|
|
|
return {
|
|
|
|
whoami: {type: String},
|
|
|
|
users: {type: Object},
|
|
|
|
following: {type: Array},
|
2022-09-14 20:16:37 -04:00
|
|
|
query: {type: String},
|
2023-06-14 12:39:08 -04:00
|
|
|
expanded: {type: Object},
|
2023-03-29 18:02:12 -04:00
|
|
|
};
|
2022-09-11 13:42:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static styles = styles;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
let self = this;
|
|
|
|
this.whoami = null;
|
|
|
|
this.users = {};
|
|
|
|
this.following = [];
|
2023-06-14 12:39:08 -04:00
|
|
|
this.expanded = {};
|
2022-09-11 13:42:41 -04:00
|
|
|
}
|
|
|
|
|
2022-09-14 20:16:37 -04:00
|
|
|
async search(query) {
|
2022-09-11 13:42:41 -04:00
|
|
|
console.log('Searching...', this.whoami, query);
|
2022-09-14 20:16:37 -04:00
|
|
|
let search = this.renderRoot.getElementById('search');
|
|
|
|
if (search) {
|
|
|
|
search.value = query;
|
|
|
|
search.focus();
|
|
|
|
search.select();
|
|
|
|
}
|
|
|
|
await tfrpc.rpc.setHash('#q=' + encodeURIComponent(query));
|
2022-09-11 13:42:41 -04:00
|
|
|
let results = await tfrpc.rpc.query(
|
|
|
|
`
|
|
|
|
SELECT messages.*
|
|
|
|
FROM messages_fts(?)
|
|
|
|
JOIN messages ON messages.rowid = messages_fts.rowid
|
|
|
|
JOIN json_each(?) AS following ON messages.author = following.value
|
|
|
|
ORDER BY timestamp DESC limit 100
|
|
|
|
`,
|
2022-09-14 19:33:57 -04:00
|
|
|
['"' + query.replace('"', '""') + '"', JSON.stringify(this.following)]
|
|
|
|
);
|
2022-09-11 13:42:41 -04:00
|
|
|
console.log('Done.');
|
2022-09-14 20:16:37 -04:00
|
|
|
search = this.renderRoot.getElementById('search');
|
|
|
|
if (search) {
|
|
|
|
search.value = query;
|
|
|
|
search.focus();
|
|
|
|
search.select();
|
|
|
|
}
|
2022-09-11 13:42:41 -04:00
|
|
|
this.renderRoot.getElementById('news').messages = results;
|
|
|
|
}
|
|
|
|
|
|
|
|
search_keydown(event) {
|
|
|
|
if (event.keyCode == 13) {
|
2022-09-14 20:16:37 -04:00
|
|
|
this.query = this.renderRoot.getElementById('search').value;
|
2022-09-11 13:42:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:39:08 -04:00
|
|
|
on_expand(event) {
|
|
|
|
if (event.detail.expanded) {
|
|
|
|
let expand = {};
|
|
|
|
expand[event.detail.id] = true;
|
|
|
|
this.expanded = Object.assign({}, this.expanded, expand);
|
|
|
|
} else {
|
|
|
|
delete this.expanded[event.detail.id];
|
|
|
|
this.expanded = Object.assign({}, this.expanded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-11 13:42:41 -04:00
|
|
|
render() {
|
2022-09-14 20:16:37 -04:00
|
|
|
if (this.query !== this.last_query) {
|
2023-06-14 12:39:08 -04:00
|
|
|
this.last_query = this.query;
|
2022-09-14 20:16:37 -04:00
|
|
|
this.search(this.query);
|
|
|
|
}
|
|
|
|
let self = this;
|
2022-09-11 13:42:41 -04:00
|
|
|
return html`
|
2024-01-12 21:55:52 -05:00
|
|
|
<div style="display: flex; flex-direction: row; gap: 4px">
|
|
|
|
<input type="text" class="w3-input w3-dark-grey" id="search" value=${this.query} style="flex: 1" @keydown=${this.search_keydown}></input>
|
|
|
|
<button class="w3-button w3-dark-grey" @click=${(event) => self.search(self.renderRoot.getElementById('search').value)}>Search</button>
|
2022-09-14 20:16:37 -04:00
|
|
|
</div>
|
2023-06-14 12:39:08 -04:00
|
|
|
<tf-news id="news" whoami=${this.whoami} .messages=${this.messages} .users=${this.users} .expanded=${this.expanded} @tf-expand=${this.on_expand}></tf-news>
|
2022-09-11 13:42:41 -04:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define('tf-tab-search', TfTabSearchElement);
|