-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathplugins.ts
More file actions
83 lines (76 loc) · 2.38 KB
/
Copy pathplugins.ts
File metadata and controls
83 lines (76 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { useCallback, useState } from 'react';
import type { LaboratoryContextProps } from '../components/laboratory/context';
import { LaboratoryTabCustom } from './tabs';
export interface LaboratoryPluginTab<State = Record<string, unknown>> {
type: string;
name: string | ((laboratory: LaboratoryContextProps, state: State) => string);
icon?: React.ReactNode | ((laboratory: LaboratoryContextProps, state: State) => React.ReactNode);
component: (
tab: LaboratoryTabCustom,
laboratory: LaboratoryContextProps,
state: State,
setState: (state: State) => void,
) => React.ReactNode;
}
export interface LaboratoryPlugin<State = Record<string, unknown>> {
id: string;
name: string;
description?: string;
icon?: React.ReactNode;
defaultState?: State;
onStateChange?: (state: State) => void;
tabs?: LaboratoryPluginTab<State>[];
commands?: {
name: string | ((laboratory: LaboratoryContextProps, state: State) => string);
icon?:
| React.ReactNode
| ((laboratory: LaboratoryContextProps, state: State) => React.ReactNode);
onClick: (laboratory: LaboratoryContextProps, state: State) => void;
}[];
preflight?: {
lab?: {
definition?: string;
props?: Record<string, string>;
object: (
props: Record<string, string>,
state: State,
setState: (state: State) => void,
) => Record<string, unknown>;
};
};
}
export interface LaboratoryPluginsState {
plugins: LaboratoryPlugin[];
pluginsState: Record<string, any>;
}
export interface LaboratoryPluginsActions {
setPluginsState: (state: Record<string, any>) => void;
}
export const usePlugins = (props: {
plugins?: LaboratoryPlugin[];
defaultPluginsState?: Record<string, any>;
onPluginsStateChange?: (state: Record<string, any>) => void;
}): LaboratoryPluginsState & LaboratoryPluginsActions => {
const [pluginsState, _setPluginsState] = useState<Record<string, any>>({
...props.plugins?.reduce(
(acc, plugin) => {
acc[plugin.id] = plugin.defaultState ?? {};
return acc;
},
{} as Record<string, any>,
),
...props.defaultPluginsState,
});
const setPluginsState = useCallback(
(state: Record<string, any>) => {
_setPluginsState(state);
props.onPluginsStateChange?.(state);
},
[props],
);
return {
plugins: props.plugins ?? [],
pluginsState,
setPluginsState,
};
};