TeaWeb/shared/js/ui/react-elements/Icon.tsx

68 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-04-10 20:57:50 +02:00
import * as React from "react";
2020-05-03 23:32:04 +02:00
import {LocalIcon} from "tc-shared/file/Icons";
2020-04-10 20:57:50 +02:00
2020-09-24 15:51:22 +02:00
export const IconRenderer = (props: {
icon: string | LocalIcon;
title?: string;
2020-09-24 15:51:22 +02:00
className?: string;
}) => {
if(!props.icon) {
return <div className={"icon-container icon-empty " + props.className} title={props.title} />;
} else if(typeof props.icon === "string") {
return <div className={"icon " + props.icon + " " + props.className} title={props.title} />;
} else if(props.icon instanceof LocalIcon) {
return <LocalIconRenderer icon={props.icon} title={props.title} className={props.className} />;
} else {
throw "JQuery icons are not longer supported";
}
}
export interface LoadedIconRenderer {
icon: LocalIcon;
title?: string;
2020-09-24 15:51:22 +02:00
className?: string;
}
export class LocalIconRenderer extends React.Component<LoadedIconRenderer, {}> {
private readonly callback_state_update;
2020-04-10 20:57:50 +02:00
constructor(props) {
super(props);
this.callback_state_update = () => {
const icon = this.props.icon;
if(icon.status !== "destroyed")
this.forceUpdate();
};
2020-04-10 20:57:50 +02:00
}
render() {
const icon = this.props.icon;
2020-04-21 16:17:21 +02:00
if(!icon || icon.status === "empty" || icon.status === "destroyed")
2020-09-24 15:51:22 +02:00
return <div key={"empty"} className={"icon-container icon-empty " + this.props.className} title={this.props.title} />;
2020-04-21 16:17:21 +02:00
else if(icon.status === "loaded") {
if(icon.icon_id >= 0 && icon.icon_id <= 1000) {
if(icon.icon_id === 0)
2020-06-15 16:56:05 +02:00
return <div key={"loaded-empty"} className={"icon-container icon-empty"} title={this.props.title} />;
return <div key={"loaded"} className={"icon_em client-group_" + icon.icon_id} />;
}
2020-09-25 20:36:23 +02:00
return <div key={"icon"} className={"icon-container " + this.props.className}><img style={{ maxWidth: "100%", maxHeight: "100%" }} src={icon.loaded_url} alt={this.props.title || ("icon " + icon.icon_id)} draggable={false} /></div>;
} else if(icon.status === "loading")
2020-09-24 15:51:22 +02:00
return <div key={"loading"} className={"icon-container " + this.props.className} title={this.props.title}><div className={"icon_loading"} /></div>;
else if(icon.status === "error")
2020-09-24 15:51:22 +02:00
return <div key={"error"} className={"icon client-warning " + this.props.className} title={icon.error_message || tr("Failed to load icon")} />;
2020-04-10 20:57:50 +02:00
}
componentDidMount(): void {
2020-04-21 16:17:21 +02:00
this.props.icon?.status_change_callbacks.push(this.callback_state_update);
2020-04-10 20:57:50 +02:00
}
2020-04-10 20:57:50 +02:00
componentWillUnmount(): void {
2020-04-21 16:17:21 +02:00
this.props.icon?.status_change_callbacks.remove(this.callback_state_update);
2020-04-10 20:57:50 +02:00
}
2020-06-15 16:56:05 +02:00
componentDidUpdate(prevProps: Readonly<LoadedIconRenderer>, prevState: Readonly<{}>, snapshot?: any): void {
prevProps.icon?.status_change_callbacks.remove(this.callback_state_update);
this.props.icon?.status_change_callbacks.push(this.callback_state_update);
}
2020-04-10 20:57:50 +02:00
}