Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix][WRS-2298] Read connector options from linked variable #277

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"react-select-event": "^5.5.1",
"resize-observer-polyfill": "^1.5.1",
"ts-jest": "^29.1.0",
"typescript": "*",
"typescript": "^5.7.3",
"vite": "^4.3.9",
"vite-plugin-environment": "^1.1.3"
},
Expand Down
14 changes: 8 additions & 6 deletions src/components/pagesPanel/Pages.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { useCallback, useEffect, useState } from 'react';
import { Page } from '@chili-publish/studio-sdk';
import {
PreviewCard,
PreviewCardVariant,
PreviewType,
ScrollbarWrapper,
useMobileSize,
} from '@chili-publish/grafx-shared-components';
import { ScrollableContainer, Card, Container } from './Pages.styles';
import { BORDER_SIZE, PAGES_CONTAINER_HEIGHT, PREVIEW_FALLBACK } from '../../utils/constants';
import { Page } from '@chili-publish/studio-sdk';
import { useCallback, useEffect, useState } from 'react';
import { useUiConfigContext } from '../../contexts/UiConfigContext';
import { PageSnapshot } from '../../types/types';
import { BORDER_SIZE, PAGES_CONTAINER_HEIGHT, PREVIEW_FALLBACK } from '../../utils/constants';
import { Card, Container, ScrollableContainer } from './Pages.styles';
import { PreviewCardBadge } from './PreviewCardBadge';
import { useAttachArrowKeysListener } from './useAttachArrowKeysListener';
import { useUiConfigContext } from '../../contexts/UiConfigContext';

interface PagesProps {
pages: Page[];
Expand Down Expand Up @@ -83,7 +83,7 @@
return () => {
clearTimeout(timeoutId);
};
}, [pagesToRefresh, setPagesToRefresh, getPagesSnapshot]);

Check warning on line 86 in src/components/pagesPanel/Pages.tsx

View workflow job for this annotation

GitHub Actions / build (20)

React Hook useEffect has a missing dependency: 'pages'. Either include it or remove the dependency array. If 'setPageSnapshots' needs the current value of 'pages', you can also switch to useReducer instead of useState and read 'pages' in the reducer

if (uiOptions.widgets?.bottomBar?.visible === false) return null;

Expand All @@ -105,7 +105,9 @@
path={
pageSnapshots[index] &&
URL.createObjectURL(
new Blob([pageSnapshots[index].snapshot.buffer], { type: 'image/png' }),
new Blob([pageSnapshots[index].snapshot.buffer as BlobPart], {
type: 'image/png',
}),
)
}
type={PreviewType.IMAGE}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useCallback, useEffect, useState } from 'react';
import { ConnectorMappingDirection, ConnectorStateType, Media } from '@chili-publish/studio-sdk';
import { useCallback, useEffect, useState } from 'react';
import { useVariablePanelContext } from '../../../contexts/VariablePanelContext';
import { useVariablesChange } from '../../../core/hooks/useVariablesChange';
import { fromLinkToVariableId, isLinkToVariable } from '../../../utils/connectors';

export const useMediaDetails = (connectorId: string | undefined, mediaAssetId: string | undefined) => {
const { connectorCapabilities, getCapabilitiesForConnector } = useVariablePanelContext();
Expand Down Expand Up @@ -47,11 +48,13 @@ export const useMediaDetails = (connectorId: string | undefined, mediaAssetId: s
if (!connectorId || mediaConnectorState === null) {
return;
}
const mappings = await window.StudioUISDK.connector.getMappings(connectorId);
const mappings = await window.StudioUISDK.connector.getMappings(
connectorId,
ConnectorMappingDirection.engineToConnector,
);
const variableIds = mappings.parsedData
?.filter((m) => m.direction === ConnectorMappingDirection.engineToConnector)
.filter((m) => typeof m.value === 'string' && m.value.startsWith('var.'))
.map((m) => (m.value as string).replace('var.', ''));
?.filter((m) => isLinkToVariable(m))
.map((m) => fromLinkToVariableId(m.value));
setVariableIdsInMapping(variableIds ?? []);
})();
}, [connectorId, mediaConnectorState]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ describe('"useMediaDetails" hook', () => {

expect(window.StudioUISDK.connector.waitToBeReady).not.toHaveBeenCalledWith('grafx-media');

await waitFor(() => expect(window.StudioUISDK.connector.getMappings).toHaveBeenCalledWith('grafx-media'));
await waitFor(() =>
expect(window.StudioUISDK.connector.getMappings).toHaveBeenCalledWith(
'grafx-media',
ConnectorMappingDirection.engineToConnector,
),
);
await waitFor(() => expect(getCapabilitiesForConnector).toHaveBeenCalledWith('grafx-media'));

expect(window.StudioUISDK.mediaConnector.query).not.toHaveBeenCalled();
Expand All @@ -127,7 +132,12 @@ describe('"useMediaDetails" hook', () => {

await waitFor(() => expect(window.StudioUISDK.connector.getState).toHaveBeenCalledWith('grafx-media'));

await waitFor(() => expect(window.StudioUISDK.connector.getMappings).toHaveBeenCalledWith('grafx-media'));
await waitFor(() =>
expect(window.StudioUISDK.connector.getMappings).toHaveBeenCalledWith(
'grafx-media',
ConnectorMappingDirection.engineToConnector,
),
);

expect(getCapabilitiesForConnector).not.toHaveBeenCalledWith('grafx-media');
expect(window.StudioUISDK.mediaConnector.query).not.toHaveBeenCalled();
Expand All @@ -150,7 +160,12 @@ describe('"useMediaDetails" hook', () => {
renderHook(() => useMediaDetails('grafx-media', 'media-asset-id'));

await waitFor(() => expect(window.StudioUISDK.connector.getState).toHaveBeenCalledWith('grafx-media'));
await waitFor(() => expect(window.StudioUISDK.connector.getMappings).toHaveBeenCalledWith('grafx-media'));
await waitFor(() =>
expect(window.StudioUISDK.connector.getMappings).toHaveBeenCalledWith(
'grafx-media',
ConnectorMappingDirection.engineToConnector,
),
);

const variableChange = { id: '7377E97A-5FD9-46B1-A8CF-0C7C776C7DC2', value: '1234' } as unknown as Variable;
mockSubscriber.emit('onVariableListChanged', [variableChange]);
Expand All @@ -176,7 +191,12 @@ describe('"useMediaDetails" hook', () => {
renderHook(() => useMediaDetails('grafx-media', 'media-asset-id'));

await waitFor(() => expect(window.StudioUISDK.connector.getState).toHaveBeenCalledWith('grafx-media'));
await waitFor(() => expect(window.StudioUISDK.connector.getMappings).toHaveBeenCalledWith('grafx-media'));
await waitFor(() =>
expect(window.StudioUISDK.connector.getMappings).toHaveBeenCalledWith(
'grafx-media',
ConnectorMappingDirection.engineToConnector,
),
);

const variableChange = { id: '2', value: '1234' } as unknown as Variable;
mockSubscriber.emit('onVariableListChanged', [variableChange]);
Expand All @@ -202,7 +222,12 @@ describe('"useMediaDetails" hook', () => {
const { result } = renderHook(() => useMediaDetails('grafx-media', 'media-asset-id'));

await waitFor(() => expect(window.StudioUISDK.connector.getState).toHaveBeenCalledWith('grafx-media'));
await waitFor(() => expect(window.StudioUISDK.connector.getMappings).toHaveBeenCalledWith('grafx-media'));
await waitFor(() =>
expect(window.StudioUISDK.connector.getMappings).toHaveBeenCalledWith(
'grafx-media',
ConnectorMappingDirection.engineToConnector,
),
);

const variableChange = { id: '8A59BB89-898D-4BAC-9C8F-F40F6C83479E', value: '1234' } as unknown as Variable;
mockSubscriber.emit('onVariableListChanged', [variableChange]);
Expand Down Expand Up @@ -233,7 +258,12 @@ describe('"useMediaDetails" hook', () => {
const { result } = renderHook(() => useMediaDetails('grafx-media', 'media-asset-id'));

await waitFor(() => expect(window.StudioUISDK.connector.getState).toHaveBeenCalledWith('grafx-media'));
await waitFor(() => expect(window.StudioUISDK.connector.getMappings).toHaveBeenCalledWith('grafx-media'));
await waitFor(() =>
expect(window.StudioUISDK.connector.getMappings).toHaveBeenCalledWith(
'grafx-media',
ConnectorMappingDirection.engineToConnector,
),
);

const variableChange = { id: '8A59BB89-898D-4BAC-9C8F-F40F6C83479E', value: '1234' } as unknown as Variable;
mockSubscriber.emit('onVariableListChanged', [variableChange]);
Expand Down Expand Up @@ -271,7 +301,12 @@ describe('"useMediaDetails" hook', () => {
const { result } = renderHook(() => useMediaDetails('grafx-media', 'media-asset-id'));

await waitFor(() => expect(window.StudioUISDK.connector.getState).toHaveBeenCalledWith('grafx-media'));
await waitFor(() => expect(window.StudioUISDK.connector.getMappings).toHaveBeenCalledWith('grafx-media'));
await waitFor(() =>
expect(window.StudioUISDK.connector.getMappings).toHaveBeenCalledWith(
'grafx-media',
ConnectorMappingDirection.engineToConnector,
),
);

const variableChange = { id: '8A59BB89-898D-4BAC-9C8F-F40F6C83479E', value: '1234' } as unknown as Variable;
await act(() => {
Expand Down
59 changes: 57 additions & 2 deletions src/tests/utils/connectors.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import EditorSDK from '@chili-publish/studio-sdk';
import EditorSDK, { ConnectorMappingDirection } from '@chili-publish/studio-sdk';
import axios from 'axios';
import { mock } from 'jest-mock-extended';
import { getRemoteConnector, isAuthenticationRequired, verifyAuthentication } from '../../utils/connectors';
import {
getConnectorConfigurationOptions,
getRemoteConnector,
isAuthenticationRequired,
verifyAuthentication,
} from '../../utils/connectors';

jest.mock('axios');

Expand All @@ -18,6 +23,8 @@ describe('utils connectors', () => {
},
});
mockSDK.mediaConnector.query = jest.fn();
mockSDK.variable.getById = jest.fn();
mockSDK.connector.getMappings = jest.fn();

window.StudioUISDK = mockSDK;

Expand Down Expand Up @@ -74,4 +81,52 @@ describe('utils connectors', () => {

expect(mockSDK.mediaConnector.query).toHaveBeenCalledWith('connectorId', {});
});

it('should "getConnectorConfigurationOptions" correctly', async () => {
(mockSDK.variable.getById as jest.Mock)
.mockResolvedValueOnce({
parsedData: {
value: 'text',
},
})
.mockResolvedValueOnce({
parsedData: {
value: false,
},
});

(mockSDK.connector.getMappings as jest.Mock).mockResolvedValueOnce({
parsedData: [
{
name: 'option-1',
value: 'plain-text',
},
{
name: 'option-2',
value: 'var.var1',
},
{
name: 'option-3',
value: true,
},
{
name: 'option-4',
value: 'var.var2',
},
],
});

const options = await getConnectorConfigurationOptions('connector-1');

expect(mockSDK.connector.getMappings).toHaveBeenCalledWith(
'connector-1',
ConnectorMappingDirection.engineToConnector,
);
expect(options).toEqual({
'option-1': 'plain-text',
'option-2': 'text',
'option-3': true,
'option-4': false,
});
});
});
6 changes: 4 additions & 2 deletions src/tests/utils/documentExportHelper.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ describe('"getDownloadLink', () => {
);
});

it('should skip sending data source output type is not "Batch"', async () => {
it('should skip sending data source if output setting does not have "dataSourceEnabled"', async () => {
(axios.get as jest.Mock).mockResolvedValue({
data: {},
data: {
dataSourceEnabled: false,
},
});
await getDownloadLink(DownloadFormats.PDF, '', 'Token', '1', 'projectId', 'outputId', false);

Expand Down
47 changes: 40 additions & 7 deletions src/utils/connectors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ConnectorMappingDirection } from '@chili-publish/studio-sdk';
import {
BooleanVariable,
ConnectorMappingDirection,
EngineToConnectorMapping,
ShortTextVariable,
} from '@chili-publish/studio-sdk';
import {
ConnectorGrafxRegistration,
ConnectorInstance,
Expand Down Expand Up @@ -68,16 +73,44 @@ export function getEnvId(connector: ConnectorInstance) {
return connector.source.id;
}

export type TextEngineToConnectorMapping = Omit<EngineToConnectorMapping, 'value'> & { value: string };

type LinkedVariable = ShortTextVariable | BooleanVariable;

export function isLinkToVariable(mapping: EngineToConnectorMapping): mapping is TextEngineToConnectorMapping {
return typeof mapping.value === 'string' && mapping.value.startsWith('var.');
}

export function linkToVariable(variableId: string) {
return `var.${variableId}`;
}

export function fromLinkToVariableId(value: string) {
return value.replace('var.', '');
}

export async function getConnectorConfigurationOptions(connectorId: string) {
const { parsedData } = await window.StudioUISDK.connector.getMappings(
connectorId,
ConnectorMappingDirection.engineToConnector,
);
return (
parsedData?.reduce((config, mapping) => {
// eslint-disable-next-line no-param-reassign
config[mapping.name] = mapping.value;
return config;
}, {} as Record<string, string | boolean>) ?? null
if (!parsedData) {
return null;
}
const mappingValues = await Promise.all(
parsedData.map((m) => {
if (isLinkToVariable(m)) {
return window.StudioUISDK.variable.getById(fromLinkToVariableId(m.value)).then((v) => ({
name: m.name,
value: (v.parsedData as LinkedVariable).value,
}));
}
return m;
}),
);
return mappingValues.reduce((config, mapping) => {
// eslint-disable-next-line no-param-reassign
config[mapping.name] = mapping.value;
return config;
}, {} as Record<string, string | boolean>);
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6262,10 +6262,10 @@ typed-array-length@^1.0.4:
for-each "^0.3.3"
is-typed-array "^1.1.9"

typescript@*:
version "5.2.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.2.2.tgz#5ebb5e5a5b75f085f22bc3f8460fba308310fa78"
integrity sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==
typescript@^5.7.3:
version "5.7.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.7.3.tgz#919b44a7dbb8583a9b856d162be24a54bf80073e"
integrity sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw==

unbox-primitive@^1.0.2:
version "1.0.2"
Expand Down
Loading