Farmbot-Web-App/frontend/ui/fallback_img.tsx

55 lines
1.5 KiB
TypeScript
Raw Normal View History

2017-06-29 12:54:02 -06:00
import * as React from "react";
import { defensiveClone } from "../util";
2019-04-02 13:59:37 -06:00
import { t } from "../i18next_wrapper";
2017-06-29 12:54:02 -06:00
type ImgTag = React.HTMLProps<HTMLImageElement>;
2017-07-25 17:42:56 -06:00
2017-06-29 12:54:02 -06:00
interface Props extends ImgTag {
src: string;
fallback: string;
}
type State = Partial<{ needsFallback: boolean }>;
/** Like a normal `<img>`, but it has a `fallback` URL if the image does not
* load*/
export class FallbackImg extends React.Component<Props, State> {
2017-07-25 17:42:56 -06:00
state: State = { needsFallback: false };
2019-08-26 10:13:33 -06:00
UNSAFE_componentWillReceiveProps(next: Props) {
2017-06-29 12:54:02 -06:00
// Sorry. The webcam page needs live updates. <img/> tag was acting wonky.
(next.src !== this.props.src) && this.setState({ needsFallback: false });
}
fallback = () => {
2018-01-11 19:36:30 -07:00
return <div className="webcam-stream-unavailable">
<img src={this.props.fallback} style={{ maxWidth: "100%" }} />
2018-04-12 21:36:16 -06:00
<p>
2018-01-11 19:36:30 -07:00
{t("Unable to load webcam feed.")}
2018-04-12 21:36:16 -06:00
</p>
2018-01-11 19:36:30 -07:00
</div>;
2017-06-29 12:54:02 -06:00
}
dontFallback = () => {
2017-08-28 05:49:13 -06:00
const imgProps: Props = defensiveClone(this.props);
2017-08-25 08:19:04 -06:00
delete imgProps.fallback;
2018-01-11 19:36:30 -07:00
const onError = () => this.setState({ needsFallback: true });
const splitSrc = this.props.src.split(" ");
const displaySrc = () => {
switch (splitSrc[0]) {
case "iframe":
return <iframe src={splitSrc[1]} />;
default:
return <img src={this.props.src} onError={onError} />;
}
};
return <div className="webcam-stream-valid">
{displaySrc()}
</div>;
2017-06-29 12:54:02 -06:00
}
render() {
2018-01-11 19:36:30 -07:00
return (this.state.needsFallback ? this.fallback : this.dontFallback)();
2017-06-29 12:54:02 -06:00
}
}