const onInstrumentChange = useCallback(
(
newVal: string,
newName?: string,
withUpdate?: boolean,
unbind = true,
): void => {
if (!newVal) {
return;
}
setIsWidgetHeaderContextMenuOpen(false);
const symbolChanged =
currentInstrumentRef.current !== newVal;
const nextInstrumentName =
newName?.trim() ??
(symbolChanged
? ''
: currentInstrumentNameRef.current);
const nameChanged =
currentInstrumentNameRef.current !== nextInstrumentName;
if (!symbolChanged && !nameChanged) {
return;
}
if (symbolChanged) {
currentInstrumentRef.current = newVal;
setCurrentInstrument(newVal);
if (unbind) {
dispatch(unbindWidgets({ widgetId }));
}
}
currentInstrumentNameRef.current = nextInstrumentName;
setCurrentInstrumentName(nextInstrumentName);
// при смене инструмента очищаем индикативные данные виджета график
// т.к. логика для графика индикатива построена на наличии в widgetContentProps данных indicativeData
saveContentProps({
savedInstrument: newVal,
savedInstrumentName: nextInstrumentName || undefined,
withUpdate,
cleanIndicativeData: symbolChanged,
});
if (symbolChanged) {
triggerRelatedWidgetsToUpdateRef.current(newVal);
}
},
[dispatch, saveContentProps, widgetId],
);
const addInstrumentFromModal = useCallback(
(instruments: SelectedInstrument[]) => {
const instrument = instruments[0];
if (!instrument?.issKey) {
return;
}
onInstrumentChange(
instrument.issKey,
instrument.displayName,
);
},
[onInstrumentChange],
);
const onInstrumentChangeFromBind = useCallback(
(instrumentId: string, instrumentName?: string) => {
onInstrumentChange(
instrumentId,
instrumentName,
true,
false,
);
},
[onInstrumentChange],
);
const onDropInstruments = useCallback(
(
instrument: SelectedInstrument,
withUpdate?: boolean,
): void => {
if (!instrument.issKey) {
return;
}
onInstrumentChange(
instrument.issKey,
instrument.displayName,
withUpdate,
);
},
[onInstrumentChange],
);
const fieldValue = getMasterInstrumentFromPublicContext();
if (!fieldValue && widget?.master) {
const defaultInstrument = instruments.find(
(item) => item.issKey === DEFAULT_SYMBOL,
);
setCurrInstrument(
DEFAULT_SYMBOL,
defaultInstrument?.displayName,
);
return;
}
const instrument = instruments.find(
(item) => item.issKey === fieldValue,
);
if (instrument?.issKey) {
setCurrInstrument(
instrument.issKey,
instrument.displayName,
);
}