pages/search: render results

This commit is contained in:
He4eT 2024-01-20 21:36:46 +01:00
commit ae2259b2ba
3 changed files with 52 additions and 16 deletions

View file

@ -0,0 +1,7 @@
const tabView = (tab) =>
`<button class='tab'>${tab.displayName}</button>`
export const updateSearchResults = (state) => {
const container = document.getElementById('searchResults')
container.innerHTML = state.results.map(tabView).join('')
}

View file

@ -7,33 +7,59 @@ export const init = ({
/* Initial state */ /* Initial state */
const state = { const state = {
tabs: [], tabs: [],
results: [],
query: '',
} }
/* */ /* */
const addLabel = (tabs) => (tab) => ({
label: labels.id2label(tab.id, tabs),
...tab,
})
const pickFields = (tab) => [
'favIconUrl',
'id',
'label',
'title',
'url',
].reduce((acc, x) => (acc[x] = tab[x], acc), {})
const addDisplayName = (tab) => ({
displayName: `
<span>${tab.label}</span>
<span>${tab.title} / ${tab.url}</span>`,
...tab,
})
const shapeTabs = (tabs) => tabs const shapeTabs = (tabs) => tabs
.map((tab) => ({ .map(addLabel(tabs))
label: labels.id2label(tab.id, tabs), .map(pickFields)
...tab, .map(addDisplayName)
}))
.map((tab) => [
'favIconUrl',
'id',
'label',
'title',
'url',
].reduce((acc, x) => (acc[x] = tab[x], acc), {}))
const fetchTabs = () => const fetchTabs = () =>
browserTabs.query({ currentWindow: true, active: false }) browserTabs.query({ currentWindow: true, active: false })
.then(shapeTabs) .then(shapeTabs)
.then((tabs) => tabs.reverse()) .then((tabs) => tabs.reverse())
.then((tabs) => void (state.tabs = tabs)) .then((tabs) => {
void (state.tabs = tabs)
void (state.results = tabs)
})
/* */ /* */
const updateResults = () => {
state.results = state.query.length === 0
? state.tabs
: []
}
/* */
const updateState = () => const updateState = () =>
fetchTabs() Promise.resolve()
.then(fetchTabs)
.then(updateResults)
.then(() => onStateUpdate(state)) .then(() => onStateUpdate(state))
updateState() updateState()
@ -53,6 +79,10 @@ export const init = ({
browserTabs.remove(id) browserTabs.remove(id)
.then(updateState) .then(updateState)
}, },
updateQuery(query) {
state.query = query
updateState()
},
}, },
} }
} }

View file

@ -1,8 +1,7 @@
import * as Store from './modules/store.js' import * as Store from './modules/store.js'
import * as dom from './modules/dom.js'
const store = Store.init({ const store = Store.init({
tabs: browser.tabs, tabs: browser.tabs,
onStateUpdate: console.log, onStateUpdate: dom.updateSearchResults,
}) })
console.log(store.getCurrentState())