This commit is contained in:
Tom Moor committed 2024-06-16 10:04:53 -04:00
1 parent b219e42cc8
commit fc8a491133
10 files changed
+44 -26

No files matched your search

+4 -3
View File
@@ -1,7 +1,8 @@
import { observer } from "mobx-react";
import * as React from "react";
import styled from "styled-components";
import { Hook, PluginManager } from "~/utils/PluginManager";
import Logger from "~/utils/Logger";
import { Hook, usePluginValue } from "~/utils/PluginManager";
type Props = {
/** The ID of the plugin to render an Icon for. */
@@ -16,8 +17,7 @@ type Props = {
* Renders an icon defined in a plugin (Hook.Icon).
*/
function PluginIcon({ id, color, size = 24 }: Props) {
const plugin = PluginManager.getHook(Hook.Icon, id);
const Icon = plugin?.value;
const Icon = usePluginValue(Hook.Icon, id);
if (Icon) {
return (
@@ -27,6 +27,7 @@ function PluginIcon({ id, color, size = 24 }: Props) {
);
}
Logger.warn("No Icon registered for plugin", { id });
return null;
}
@@ -93,17 +93,15 @@ function AuthenticationProvider(props: Props) {
}
return (
<Wrapper>
<ButtonLarge
onClick={() => (window.location.href = href)}
icon={<PluginIcon id={id} />}
fullwidth
>
{t("Continue with {{ authProviderName }}", {
authProviderName: name,
})}
</ButtonLarge>
</Wrapper>
<ButtonLarge
onClick={() => (window.location.href = href)}
icon={<PluginIcon id={id} />}
fullwidth
>
{t("Continue with {{ authProviderName }}", {
authProviderName: name,
})}
</ButtonLarge>
);
}
+19 -5
View File
@@ -1,6 +1,7 @@
import isArray from "lodash/isArray";
import sortBy from "lodash/sortBy";
import { observable } from "mobx";
import { action, observable } from "mobx";
import { useComputed } from "~/hooks/useComputed";
import Logger from "./Logger";
import isCloudHosted from "./isCloudHosted";
@@ -22,7 +23,7 @@ type PluginValueMap = {
/** The displayed icon of the plugin. */
icon: React.ElementType;
/** The settings screen somponent, should be lazy loaded. */
component: React.LazyExoticComponent<React.ComponentType<void>>;
component: React.LazyExoticComponent<React.ComponentType>;
};
[Hook.Icon]: React.ElementType;
};
@@ -63,6 +64,7 @@ export class PluginManager {
this.register(plugins);
}
@action
private static register<T extends Hook>(plugin: Plugin<T>) {
const enabledInDeployment =
!plugin?.deployments ||
@@ -75,7 +77,7 @@ export class PluginManager {
}
if (!this.plugins.has(plugin.type)) {
this.plugins.set(plugin.type, []);
this.plugins.set(plugin.type, observable.array([]));
}
this.plugins
@@ -108,7 +110,9 @@ export class PluginManager {
* @returns A plugin
*/
public static getHook<T extends Hook>(type: T, id: string) {
return this.getHooks(type).find((hook) => hook.id === id);
return this.plugins.get(type)?.find((hook) => hook.id === id) as
| Plugin<T>
| undefined;
}
/**
@@ -121,11 +125,21 @@ export class PluginManager {
const r = import.meta.glob("../../plugins/*/client/index.{ts,js,tsx,jsx}");
await Promise.all(Object.keys(r).map((key: string) => r[key]()));
this.loaded = true;
}
private static plugins = observable.map<Hook, Plugin<Hook>[]>();
@observable
private static loaded = false;
}
/**
* Convenience hook to get the value for a specific plugin and type.
*/
export function usePluginValue<T extends Hook>(type: T, id: string) {
return useComputed(
() => PluginManager.getHook<T>(type, id)?.value,
[type, id]
);
}
+1 -1
View File
@@ -2,7 +2,7 @@ import { Hook, PluginManager } from "~/utils/PluginManager";
import config from "../plugin.json";
import Icon from "./Icon";
export default PluginManager.add([
PluginManager.add([
{
...config,
type: Hook.Icon,
+1 -1
View File
@@ -3,7 +3,7 @@ import { Hook, PluginManager } from "~/utils/PluginManager";
import config from "../plugin.json";
import Icon from "./Icon";
export default PluginManager.add([
PluginManager.add([
{
...config,
type: Hook.Settings,
+1 -1
View File
@@ -2,7 +2,7 @@ import { Hook, PluginManager } from "~/utils/PluginManager";
import config from "../plugin.json";
import Icon from "./Icon";
export default PluginManager.add([
PluginManager.add([
{
...config,
type: Hook.Icon,
+1 -1
View File
@@ -3,7 +3,7 @@ import { Hook, PluginManager } from "~/utils/PluginManager";
import config from "../plugin.json";
import Icon from "./Icon";
export default PluginManager.add([
PluginManager.add([
{
...config,
type: Hook.Settings,
+1 -1
View File
@@ -3,7 +3,7 @@ import { Hook, PluginManager } from "~/utils/PluginManager";
import config from "../plugin.json";
import Icon from "./Icon";
export default PluginManager.add([
PluginManager.add([
{
...config,
type: Hook.Settings,
+6 -1
View File
@@ -3,7 +3,7 @@ import { Hook, PluginManager } from "~/utils/PluginManager";
import config from "../plugin.json";
import Icon from "./Icon";
export default PluginManager.add([
PluginManager.add([
{
...config,
type: Hook.Settings,
@@ -13,4 +13,9 @@ export default PluginManager.add([
component: React.lazy(() => import("./Settings")),
},
},
{
...config,
type: Hook.Icon,
value: Icon,
},
]);
+1 -1
View File
@@ -3,7 +3,7 @@ import { Hook, PluginManager } from "~/utils/PluginManager";
import config from "../plugin.json";
import Icon from "./Icon";
export default PluginManager.add([
PluginManager.add([
{
...config,
type: Hook.Settings,