@squiz/resource-browser 3.2.0 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change Log
2
2
 
3
+ ## 3.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - a64f8ae: Derive plugin synchronously from source to avoid stale state
8
+
3
9
  ## 3.2.0
4
10
 
5
11
  ### Minor Changes
package/lib/index.js CHANGED
@@ -55,7 +55,7 @@ const ResourceBrowser = (props) => {
55
55
  const [source, setSource] = (0, react_1.useState)(null);
56
56
  const [mode, setMode] = (0, react_1.useState)(null);
57
57
  const { data: sources, isLoading, error: sourcesError, reload: reloadSources } = (0, useSources_1.useSources)({ onRequestSources, plugins });
58
- const [plugin, setPlugin] = (0, react_1.useState)(null);
58
+ const plugin = source?.plugin || null;
59
59
  // MainContainer will render a list of sources of one is not provided to it, callback to allow it to set the source once a user selects
60
60
  const handleSourceSelect = (0, react_1.useCallback)((source, mode) => {
61
61
  setSource(source);
@@ -82,16 +82,6 @@ const ResourceBrowser = (props) => {
82
82
  setSource(source);
83
83
  setMode(null); // Passed in resource will always use the default mode
84
84
  }, [value, isLoading, sources, setSource, setError]);
85
- // When a source is selected update our plugin reference to match (legacy support)
86
- // the plugin is now attached to the source directly when fetched from the context so use that instead when possible
87
- (0, react_1.useEffect)(() => {
88
- if (source?.plugin) {
89
- setPlugin(source.plugin);
90
- }
91
- else {
92
- setPlugin(null);
93
- }
94
- }, [plugins, source]);
95
85
  // The modal has some control over it own open/closed state (for WCAG reasons) so keep this in sync with our state
96
86
  const handleModalStateChange = (0, react_1.useCallback)((isOpen) => {
97
87
  setIsModalOpen(isOpen);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squiz/resource-browser",
3
- "version": "3.2.0",
3
+ "version": "3.2.1",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "private": false,
@@ -666,5 +666,53 @@ describe('Resource browser input', () => {
666
666
  );
667
667
  });
668
668
  });
669
+
670
+ it('switching source updates plugin and does not use the old plugin', async () => {
671
+ const damSource = mockSource({ type: 'dam', id: '1' });
672
+ const matrixSource = mockSource({ type: 'matrix', id: '2' });
673
+
674
+ const pluginA = { ...mockDamPlugin, type: 'dam' };
675
+ const pluginB = { ...mockDamPlugin, type: 'matrix' };
676
+
677
+ mockRequestSources.mockResolvedValueOnce([damSource, matrixSource]);
678
+
679
+ renderComponent();
680
+
681
+ const { setSource } = (RBI.ResourceBrowserInput as jest.Mock).mock.calls[0][0];
682
+
683
+ act(() => {
684
+ setSource({ ...damSource, plugin: pluginA });
685
+ });
686
+
687
+ await waitFor(() => {
688
+ expect(RBI.ResourceBrowserInput).toHaveBeenCalledWith(
689
+ expect.objectContaining({
690
+ plugin: pluginA,
691
+ }),
692
+ expect.any(Object),
693
+ );
694
+ });
695
+
696
+ (Plugin.PluginRender as jest.Mock).mockClear();
697
+
698
+ act(() => {
699
+ setSource({ ...matrixSource, plugin: pluginB });
700
+ });
701
+
702
+ await waitFor(() => {
703
+ expect(Plugin.PluginRender).toHaveBeenCalledWith(
704
+ expect.objectContaining({
705
+ plugin: pluginB,
706
+ }),
707
+ expect.any(Object),
708
+ );
709
+ expect(Plugin.PluginRender).not.toHaveBeenCalledWith(
710
+ expect.objectContaining({
711
+ plugin: pluginA,
712
+ }),
713
+ expect.any(Object),
714
+ );
715
+ });
716
+ });
669
717
  });
670
718
  });
package/src/index.tsx CHANGED
@@ -6,7 +6,6 @@ import {
6
6
  PluginLaunchMode,
7
7
  ResourceBrowserUnresolvedResource,
8
8
  ResourceBrowserResource,
9
- ResourceBrowserPlugin,
10
9
  ResourceBrowserSourceWithPlugin,
11
10
  } from './types';
12
11
  import { useSources } from './Hooks/useSources';
@@ -49,7 +48,7 @@ export const ResourceBrowser = (props: ResourceBrowserProps) => {
49
48
  const [source, setSource] = useState<ResourceBrowserSourceWithPlugin | null>(null);
50
49
  const [mode, setMode] = useState<PluginLaunchMode | null>(null);
51
50
  const { data: sources, isLoading, error: sourcesError, reload: reloadSources } = useSources({ onRequestSources, plugins });
52
- const [plugin, setPlugin] = useState<ResourceBrowserPlugin | null>(null);
51
+ const plugin = source?.plugin || null;
53
52
 
54
53
  // MainContainer will render a list of sources of one is not provided to it, callback to allow it to set the source once a user selects
55
54
  const handleSourceSelect = useCallback(
@@ -83,16 +82,6 @@ export const ResourceBrowser = (props: ResourceBrowserProps) => {
83
82
  setMode(null); // Passed in resource will always use the default mode
84
83
  }, [value, isLoading, sources, setSource, setError]);
85
84
 
86
- // When a source is selected update our plugin reference to match (legacy support)
87
- // the plugin is now attached to the source directly when fetched from the context so use that instead when possible
88
- useEffect(() => {
89
- if (source?.plugin) {
90
- setPlugin(source.plugin);
91
- } else {
92
- setPlugin(null);
93
- }
94
- }, [plugins, source]);
95
-
96
85
  // The modal has some control over it own open/closed state (for WCAG reasons) so keep this in sync with our state
97
86
  const handleModalStateChange = useCallback(
98
87
  (isOpen: boolean) => {