|
| 1 | +import { TSR } from '@sofie-automation/blueprints-integration' |
| 2 | +import { StudioId } from '@sofie-automation/corelib/dist/dataModel/Ids' |
| 3 | +import { PeripheralDevice } from '@sofie-automation/corelib/dist/dataModel/PeripheralDevice' |
| 4 | +import { unprotectString } from '@sofie-automation/corelib/dist/protectedString' |
| 5 | +import { isTranslatableMessage, translateMessage } from '@sofie-automation/corelib/dist/TranslatableMessage' |
| 6 | +import { DEFAULT_TSR_ACTION_TIMEOUT_TIME } from '@sofie-automation/shared-lib/dist/core/constants' |
| 7 | +import { PeripheralDeviceType } from '@sofie-automation/shared-lib/dist/peripheralDevice/peripheralDeviceAPI' |
| 8 | +import React, { memo, useCallback } from 'react' |
| 9 | +import { useTranslation } from 'react-i18next' |
| 10 | +import { PeripheralDevices } from '../../collections' |
| 11 | +import { callPeripheralDeviceAction } from '../../lib/clientAPI' |
| 12 | +import { doModalDialog } from '../../lib/ModalDialog' |
| 13 | +import { NotificationCenter, NoticeLevel, Notification } from '../../lib/notifications/notifications' |
| 14 | +import { useTracker } from '../../lib/ReactMeteorData/ReactMeteorData' |
| 15 | +import { i18nTranslator } from '../i18n' |
| 16 | + |
| 17 | +export const CasparCGRestartButtons = memo(function CasparCGRestartButtons({ studioId }: { studioId: StudioId }) { |
| 18 | + const { t } = useTranslation() |
| 19 | + |
| 20 | + const casparCGPlayoutDevices = useTracker( |
| 21 | + () => |
| 22 | + PeripheralDevices.find({ |
| 23 | + parentDeviceId: { |
| 24 | + $in: PeripheralDevices.find({ |
| 25 | + 'studioAndConfigId.studioId': studioId, |
| 26 | + }) |
| 27 | + .fetch() |
| 28 | + .map((i) => i._id), |
| 29 | + }, |
| 30 | + type: PeripheralDeviceType.PLAYOUT, |
| 31 | + subType: TSR.DeviceType.CASPARCG, |
| 32 | + }).fetch(), |
| 33 | + [studioId], |
| 34 | + [] |
| 35 | + ) |
| 36 | + |
| 37 | + const onRestartCasparCG = useCallback( |
| 38 | + (e: React.MouseEvent<HTMLButtonElement>, device: PeripheralDevice) => { |
| 39 | + e.persist() |
| 40 | + |
| 41 | + doModalDialog({ |
| 42 | + title: t('Restart CasparCG Server'), |
| 43 | + message: t('Do you want to restart CasparCG Server "{{device}}"?', { device: device.name }), |
| 44 | + onAccept: () => { |
| 45 | + callPeripheralDeviceAction(e, device._id, DEFAULT_TSR_ACTION_TIMEOUT_TIME, TSR.CasparCGActions.RestartServer) |
| 46 | + .then((r) => { |
| 47 | + if (r?.result === TSR.ActionExecutionResultCode.Error) { |
| 48 | + throw new Error( |
| 49 | + r.response && isTranslatableMessage(r.response) |
| 50 | + ? translateMessage(r.response, i18nTranslator) |
| 51 | + : t('Unknown error') |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + NotificationCenter.push( |
| 56 | + new Notification( |
| 57 | + undefined, |
| 58 | + NoticeLevel.NOTIFICATION, |
| 59 | + t('CasparCG on device "{{deviceName}}" restarting...', { deviceName: device.name }), |
| 60 | + 'SystemStatus' |
| 61 | + ) |
| 62 | + ) |
| 63 | + }) |
| 64 | + .catch((err) => { |
| 65 | + NotificationCenter.push( |
| 66 | + new Notification( |
| 67 | + undefined, |
| 68 | + NoticeLevel.WARNING, |
| 69 | + t('Failed to restart CasparCG on device: "{{deviceName}}": {{errorMessage}}', { |
| 70 | + deviceName: device.name, |
| 71 | + errorMessage: err + '', |
| 72 | + }), |
| 73 | + 'SystemStatus' |
| 74 | + ) |
| 75 | + ) |
| 76 | + }) |
| 77 | + }, |
| 78 | + }) |
| 79 | + }, |
| 80 | + [t] |
| 81 | + ) |
| 82 | + |
| 83 | + return ( |
| 84 | + <> |
| 85 | + {casparCGPlayoutDevices.map((i) => ( |
| 86 | + <React.Fragment key={unprotectString(i._id)}> |
| 87 | + <button className="btn btn-secondary" onClick={(e) => onRestartCasparCG(e, i)}> |
| 88 | + {t('Restart {{device}}', { device: i.name })} |
| 89 | + </button> |
| 90 | + <hr /> |
| 91 | + </React.Fragment> |
| 92 | + ))} |
| 93 | + </> |
| 94 | + ) |
| 95 | +}) |
0 commit comments