Files
inji-wallet/screens/Scan/ScanScreenController.ts
2022-09-30 17:43:06 +08:00

50 lines
1.7 KiB
TypeScript

import { useSelector } from '@xstate/react';
import { useContext } from 'react';
import { useTranslation } from 'react-i18next';
import {
ScanEvents,
selectIsLocationDisabled,
selectIsLocationDenied,
selectIsScanning,
} from '../../machines/scan';
import { selectVcLabel } from '../../machines/settings';
import { selectShareableVcs } from '../../machines/vc';
import { GlobalContext } from '../../shared/GlobalContext';
export function useScanScreen() {
const { t } = useTranslation('ScanScreen');
const { appService } = useContext(GlobalContext);
const scanService = appService.children.get('scan');
const settingsService = appService.children.get('settings');
const vcService = appService.children.get('vc');
const shareableVcs = useSelector(vcService, selectShareableVcs);
const isLocationDisabled = useSelector(scanService, selectIsLocationDisabled);
const isLocationDenied = useSelector(scanService, selectIsLocationDenied);
const locationError = { message: '', button: '' };
if (isLocationDisabled) {
locationError.message = t('errors.locationDisabled.message');
locationError.button = t('errors.locationDisabled.button');
} else if (isLocationDenied) {
locationError.message = t('errors.locationDenied.message');
locationError.button = t('errors.locationDenied.button');
}
return {
locationError,
vcLabel: useSelector(settingsService, selectVcLabel),
isEmpty: !shareableVcs.length,
isLocationDisabled,
isLocationDenied,
isScanning: useSelector(scanService, selectIsScanning),
LOCATION_REQUEST: () => scanService.send(ScanEvents.LOCATION_REQUEST()),
SCAN: (qrCode: string) => scanService.send(ScanEvents.SCAN(qrCode)),
};
}