MCPcopy
hub / github.com/graphql/graphiql / PluginContextProvider

Function PluginContextProvider

packages/graphiql-react/src/plugin.tsx:71–156  ·  view source on GitHub ↗
({
  onTogglePluginVisibility,
  children,
  visiblePlugin,
  plugins: $plugins,
  referencePlugin,
})

Source from the content-addressed store, hash-verified

69};
70
71export const PluginContextProvider: FC<PluginContextProviderProps> = ({
72 onTogglePluginVisibility,
73 children,
74 visiblePlugin,
75 plugins: $plugins,
76 referencePlugin,
77}) => {
78 const storage = useStorageContext();
79 const plugins = (() => {
80 const pluginList: GraphiQLPlugin[] = [];
81 const pluginTitles: Record<string, true> = {};
82 for (const plugin of $plugins || []) {
83 if (typeof plugin.title !== 'string' || !plugin.title) {
84 throw new Error('All GraphiQL plugins must have a unique title');
85 }
86 if (pluginTitles[plugin.title]) {
87 throw new Error(
88 `All GraphiQL plugins must have a unique title, found two plugins with the title '${plugin.title}'`,
89 );
90 } else {
91 pluginList.push(plugin);
92 pluginTitles[plugin.title] = true;
93 }
94 }
95
96 return pluginList;
97 })();
98
99 const [$visiblePlugin, internalSetVisiblePlugin] =
100 useState<GraphiQLPlugin | null>(() => {
101 const storedValue = storage?.get(STORAGE_KEY);
102 const pluginForStoredValue = plugins.find(
103 plugin => plugin.title === storedValue,
104 );
105 if (pluginForStoredValue) {
106 return pluginForStoredValue;
107 }
108 if (storedValue) {
109 storage?.set(STORAGE_KEY, '');
110 }
111
112 if (!visiblePlugin) {
113 return null;
114 }
115
116 return (
117 plugins.find(
118 plugin =>
119 (typeof visiblePlugin === 'string' ? plugin.title : plugin) ===
120 visiblePlugin,
121 ) || null
122 );
123 });
124
125 const setVisiblePlugin: PluginContextType['setVisiblePlugin'] = // eslint-disable-line react-hooks/exhaustive-deps -- false positive, function is optimized by react-compiler, no need to wrap with useCallback
126 plugin => {
127 const newVisiblePlugin = plugin
128 ? plugins.find(

Callers

nothing calls this directly

Calls 4

setVisiblePluginFunction · 0.85
pushMethod · 0.80
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected