Skip to content

Commit 8309795

Browse files
committed
Add support for installing plugin-hub plugins via website
This works only for logged in users as it is simply changing the config. Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent 420a7e3 commit 8309795

File tree

4 files changed

+120
-10
lines changed

4 files changed

+120
-10
lines changed

src/components/external-plugin.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ const ExternalPlugin = ({
99
support,
1010
imageUrl,
1111
installed,
12-
count
12+
internalName,
13+
count,
14+
update
1315
}) => (
1416
<div class="col-md-4 col-sm-6 col-xs-12 mb-2">
1517
<div class="card">
@@ -45,7 +47,21 @@ const ExternalPlugin = ({
4547
{numberWithCommas(count)}{' '}
4648
{count > 1 ? 'active installs' : 'active install'}
4749
</span>{' '}
48-
{installed && <span class="badge badge-success">installed</span>}
50+
{installed ? (
51+
<button
52+
class="badge badge-danger btn"
53+
onClick={() => update(installed, internalName)}
54+
>
55+
uninstall
56+
</button>
57+
) : (
58+
<button
59+
class="badge badge-success btn"
60+
onClick={() => update(installed, internalName)}
61+
>
62+
install
63+
</button>
64+
)}
4965
</p>
5066
)}
5167
<p class="card-text">

src/modules/config.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ const configNameFilters = [
1515
]
1616

1717
// Actions
18-
export const { fetchConfig, setConfig, changeAccount } = createActions(
18+
export const {
19+
fetchConfig,
20+
updateConfig,
21+
setConfig,
22+
addModifiedKey,
23+
changeAccount
24+
} = createActions(
1925
{
2026
FETCH_CONFIG: () => async (dispatch, getState) => {
2127
const version = getLatestRelease(getState())
@@ -51,9 +57,37 @@ export const { fetchConfig, setConfig, changeAccount } = createActions(
5157
}
5258

5359
return config
60+
},
61+
UPDATE_CONFIG: (key, value) => async (dispatch, getState) => {
62+
const version = getLatestRelease(getState())
63+
const uuid = getState().account.uuid
64+
65+
if (!uuid) {
66+
return {}
67+
}
68+
69+
if (value.length > 0) {
70+
await runeliteApi(`runelite-${version}/config/${key}`, {
71+
method: 'PUT',
72+
headers: {
73+
'RUNELITE-AUTH': uuid
74+
},
75+
body: value
76+
})
77+
} else {
78+
await runeliteApi(`runelite-${version}/config/${key}`, {
79+
method: 'DELETE',
80+
headers: {
81+
'RUNELITE-AUTH': uuid
82+
}
83+
})
84+
}
85+
86+
dispatch(addModifiedKey(key))
5487
}
5588
},
5689
'SET_CONFIG',
90+
'ADD_MODIFIED_KEY',
5791
'CHANGE_ACCOUNT'
5892
)
5993

@@ -67,17 +101,23 @@ export default handleActions(
67101
[changeAccount]: (state, { payload }) => ({
68102
...state,
69103
selectedAccount: payload
104+
}),
105+
[addModifiedKey]: (state, { payload }) => ({
106+
...state,
107+
modifiedKeys: [...new Set(state.modifiedKeys.concat([payload]))]
70108
})
71109
},
72110
{
73111
config: {},
112+
modifiedKeys: [],
74113
selectedAccount: ''
75114
}
76115
)
77116

78117
// Selectors
79118
export const getConfig = state => state.config.config
80119
export const getSelectedAccount = state => state.config.selectedAccount
120+
export const getModifiedKeys = state => state.config.modifiedKeys
81121

82122
export const getAccounts = createSelector(getConfig, config => {
83123
const names = new Set()

src/routes/plugin-hub.js

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import {
1717
setPluginSorting
1818
} from '../modules/plugin-hub'
1919
import SearchBar from '../components/search-bar'
20-
import { fetchConfig } from '../modules/config'
20+
import {
21+
fetchConfig,
22+
getExternalPlugins,
23+
getModifiedKeys,
24+
updateConfig
25+
} from '../modules/config'
2126
import Choice from '../components/choice'
2227
import { numberWithCommas } from '../util'
2328

@@ -33,21 +38,44 @@ const handleChange = (event, setPluginFilter) =>
3338
name: event.target.value
3439
})
3540

41+
const handleUpdate = (updateConfig, fetchConfig, externalPlugins) => async (
42+
installed,
43+
pluginName
44+
) => {
45+
if (installed) {
46+
externalPlugins = externalPlugins.filter(i => i !== pluginName)
47+
} else {
48+
externalPlugins.push(pluginName)
49+
}
50+
51+
await updateConfig('runelite.externalPlugins', externalPlugins.join(','))
52+
await fetchConfig()
53+
}
54+
3655
const PluginHub = ({
3756
author,
3857
externalPlugins,
58+
configExternalPlugins,
3959
pluginFilter,
4060
pluginSorting,
4161
setPluginFilter,
42-
setPluginSorting
62+
setPluginSorting,
63+
updateConfig,
64+
fetchConfig,
65+
modifiedKeys
4366
}) => {
44-
externalPlugins = externalPlugins.filter(plugin =>
67+
externalPlugins = [...externalPlugins].filter(plugin =>
4568
author ? plugin.author === author : true
4669
)
4770

4871
const pluginCount = externalPlugins.length
4972
const installedPluginCount = externalPlugins.filter(p => p.installed).length
5073
const totalCount = externalPlugins.reduce((a, b) => a + b.count, 0)
74+
const updateFunction = handleUpdate(
75+
updateConfig,
76+
fetchConfig,
77+
configExternalPlugins
78+
)
5179

5280
return (
5381
<Layout>
@@ -110,9 +138,27 @@ const PluginHub = ({
110138
/>
111139
</div>
112140
</div>
141+
{modifiedKeys.includes('runelite.externalPlugins') && (
142+
<div
143+
style={{
144+
background: '#1e1e1e'
145+
}}
146+
class="p-3"
147+
>
148+
<span class="badge badge-warning">
149+
<b>Warning</b>
150+
</span>{' '}
151+
Installing and uninstalling plugins through this interface
152+
requires client restart.
153+
</div>
154+
)}
113155
<div class="row">
114156
{externalPlugins.map(plugin => (
115-
<ExternalPlugin key={plugin.internalName} {...plugin} />
157+
<ExternalPlugin
158+
key={plugin.internalName}
159+
{...plugin}
160+
update={updateFunction}
161+
/>
116162
))}
117163
</div>
118164
</div>
@@ -124,8 +170,10 @@ const PluginHub = ({
124170
const mapStateToProps = (state, props) => ({
125171
...props,
126172
externalPlugins: getSortedExternalPlugins(state),
173+
configExternalPlugins: getExternalPlugins(state),
127174
pluginFilter: getPluginFilter(state),
128-
pluginSorting: getPluginSorting(state)
175+
pluginSorting: getPluginSorting(state),
176+
modifiedKeys: getModifiedKeys(state)
129177
})
130178

131179
const mapDispatchToProps = dispatch =>
@@ -136,7 +184,8 @@ const mapDispatchToProps = dispatch =>
136184
fetchExternalPlugins,
137185
fetchPluginHubStats,
138186
setPluginFilter,
139-
setPluginSorting
187+
setPluginSorting,
188+
updateConfig
140189
},
141190
dispatch
142191
)

src/store.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ export default callback => {
1616

1717
// Add logger
1818
if (isDebug) {
19-
middlewares.push(require('redux-logger').default)
19+
const { createLogger } = require('redux-logger')
20+
middlewares.push(
21+
createLogger({
22+
diff: true
23+
})
24+
)
2025
}
2126

2227
// Create reducer

0 commit comments

Comments
 (0)