mirror of
https://github.com/He4eT/tabswitcher.git
synced 2026-05-05 01:17:23 +00:00
pages/search: render results
This commit is contained in:
parent
58bd805ee3
commit
ae2259b2ba
3 changed files with 52 additions and 16 deletions
7
pages/search/modules/dom.js
Normal file
7
pages/search/modules/dom.js
Normal 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('')
|
||||
}
|
||||
|
|
@ -7,33 +7,59 @@ export const init = ({
|
|||
/* Initial state */
|
||||
const state = {
|
||||
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
|
||||
.map((tab) => ({
|
||||
label: labels.id2label(tab.id, tabs),
|
||||
...tab,
|
||||
}))
|
||||
.map((tab) => [
|
||||
'favIconUrl',
|
||||
'id',
|
||||
'label',
|
||||
'title',
|
||||
'url',
|
||||
].reduce((acc, x) => (acc[x] = tab[x], acc), {}))
|
||||
.map(addLabel(tabs))
|
||||
.map(pickFields)
|
||||
.map(addDisplayName)
|
||||
|
||||
const fetchTabs = () =>
|
||||
browserTabs.query({ currentWindow: true, active: false })
|
||||
.then(shapeTabs)
|
||||
.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 = () =>
|
||||
fetchTabs()
|
||||
Promise.resolve()
|
||||
.then(fetchTabs)
|
||||
.then(updateResults)
|
||||
.then(() => onStateUpdate(state))
|
||||
|
||||
updateState()
|
||||
|
|
@ -53,6 +79,10 @@ export const init = ({
|
|||
browserTabs.remove(id)
|
||||
.then(updateState)
|
||||
},
|
||||
updateQuery(query) {
|
||||
state.query = query
|
||||
updateState()
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue