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 */
|
/* Initial state */
|
||||||
const state = {
|
const state = {
|
||||||
tabs: [],
|
tabs: [],
|
||||||
|
results: [],
|
||||||
|
query: '',
|
||||||
}
|
}
|
||||||
|
|
||||||
/* */
|
/* */
|
||||||
|
|
||||||
const shapeTabs = (tabs) => tabs
|
const addLabel = (tabs) => (tab) => ({
|
||||||
.map((tab) => ({
|
|
||||||
label: labels.id2label(tab.id, tabs),
|
label: labels.id2label(tab.id, tabs),
|
||||||
...tab,
|
...tab,
|
||||||
}))
|
})
|
||||||
.map((tab) => [
|
|
||||||
|
const pickFields = (tab) => [
|
||||||
'favIconUrl',
|
'favIconUrl',
|
||||||
'id',
|
'id',
|
||||||
'label',
|
'label',
|
||||||
'title',
|
'title',
|
||||||
'url',
|
'url',
|
||||||
].reduce((acc, x) => (acc[x] = tab[x], acc), {}))
|
].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(addLabel(tabs))
|
||||||
|
.map(pickFields)
|
||||||
|
.map(addDisplayName)
|
||||||
|
|
||||||
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()
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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())
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue