owncast/web/utils/input-statuses.tsx

69 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-01-31 19:13:35 +01:00
import {
CheckCircleFilled,
ExclamationCircleFilled,
LoadingOutlined,
WarningOutlined,
} from '@ant-design/icons';
2021-01-31 04:33:01 +01:00
export const STATUS_RESET_TIMEOUT = 3000;
export const STATUS_ERROR = 'error';
export const STATUS_INVALID = 'invalid';
export const STATUS_PROCESSING = 'proessing';
export const STATUS_SUCCESS = 'success';
export const STATUS_WARNING = 'warning';
export type InputStatusTypes = 'error' | 'invalid' | 'proessing' | 'success' | 'warning';
2021-01-31 04:33:01 +01:00
export interface StatusState {
2021-01-31 19:13:35 +01:00
type: InputStatusTypes;
2021-01-31 04:33:01 +01:00
icon: any; // Element type of sorts?
message: string;
}
interface InputStates {
[key: string]: StatusState;
}
2021-01-31 04:33:01 +01:00
export const INPUT_STATES: InputStates = {
2021-01-31 04:33:01 +01:00
[STATUS_SUCCESS]: {
type: STATUS_SUCCESS,
2021-01-31 04:33:01 +01:00
icon: <CheckCircleFilled style={{ color: 'green' }} />,
message: 'Success!',
},
[STATUS_ERROR]: {
type: STATUS_ERROR,
2021-01-31 04:33:01 +01:00
icon: <ExclamationCircleFilled style={{ color: 'red' }} />,
2021-01-31 19:13:35 +01:00
message: 'An error occurred.',
2021-01-31 04:33:01 +01:00
},
[STATUS_INVALID]: {
type: STATUS_INVALID,
2021-01-31 04:33:01 +01:00
icon: <ExclamationCircleFilled style={{ color: 'red' }} />,
2021-01-31 19:13:35 +01:00
message: 'An error occurred.',
2021-01-31 04:33:01 +01:00
},
[STATUS_PROCESSING]: {
type: STATUS_PROCESSING,
2021-01-31 04:33:01 +01:00
icon: <LoadingOutlined />,
message: '',
},
[STATUS_WARNING]: {
type: STATUS_WARNING,
2021-01-31 04:33:01 +01:00
icon: <WarningOutlined style={{ color: '#fc0' }} />,
2021-01-31 19:13:35 +01:00
message: '',
2021-01-31 04:33:01 +01:00
},
};
// Don't like any of the default messages in INPUT_STATES? Create a state with custom message by providing an icon style with your message.
2021-01-31 09:48:39 +01:00
export function createInputStatus(type: InputStatusTypes, message?: string): StatusState {
2021-01-31 04:33:01 +01:00
if (!type || !INPUT_STATES[type]) {
return null;
}
2021-01-31 19:13:35 +01:00
if (!message) {
2021-01-31 04:33:01 +01:00
return INPUT_STATES[type];
}
return {
2021-01-31 19:13:35 +01:00
type,
2021-01-31 04:33:01 +01:00
icon: INPUT_STATES[type].icon,
message,
};
}