-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from spacebarchat/fix/message-rendering
Fix message rendering
- Loading branch information
Showing
8 changed files
with
161 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { APIAttachment } from "@spacebarchat/spacebar-api-types/v9"; | ||
import React from "react"; | ||
import { PuffLoader } from "react-spinners"; | ||
import styled from "styled-components"; | ||
import { calculateImageRatio, calculateScaledDimensions } from "../utils/Message"; | ||
|
||
const Container = styled.div` | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
min-height: 300px; | ||
min-width: 300px; | ||
background-color: var(--background-primary); | ||
border-radius: 10px; | ||
`; | ||
|
||
interface Props { | ||
attachment: APIAttachment; | ||
} | ||
|
||
function Video({ attachment }: Props) { | ||
const ref = React.useRef<HTMLVideoElement>(null); | ||
const [isLoading, setLoading] = React.useState(true); | ||
const [dimensions, setDimensions] = React.useState({ width: 0, height: 0 }); | ||
const [isErrored, setErrored] = React.useState(false); | ||
|
||
const url = attachment.proxy_url && attachment.proxy_url.length > 0 ? attachment.proxy_url : attachment.url; | ||
|
||
const onLoadedMetadata = (e: React.SyntheticEvent<HTMLVideoElement, Event>) => { | ||
const video = e.target as HTMLVideoElement; | ||
const width = video.videoWidth; | ||
const height = video.videoHeight; | ||
const ratio = calculateImageRatio(width, height, 300, 300); | ||
const scaledDimensions = calculateScaledDimensions(width, height, ratio, 300, 300); | ||
setDimensions({ width: scaledDimensions.scaledWidth, height: scaledDimensions.scaledHeight }); | ||
setLoading(false); | ||
}; | ||
|
||
const onError = () => { | ||
setErrored(true); | ||
}; | ||
|
||
// TODO: poster | ||
// TODO: the server doesn't return height and width yet for videos | ||
return ( | ||
<> | ||
{isLoading && !isErrored && ( | ||
<Container> | ||
<PuffLoader size={"42px"} color="var(--primary)" /> | ||
</Container> | ||
)} | ||
{isErrored && ( | ||
<Container> | ||
<p>Failed to load video</p> | ||
</Container> | ||
)} | ||
|
||
{!isErrored && ( | ||
<video | ||
style={isLoading ? { display: "none" } : {}} | ||
playsInline | ||
controls | ||
preload="metadata" | ||
width={dimensions.width} | ||
height={dimensions.height} | ||
ref={ref} | ||
onLoadedMetadata={onLoadedMetadata} | ||
onError={onError} | ||
> | ||
<source src={url} type={attachment.content_type} /> | ||
</video> | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
export default Video; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.