@sberdevices/plasma-temple
Version:
SberDevices CanvasApp Templates.
85 lines (74 loc) • 2.41 kB
JavaScript
import { useCallback } from 'react';
import { isSberPortal } from '@sberdevices/plasma-ui/utils';
import { useAssistantOnNavigation } from './useAssistantOnNavigation';
var defaultStepSize = /*#__PURE__*/isSberPortal() ? 3 : 4;
export var useVoiceNavigation = function useVoiceNavigation(_ref) {
var index = _ref.index,
setIndex = _ref.setIndex,
_ref$minIndex = _ref.minIndex,
minIndex = _ref$minIndex === void 0 ? 0 : _ref$minIndex,
maxIndex = _ref.maxIndex,
_ref$stepSize = _ref.stepSize,
stepSize = _ref$stepSize === void 0 ? defaultStepSize : _ref$stepSize,
disabled = _ref.disabled,
axis = _ref.axis,
main = _ref.main;
var onNavigate = useCallback(function (command) {
if (disabled) {
return;
}
var direction = command.navigation.command;
var isNext = axis === 'x' ? direction === 'RIGHT' : direction === 'DOWN';
var isPrevious = axis === 'x' ? direction === 'LEFT' : direction === 'UP';
if (isNext || main && direction === 'FORWARD') {
setIndex(maxIndex ? Math.min(maxIndex, index + stepSize) : index + stepSize);
} else if (isPrevious) {
setIndex(Math.max(minIndex, index - stepSize));
}
}, [axis, disabled, index, maxIndex, minIndex, setIndex, stepSize, main]);
useAssistantOnNavigation(onNavigate);
};
export var useVoiceNavigationWithSpatNav = function useVoiceNavigationWithSpatNav(_ref2) {
var axis = _ref2.axis,
main = _ref2.main;
var onNavigate = useCallback(function (command) {
var direction = command.navigation.command;
if (axis === 'x') {
switch (direction) {
case 'LEFT':
window.navigate('left');
break;
case 'RIGHT':
window.navigate('right');
break;
case 'FORWARD':
{
if (main) {
window.navigate('right');
}
break;
}
default:
}
} else {
switch (direction) {
case 'DOWN':
window.navigate('down');
break;
case 'UP':
window.navigate('up');
break;
case 'FORWARD':
{
if (main) {
window.navigate('down');
}
break;
}
default:
}
}
}, [axis, main]);
useAssistantOnNavigation(onNavigate);
};
//# sourceMappingURL=useVoiceNavigation.js.map