mirror of
https://github.com/revoltchat/revite.git
synced 2024-11-22 15:10:57 -05:00
Merge pull request #70 from MichailiK/feature/message/image-loading-background
This commit is contained in:
commit
a17487606f
3 changed files with 57 additions and 13 deletions
|
@ -92,4 +92,10 @@
|
||||||
|
|
||||||
.image {
|
.image {
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
|
||||||
|
&.loading {
|
||||||
|
background: var(--background);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ import AttachmentActions from "./AttachmentActions";
|
||||||
import { SizedGrid } from "./Grid";
|
import { SizedGrid } from "./Grid";
|
||||||
import Spoiler from "./Spoiler";
|
import Spoiler from "./Spoiler";
|
||||||
import TextFile from "./TextFile";
|
import TextFile from "./TextFile";
|
||||||
|
import ImageFile from "./ImageFile";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
attachment: AttachmentI;
|
attachment: AttachmentI;
|
||||||
|
@ -21,7 +22,6 @@ const MAX_ATTACHMENT_WIDTH = 480;
|
||||||
|
|
||||||
export default function Attachment({ attachment, hasContent }: Props) {
|
export default function Attachment({ attachment, hasContent }: Props) {
|
||||||
const client = useContext(AppContext);
|
const client = useContext(AppContext);
|
||||||
const { openScreen } = useIntermediate();
|
|
||||||
const { filename, metadata } = attachment;
|
const { filename, metadata } = attachment;
|
||||||
const [spoiler, setSpoiler] = useState(filename.startsWith("SPOILER_"));
|
const [spoiler, setSpoiler] = useState(filename.startsWith("SPOILER_"));
|
||||||
|
|
||||||
|
@ -41,18 +41,7 @@ export default function Attachment({ attachment, hasContent }: Props) {
|
||||||
[styles.margin]: hasContent,
|
[styles.margin]: hasContent,
|
||||||
spoiler,
|
spoiler,
|
||||||
})}>
|
})}>
|
||||||
<img
|
<ImageFile attachment={attachment} />
|
||||||
src={url}
|
|
||||||
alt={filename}
|
|
||||||
className={styles.image}
|
|
||||||
loading="lazy"
|
|
||||||
onClick={() =>
|
|
||||||
openScreen({ id: "image_viewer", attachment })
|
|
||||||
}
|
|
||||||
onMouseDown={(ev) =>
|
|
||||||
ev.button === 1 && window.open(url, "_blank")
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
{spoiler && <Spoiler set={setSpoiler} />}
|
{spoiler && <Spoiler set={setSpoiler} />}
|
||||||
</SizedGrid>
|
</SizedGrid>
|
||||||
);
|
);
|
||||||
|
|
49
src/components/common/messaging/attachments/ImageFile.tsx
Normal file
49
src/components/common/messaging/attachments/ImageFile.tsx
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
import {Attachment} from "revolt-api/types/Autumn";
|
||||||
|
|
||||||
|
import styles from "./Attachment.module.scss";
|
||||||
|
import classNames from "classnames";
|
||||||
|
import {useContext, useState} from "preact/hooks";
|
||||||
|
|
||||||
|
import {useIntermediate} from "../../../../context/intermediate/Intermediate";
|
||||||
|
import {AppContext} from "../../../../context/revoltjs/RevoltClient";
|
||||||
|
|
||||||
|
enum ImageLoadingState
|
||||||
|
{
|
||||||
|
Loading,
|
||||||
|
Loaded,
|
||||||
|
Error
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
attachment: Attachment;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function ImageFile({ attachment }: Props)
|
||||||
|
{
|
||||||
|
const [loading, setLoading] = useState(ImageLoadingState.Loading);
|
||||||
|
const client = useContext(AppContext);
|
||||||
|
const { openScreen } = useIntermediate();
|
||||||
|
const url = client.generateFileURL(attachment)!;
|
||||||
|
|
||||||
|
return <img
|
||||||
|
src={url}
|
||||||
|
alt={attachment.filename}
|
||||||
|
loading="lazy"
|
||||||
|
className={classNames(styles.image, {
|
||||||
|
[styles.loading]: loading !== ImageLoadingState.Loaded
|
||||||
|
})}
|
||||||
|
onClick={() =>
|
||||||
|
openScreen({id: "image_viewer", attachment})
|
||||||
|
}
|
||||||
|
onMouseDown={(ev) =>
|
||||||
|
ev.button === 1 && window.open(url, "_blank")
|
||||||
|
}
|
||||||
|
onLoad={() =>
|
||||||
|
setLoading(ImageLoadingState.Loaded)
|
||||||
|
}
|
||||||
|
onError={() =>
|
||||||
|
setLoading(ImageLoadingState.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
/>
|
||||||
|
}
|
Loading…
Reference in a new issue