diff --git a/client/extensions/auto-tagger/src/auto-tagging.tsx b/client/extensions/auto-tagger/src/auto-tagging.tsx index 0e580254..eb490591 100644 --- a/client/extensions/auto-tagger/src/auto-tagging.tsx +++ b/client/extensions/auto-tagger/src/auto-tagging.tsx @@ -3,7 +3,7 @@ import {OrderedMap, OrderedSet, Map} from 'immutable'; import {Switch, Button, ButtonGroup, EmptyState, Autocomplete, Modal} from 'superdesk-ui-framework/react'; import {ToggleBoxNext} from 'superdesk-ui-framework'; -import {IArticle, IArticleSideWidget, ISuperdesk} from 'superdesk-api'; +import {IArticle, IAuthoringSideWidget, ISuperdesk} from 'superdesk-api'; import {getTagsListComponent} from './tag-list'; import {getNewItemComponent} from './new-item'; @@ -35,7 +35,7 @@ interface IAutoTaggingSearchResult { }; } -type IProps = React.ComponentProps; +type IProps = React.ComponentProps; interface ISemaphoreFields { [key: string]: { @@ -141,12 +141,12 @@ function showAutoTaggerServiceErrorModal(superdesk: ISuperdesk, errors: Array + <> + { + (() => { + if (data === 'loading' || data === 'not-initialized') { + return null; + } else { + const treeErrors = arrayToTree( + data.changes.analysis.toArray(), + (item) => item.qcode, + (item) => item.parent, + ).errors; + + // only show errors when there are unsaved changes + if (treeErrors.length > 0 && dirty) { + return ( + { + showAutoTaggerServiceErrorModal(superdesk, treeErrors); + }, + icon: 'info-sign', + }, + ]} + /> + ); + } else { + return null; + } + } + })() + } + + { data === 'loading' || data === 'not-initialized' || !dirty ? null : (
@@ -436,192 +479,146 @@ export function getAutoTaggingComponent(superdesk: ISuperdesk, label: string): I
) } - - )} - body={( - +
+
+
+ {/* Run automatically button is hidden for the next release */} +
+ + { + const newValue = !runAutomaticallyPreference; + + this.setState({runAutomaticallyPreference: newValue}); + + superdesk.preferences.set(RUN_AUTOMATICALLY_PREFERENCE, newValue); + + if (newValue && this.state.data === 'not-initialized') { + this.runAnalysis(); + } + }} + aria-label="Run automatically" + label={{content: gettext('Run automatically')}} + /> + +
+ { - (() => { - if (data === 'loading' || data === 'not-initialized') { - return null; - } else { - const treeErrors = arrayToTree( - data.changes.analysis.toArray(), - (item) => item.qcode, - (item) => item.parent, - ).errors; - - // only show errors when there are unsaved changes - if (treeErrors.length > 0 && dirty) { - return ( - { - showAutoTaggerServiceErrorModal(superdesk, treeErrors); + data === 'loading' || data === 'not-initialized' || log === 'error' ? null : ( + <> +
+
+ { + let cancelled = false; + + httpRequestJsonLocal({ + method: 'POST', + path: '/ai_data_op/', + payload: { + service: 'semaphore', + operation: 'search', + data: { + searchString, + }, }, - icon: 'info-sign', - }, - ]} - /> - ); - } else { - return null; - } - } - })() - } - -
-
- {/* Run automatically button is hidden for the next release */} -
- - { - const newValue = !runAutomaticallyPreference; - - this.setState({runAutomaticallyPreference: newValue}); + }).then((res) => { + if (cancelled === true) { + return; + } - superdesk.preferences.set(RUN_AUTOMATICALLY_PREFERENCE, newValue); + const json_response = res.result.tags; + const result_data = res; - if (newValue && this.state.data === 'not-initialized') { - this.runAnalysis(); - } - }} - aria-label="Run automatically" - label={{content: gettext('Run automatically')}} - /> - -
+ const result = toClientFormat(json_response).toArray(); - { - data === 'loading' || data === 'not-initialized' || log === 'error' ? null : ( - <> -
-
- { - let cancelled = false; - - httpRequestJsonLocal<{analysis: IAutoTaggingSearchResult}>({ - method: 'POST', - path: '/ai/', - payload: { - service: 'semaphore', - item: { - searchString, - }, - }, - }).then((res) => { - if (cancelled === true) { - return; - } - - const json_response = res.analysis.result.tags; - const result_data = res.analysis; - - const result = toClientFormat(json_response).toArray(); - - const withoutExistingTags = result.filter( - (searchTag) => !tagAlreadyExists(data, searchTag.qcode), - ); + const withoutExistingTags = result.filter( + (searchTag) => !tagAlreadyExists(data, searchTag.qcode), + ); - const withResponse = withoutExistingTags.map((tag) => ({ - // required for Autocomplete component - keyValue: tag.name, + const withResponse = withoutExistingTags.map((tag) => ({ + // required for Autocomplete component + keyValue: tag.name, - tag, + tag, - // required to get all parents when an item is selected - entireResponse: result_data, - })); + // required to get all parents when an item is selected + entireResponse: result_data, + })); - callback(withResponse); - }); + callback(withResponse); + }); - return { - cancel: () => { - cancelled = true; - }, - }; - }} - listItemTemplate={(__item: any) => { - const _item: ITagUi = __item.tag; - - return ( -
- {_item.name} - - { - _item?.group?.value == null ? null : ( -

{_item.group.value}

- ) - } - - { - _item?.description == null ? null : ( -

{_item.description}

- ) - } -
- ); - }} - onSelect={(_value: any) => { - const tag: ITagUi = _value.tag; - const entireResponse: IAutoTaggingSearchResult = - _value.entireResponse; - - this.insertTagFromSearch(tag, data, entireResponse); - this.setState({ - tentativeTagName: '', - forceRenderKey: Math.random(), - }); - }} - onChange={noop} - /> -
-
-
-
- - ) - } -
+ return { + cancel: () => { + cancelled = true; + }, + }; + }} + listItemTemplate={(__item: any) => { + const _item: ITagUi = __item.tag; + + return ( +
+ {_item.name} + + { + _item?.group?.value == null ? null : ( +

{_item.group.value}

+ ) + } + + { + _item?.description == null ? null : ( +

{_item.description}

+ ) + } +
+ ); + }} + onSelect={(_value: any) => { + const tag: ITagUi = _value.tag; + const entireResponse: IAutoTaggingSearchResult = + _value.entireResponse; + + this.insertTagFromSearch(tag, data, entireResponse); + this.setState({ + tentativeTagName: '', + forceRenderKey: Math.random(), + }); + }} + onChange={noop} + /> +
+
+
+
+ + ) + } +
{(() => { if (data === 'loading') { @@ -764,41 +761,43 @@ export function getAutoTaggingComponent(superdesk: ISuperdesk, label: string): I ); } })()} -
- - )} - footer={(() => { - if (data === 'loading' || log === 'error') { - return ; - } else if (data === 'not-initialized') { - return ( -
+ + ); } };