lemmy-ui/src/shared/components/common/pictrs-image.tsx

85 lines
2.2 KiB
TypeScript
Raw Normal View History

import classNames from "classnames";
2021-02-22 02:39:04 +00:00
import { Component } from "inferno";
const iconThumbnailSize = 96;
const thumbnailSize = 256;
interface PictrsImageProps {
src: string;
alt?: string;
icon?: boolean;
2022-01-06 16:26:32 +00:00
banner?: boolean;
thumbnail?: boolean;
nsfw?: boolean;
iconOverlay?: boolean;
pushup?: boolean;
}
export class PictrsImage extends Component<PictrsImageProps, any> {
constructor(props: any, context: any) {
super(props, context);
}
render() {
return (
<picture>
2021-02-22 02:39:04 +00:00
<source srcSet={this.src("webp")} type="image/webp" />
<source srcSet={this.props.src} />
2021-02-22 02:39:04 +00:00
<source srcSet={this.src("jpg")} type="image/jpeg" />
<img
src={this.props.src}
alt={this.alt()}
loading="lazy"
className={classNames({
"img-fluid": !this.props.icon && !this.props.iconOverlay,
banner: this.props.banner,
"thumbnail rounded":
this.props.thumbnail && !this.props.icon && !this.props.banner,
"img-expanded slight-radius":
!this.props.thumbnail && !this.props.icon,
"img-blur": this.props.thumbnail && this.props.nsfw,
"rounded-circle img-icon mr-2": this.props.icon,
"ml-2 mb-0 rounded-circle avatar-overlay": this.props.iconOverlay,
"avatar-pushup": this.props.pushup,
})}
/>
</picture>
);
}
src(format: string): string {
// sample url:
// http://localhost:8535/pictrs/image/file.png?thumbnail=256&format=jpg
2023-06-05 21:31:12 +00:00
const split = this.props.src.split("/pictrs/image/");
// If theres not multiple, then its not a pictrs image
if (split.length == 1) {
return this.props.src;
}
2023-06-05 21:31:12 +00:00
const host = split[0];
const path = split[1];
2023-06-05 21:31:12 +00:00
const params = { format };
if (this.props.thumbnail) {
2021-02-22 02:39:04 +00:00
params["thumbnail"] = thumbnailSize;
} else if (this.props.icon) {
2021-02-22 02:39:04 +00:00
params["thumbnail"] = iconThumbnailSize;
}
2023-06-05 21:31:12 +00:00
const paramsStr = new URLSearchParams(params).toString();
const out = `${host}/pictrs/image/${path}?${paramsStr}`;
return out;
}
alt(): string {
if (this.props.icon) {
2021-02-22 02:39:04 +00:00
return "";
}
2021-02-22 02:39:04 +00:00
return this.props.alt || "";
}
}