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

54 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-04-10 20:57:50 +02:00
import * as React from "react";
2020-09-26 01:22:21 +02:00
import {RemoteIcon} from "tc-shared/file/Icons";
import {useState} from "react";
2020-04-10 20:57:50 +02:00
const cssStyle = require("./Icon.scss");
2020-09-24 15:51:22 +02:00
export const IconRenderer = (props: {
2020-09-26 01:22:21 +02:00
icon: string;
title?: string;
2020-09-24 15:51:22 +02:00
className?: string;
}) => {
if(!props.icon) {
return <div className={cssStyle.empty + " icon-container icon-empty " + props.className} title={props.title} />;
2020-09-24 15:51:22 +02:00
} else if(typeof props.icon === "string") {
return <div className={"icon " + props.icon + " " + props.className} title={props.title} />;
} else {
throw "JQuery icons are not longer supported";
}
}
2020-09-26 01:22:21 +02:00
export const RemoteIconRenderer = (props: { icon: RemoteIcon, className?: string, title?: string }) => {
const [ revision, setRevision ] = useState(0);
2020-09-26 01:22:21 +02:00
props.icon.events.reactUse("notify_state_changed", () => setRevision(revision + 1));
2020-04-10 20:57:50 +02:00
2020-09-26 01:22:21 +02:00
switch (props.icon.getState()) {
case "empty":
case "destroyed":
return <div key={"empty"} className={"icon-container icon-empty " + props.className} title={props.title} />;
2020-04-10 20:57:50 +02:00
2020-09-26 01:22:21 +02:00
case "loaded":
if(props.icon.iconId >= 0 && props.icon.iconId <= 1000) {
if(props.icon.iconId === 0) {
return <div key={"loaded-empty"} className={"icon-container icon-empty " + props.className} title={props.title} />;
}
2020-04-10 20:57:50 +02:00
2020-09-26 01:22:21 +02:00
return <div key={"loaded"} className={"icon_em client-group_" + props.icon.iconId + " " + props.className} title={props.title} />;
}
2020-09-26 01:22:21 +02:00
return (
2020-09-26 11:17:55 +02:00
<div key={"icon-" + props.icon.iconId} className={"icon-container " + props.className}>
2020-09-26 01:22:21 +02:00
<img style={{ maxWidth: "100%", maxHeight: "100%" }} src={props.icon.getImageUrl()} alt={props.title || ("icon " + props.icon.iconId)} draggable={false} />
</div>
);
2020-04-10 20:57:50 +02:00
2020-09-26 01:22:21 +02:00
case "loading":
return <div key={"loading"} className={"icon-container " + props.className} title={props.title}><div className={"icon_loading"} /></div>;
2020-09-26 01:22:21 +02:00
case "error":
return <div key={"error"} className={"icon client-warning " + props.className} title={props.icon.getErrorMessage() || tr("Failed to load icon")} />;
2020-06-15 16:56:05 +02:00
2020-09-26 01:22:21 +02:00
default:
throw "invalid icon state";
2020-06-15 16:56:05 +02:00
}
2020-09-26 01:22:21 +02:00
};