Skip to content

Commit

Permalink
Merge pull request #208 from BrainMaestro/master
Browse files Browse the repository at this point in the history
Split plugins into categories
  • Loading branch information
KELiON authored Mar 4, 2017
2 parents c42f4b6 + 0ec91ec commit fbbc295
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions app/main/plugins/core/cerebro/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import loadPlugins from './loadPlugins'
import icon from '../icon.png'
import * as format from './format'
import { flow, map, partialRight, tap } from 'lodash/fp'
import { partition } from 'lodash'
import initializeAsync from './initializeAsync'

const toString = ({ name, description }) => [name, description].join(' ')
const categories = [
['Updates', plugin => plugin.isUpdateAvailable],
['Installed', plugin => plugin.isInstalled],
['Available', plugin => plugin.name],
]

const updatePlugin = (update, name) => {
loadPlugins().then(plugins => {
Expand All @@ -26,20 +32,42 @@ const updatePlugin = (update, name) => {
})
}

const pluginToResult = update => plugin => ({
icon,
id: plugin.name,
title: `${format.name(plugin.name)} (${format.version(plugin)})`,
subtitle: format.description(plugin.description),
onSelect: () => shell.openExternal(plugin.repo),
getPreview: () => (
<Preview
{...plugin}
key={plugin.name}
onComplete={() => updatePlugin(update, plugin.name)}
/>
)
})
const pluginToResult = update => plugin => {
if (typeof plugin === 'string') {
return { title: plugin }
}

return {
icon,
id: plugin.name,
title: `${format.name(plugin.name)} (${format.version(plugin)})`,
subtitle: format.description(plugin.description),
onSelect: () => shell.openExternal(plugin.repo),
getPreview: () => (
<Preview
{...plugin}
key={plugin.name}
onComplete={() => updatePlugin(update, plugin.name)}
/>
)
}
}

const categorize = (plugins, callback) => {
const result = []
let remainder = plugins

categories.forEach(category => {
const [title, filter] = category
const [matched, others] = partition(remainder, filter)
if (matched.length) result.push(title, ...matched)
remainder = others
})

plugins.splice(0, plugins.length)
plugins.push(...result)
callback()
}

const fn = ({ term, display, hide, update }) => {
const match = term.match(/^plugins?\s*(.+)?$/i)
Expand All @@ -51,8 +79,8 @@ const fn = ({ term, display, hide, update }) => {
})
loadPlugins().then(flow(
partialRight(search, [match[1], toString]),
tap(plugins => categorize(plugins, () => hide('loading'))),
map(pluginToResult(update)),
tap(() => hide('loading')),
display
))
}
Expand Down

0 comments on commit fbbc295

Please sign in to comment.