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

Support API_PREFIX instead of API_ENDPOINT + enableExport setting field for charts #1801

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ export type EmbeddingInfo = {
createdBy: string;
createdAt: string;
publicParamsMode: boolean;
settings: {
enableExport?: boolean;
};
};
entry: ChartEntryData | DashEntryData;
};
Expand Down
5 changes: 5 additions & 0 deletions src/server/components/charts-engine/controllers/embeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ export const embedsController = (chartsEngine: ChartsEngine) => {
req.body.config = entry;
req.body.key = entry.key;

req.body.widgetConfig = {
...req.body.widgetConfig,
enableMenuExport: embeddingInfo.embed.settings?.enableExport === true,
};

return runnerFound.handler(ctx, {
chartsEngine,
req,
Expand Down
2 changes: 1 addition & 1 deletion src/server/configs/opensource/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,5 @@ export default {
serviceClientId: process.env.SERVICE_CLIENT_ID || '',
serviceClientSecret: process.env.SERVICE_CLIENT_SECRET || '',

runEndpoint: '/api/run',
apiPrefix: '/api',
};
3 changes: 3 additions & 0 deletions src/shared/schema/us/types/embeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export type Embed = {
updatedAt: string;
updatedBy: string;
publicParamsMode: boolean;
settings: {
enableExport?: boolean;
};
};

export type CreateEmbedArgs = {
Expand Down
1 change: 1 addition & 0 deletions src/shared/types/charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ export type DashWidgetConfig = {
enable?: boolean;
fields?: string[];
};
enableMenuExport?: boolean;
mournfulCoroner marked this conversation as resolved.
Show resolved Hide resolved
};
};

Expand Down
1 change: 1 addition & 0 deletions src/shared/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export type DLGlobalData = {
}
| boolean;
runEndpoint?: string;
apiPrefix?: string;
docPathName?: DocPathName;
chartkitSettings?: ChartkitGlobalSettings;
extraPalettes?: Record<string, Palette>;
Expand Down
26 changes: 25 additions & 1 deletion src/ui/components/Widgets/Chart/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react';
import block from 'bem-cn-lite';
import get from 'lodash/get';
import {useSelector} from 'react-redux';
import {MenuItemsIds} from 'shared';
import type {StringParams} from 'shared';
import {ChartInfoIcon} from 'ui/components/Widgets/Chart/components/ChartInfoIcon';
import type {ChartKitDataProvider} from 'ui/libs/DatalensChartkit/components/ChartKitBase/types';
Expand Down Expand Up @@ -93,7 +94,28 @@ export const Header = (props: HeaderProps) => {
*/
const hideChartComments = Boolean((loadedData?.config as GraphWidget['config'])?.hideComments);

const canBeShownMenu = isMenuAvailable && widgetDataRef;
// exceptions that are shown forcibly under certain conditions
// when menu is not available
const {forceVisibleMenuConfig, hasForceVisibleItems} = React.useMemo(() => {
if (isMenuAvailable) {
return {
forceVisibleMenuConfig: {},
hasForceVisibleItems: false,
};
}

const config = {
[MenuItemsIds.EXPORT]: loadedData?.widgetConfig?.enableMenuExport,
};

return {
forceVisibleMenuConfig: config,
hasForceVisibleItems: Object.values(config).some(Boolean),
};
}, [loadedData?.widgetConfig?.enableMenuExport, isMenuAvailable]);

const canBeShownMenu = (isMenuAvailable || hasForceVisibleItems) && widgetDataRef;

const configMenu = menuType
? getChartkitMenu({
type: menuType,
Expand Down Expand Up @@ -149,6 +171,8 @@ export const Header = (props: HeaderProps) => {
/* isWidgetMenuDataChanged - need this flag for extra rerender after widget rendered to check visibility of items (it is not used in component directly) */
isWidgetMenuDataChanged={isWidgetMenuDataChanged}
chartsDataProvider={dataProvider}
forceVisibleMenuConfig={forceVisibleMenuConfig}
hasForceVisibleItems={hasForceVisibleItems}
/>
)}
</div>
Expand Down
3 changes: 3 additions & 0 deletions src/ui/constants/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ export const DL = {
get RUN_ENDPOINT() {
return window.DL.runEndpoint || '/api/run';
},
get API_PREFIX() {
return window.DL.apiPrefix || '/api';
},
get CONNECTOR_ICONS() {
return window.DL.connectorIcons || [];
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const i18n = I18n.keyset('chartkit.menu.download-csv');
const EXPORT_WARNING_TYPE = 'table';

type DownloadCsvProps = {
onApply: ({chartData, params, path}: ExportChartArgs) => void;
onApply: ({chartData, params}: ExportChartArgs) => void;
loading?: boolean;
onClose: () => void;
chartType: string;
Expand Down Expand Up @@ -89,7 +89,6 @@ export const DownloadCsv = ({
onClose,
chartType,
chartData,
path,
onExportLoading,
}: DownloadCsvProps) => {
const [delValue, setDelValue] = React.useState(';');
Expand All @@ -106,9 +105,9 @@ export const DownloadCsv = ({
encoding,
};

onApply({chartData, params, path, onExportLoading});
onApply({chartData, params, onExportLoading});
onClose();
}, [chartData, delNumber, delValue, encoding, onApply, path]);
}, [chartData, delNumber, delValue, encoding, onApply]);

return (
<Dialog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const i18n = I18n.keyset('chartkit.menu.export');

const toaster = new Toaster();

const getExportResult = async ({chartData, params, path}: ExportChartArgs) => {
const getExportResult = async ({chartData, params}: ExportChartArgs) => {
const {widgetDataRef, loadedData, widget} = chartData;

const fileName = getFileName(loadedData.key);
Expand All @@ -42,7 +42,6 @@ const getExportResult = async ({chartData, params, path}: ExportChartArgs) => {
const exportResult = (await exportWidget({
widgetDataRef: widgetDataRef?.current,
widget: widgetDataRef?.current || widget,
path,
data: loadedData.data,
widgetType: loadedData.type,
options: params,
Expand All @@ -59,8 +58,8 @@ const getExportResult = async ({chartData, params, path}: ExportChartArgs) => {
return exportResult;
};

const copyData = async ({chartData, params, path}: ExportChartArgs) => {
const exportResult = await getExportResult({chartData, path, params});
const copyData = async ({chartData, params}: ExportChartArgs) => {
const exportResult = await getExportResult({chartData, params});
if (!exportResult) {
return;
}
Expand All @@ -71,13 +70,13 @@ const copyData = async ({chartData, params, path}: ExportChartArgs) => {
}
};

const downloadData = async ({chartData, params, path, onExportLoading}: ExportChartArgs) => {
const downloadData = async ({chartData, params, onExportLoading}: ExportChartArgs) => {
const {loadedData} = chartData;
const fileName = getFileName(loadedData.key) + '.';
setLoadingToast(fileName, params?.format || '');
onExportLoading?.(true);

const exportResult = await getExportResult({chartData, path, params});
const exportResult = await getExportResult({chartData, params});
if (!exportResult) {
toaster.remove(fileName);
onExportLoading?.(false);
Expand Down Expand Up @@ -124,18 +123,10 @@ const csvExportAction = (
};

const directExportAction = (
chartsDataProvider: ChartKitDataProvider,
format: ExportFormatsType,
onExportLoading?: ExportChartArgs['onExportLoading'],
) => {
return async (chartData: ExportActionArgs) => {
const {loadedData, propsData} = chartData;

const path = chartsDataProvider.getGoAwayLink(
{loadedData, propsData},
{urlPostfix: '/preview', idPrefix: '/editor/'},
);

const params = {
format,
delValues: null,
Expand All @@ -144,10 +135,10 @@ const directExportAction = (
};

if (format === EXPORT_FORMATS.XLSX) {
downloadData({chartData, params, path, onExportLoading});
downloadData({chartData, params, onExportLoading});
return;
}
copyData({chartData, params, path});
copyData({chartData, params});
};
};

Expand Down Expand Up @@ -210,7 +201,7 @@ const getSubItems = ({
isVisible: ({loadedData, error}: MenuItemArgs) =>
Utils.isEnabledFeature(Feature.XlsxChartExportEnabled) &&
isExportVisible({loadedData, error}),
action: directExportAction(chartsDataProvider, EXPORT_FORMATS.XLSX, onExportLoading),
action: directExportAction(EXPORT_FORMATS.XLSX, onExportLoading),
},
{
id: MenuItemsIds.EXPORT_CSV,
Expand All @@ -222,14 +213,14 @@ const getSubItems = ({
id: MenuItemsIds.EXPORT_MARKDOWN,
title: i18n('format_markdown'),
isVisible: isExportVisible,
action: directExportAction(chartsDataProvider, EXPORT_FORMATS.MARKDOWN),
action: directExportAction(EXPORT_FORMATS.MARKDOWN),
},
{
id: MenuItemsIds.EXPORT_WIKI,
title: i18n('format_wiki'),
isVisible: ({loadedData, error}: MenuItemArgs) =>
Boolean(showWiki) && isExportVisible({loadedData, error}),
action: directExportAction(chartsDataProvider, EXPORT_FORMATS.WIKI),
action: directExportAction(EXPORT_FORMATS.WIKI),
},
{
id: MenuItemsIds.EXPORT_SCREENSHOT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@ export type ExportActionArgs = {
export type ExportChartArgs = {
chartData: ExportActionArgs;
params?: ExportParams;
path?: string;
onExportLoading?: (isLoading: boolean) => void;
};
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export class Menu extends React.PureComponent {
* but we need to toggle show/hide comments menu
*/
hideChartComments: PropTypes.bool,
forceVisibleMenuConfig: PropTypes.object,
hasForceVisibleItems: PropTypes.bool,
};

state = {
Expand Down Expand Up @@ -133,6 +135,10 @@ export class Menu extends React.PureComponent {
if (!this.isVisibleItem({isVisible: item.isVisible, data})) {
return null;
}
const {forceVisibleMenuConfig, hasForceVisibleItems} = this.props;
if (hasForceVisibleItems && !forceVisibleMenuConfig[item.id]) {
return null;
}
return {...item, items: visibleSubItems};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ class ChartsDataProvider implements DataProvider<ChartsProps, ChartsData, Cancel

return axiosInstance(
this.prepareRequestConfig({
url: `${this.requestEndpoint}${DL.RUN_ENDPOINT}`,
url: `${this.requestEndpoint}${DL.API_PREFIX}/run`,
method: 'post',
...requestOptions,
headers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ async function processNode<T extends CurrentResponse, R extends Widget | Control
extra,
requestId,
traceId,
widgetConfig,
} = loaded;

try {
Expand Down Expand Up @@ -283,6 +284,7 @@ async function processNode<T extends CurrentResponse, R extends Widget | Control
isOldWizard: false,
isEditor: loadedType in EDITOR_CHART_NODE,
isQL: loadedType in QL_CHART_NODE,
widgetConfig,
};

if ('unresolvedParams' in loaded) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
ChartsInsightsItem,
DashLoadPriority,
DashTabItemControlSourceType,
DashWidgetConfig,
DatasetFieldCalcMode,
DatasetFieldType,
EntryPublicAuthor,
Expand Down Expand Up @@ -149,7 +150,7 @@ export interface ChartsProps {
forceShowSafeChart?: boolean;
}

export interface ChartsData {
export interface ChartsData extends DashWidgetConfig {
entryId: string;
key: string;
usedParams: StringParams;
Expand Down Expand Up @@ -318,7 +319,7 @@ interface ResponseSuccessWizardBase
interface ResponseSuccessGraphWizard extends ResponseSuccessWizardBase, Comments {}
interface ResponseSuccessMetricWizard extends ResponseSuccessWizardBase {}

export interface ResponseSuccessNodeBase {
export interface ResponseSuccessNodeBase extends DashWidgetConfig {
params: StringParams;
usedParams: StringParams;
unresolvedParams?: StringParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ function run(loaded) {
traceId,
_confStorageConfig: {data: config, type, entryId, key},
publicAuthor,
widgetConfig,
} = loaded;

try {
Expand All @@ -362,6 +363,7 @@ function run(loaded) {
isNewWizard: false,
isOldWizard: true,
extra: {},
widgetConfig,
};

if (unresolvedParams) {
Expand Down
Loading
Loading