owncast/web/utils/input-statuses.tsx

81 lines
2 KiB
TypeScript
Raw Normal View History

import dynamic from 'next/dynamic';
// Lazy loaded components
const CheckCircleFilled = dynamic(() => import('@ant-design/icons/CheckCircleFilled'), {
ssr: false,
});
const ExclamationCircleFilled = dynamic(() => import('@ant-design/icons/ExclamationCircleFilled'), {
ssr: false,
});
const LoadingOutlined = dynamic(() => import('@ant-design/icons/LoadingOutlined'), {
ssr: false,
});
const WarningOutlined = dynamic(() => import('@ant-design/icons/WarningOutlined'), {
ssr: false,
});
2021-01-31 06:33:01 +03:00
export const STATUS_ERROR = 'error';
export const STATUS_PROCESSING = 'proessing';
export const STATUS_SUCCESS = 'success';
export const STATUS_WARNING = 'warning';
const STATUS_INVALID = 'invalid';
export type InputStatusTypes = 'error' | 'invalid' | 'proessing' | 'success' | 'warning';
2021-01-31 06:33:01 +03:00
export interface StatusState {
2021-01-31 21:13:35 +03:00
type: InputStatusTypes;
2021-01-31 06:33:01 +03:00
icon: any; // Element type of sorts?
message: string;
}
interface InputStates {
[key: string]: StatusState;
}
2021-01-31 06:33:01 +03:00
const INPUT_STATES: InputStates = {
2021-01-31 06:33:01 +03:00
[STATUS_SUCCESS]: {
type: STATUS_SUCCESS,
2021-01-31 06:33:01 +03:00
icon: <CheckCircleFilled style={{ color: 'green' }} />,
message: 'Success!',
},
[STATUS_ERROR]: {
type: STATUS_ERROR,
2021-01-31 06:33:01 +03:00
icon: <ExclamationCircleFilled style={{ color: 'red' }} />,
2021-01-31 21:13:35 +03:00
message: 'An error occurred.',
2021-01-31 06:33:01 +03:00
},
[STATUS_INVALID]: {
type: STATUS_INVALID,
2021-01-31 06:33:01 +03:00
icon: <ExclamationCircleFilled style={{ color: 'red' }} />,
2021-01-31 21:13:35 +03:00
message: 'An error occurred.',
2021-01-31 06:33:01 +03:00
},
[STATUS_PROCESSING]: {
type: STATUS_PROCESSING,
2021-01-31 06:33:01 +03:00
icon: <LoadingOutlined />,
message: '',
},
[STATUS_WARNING]: {
type: STATUS_WARNING,
2021-01-31 06:33:01 +03:00
icon: <WarningOutlined style={{ color: '#fc0' }} />,
2021-01-31 21:13:35 +03:00
message: '',
2021-01-31 06:33:01 +03: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 11:48:39 +03:00
export function createInputStatus(type: InputStatusTypes, message?: string): StatusState {
2021-01-31 06:33:01 +03:00
if (!type || !INPUT_STATES[type]) {
return null;
}
2021-01-31 21:13:35 +03:00
if (!message) {
2021-01-31 06:33:01 +03:00
return INPUT_STATES[type];
}
return {
2021-01-31 21:13:35 +03:00
type,
2021-01-31 06:33:01 +03:00
icon: INPUT_STATES[type].icon,
message,
};
}