2019-09-18 19:52:39 +02:00
|
|
|
import React, { Component } from "react";
|
|
|
|
import {
|
|
|
|
withStyles,
|
|
|
|
CircularProgress,
|
|
|
|
Typography,
|
|
|
|
Paper
|
|
|
|
} from "@material-ui/core";
|
|
|
|
import { styles } from "./PageLayout.styles";
|
|
|
|
import Post from "../components/Post";
|
|
|
|
import { Status } from "../types/Status";
|
|
|
|
import { Context } from "../types/Context";
|
|
|
|
import Mastodon from "megalodon";
|
|
|
|
import { withSnackbar } from "notistack";
|
2019-04-01 03:56:41 +02:00
|
|
|
|
|
|
|
interface IConversationPageState {
|
|
|
|
posts?: [Status];
|
|
|
|
viewIsLoading: boolean;
|
|
|
|
viewDidLoad?: boolean;
|
|
|
|
viewDidError?: boolean;
|
|
|
|
viewDidErrorCode?: any;
|
|
|
|
conversationId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Conversation extends Component<any, IConversationPageState> {
|
|
|
|
client: Mastodon;
|
|
|
|
streamListener: any;
|
|
|
|
|
|
|
|
constructor(props: any) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
viewIsLoading: true,
|
|
|
|
conversationId: props.match.params.conversationId
|
2019-09-18 19:52:39 +02:00
|
|
|
};
|
2019-04-01 03:56:41 +02:00
|
|
|
|
2019-09-18 19:52:39 +02:00
|
|
|
this.client = new Mastodon(
|
|
|
|
localStorage.getItem("access_token") as string,
|
|
|
|
(localStorage.getItem("baseurl") as string) + "/api/v1"
|
|
|
|
);
|
2019-04-01 03:56:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getContext() {
|
2019-09-18 19:52:39 +02:00
|
|
|
this.client
|
|
|
|
.get(`/statuses/${this.state.conversationId}`)
|
|
|
|
.then((resp: any) => {
|
|
|
|
let result: Status = resp.data;
|
|
|
|
this.setState({ posts: [result] });
|
2019-04-01 03:56:41 +02:00
|
|
|
})
|
2019-09-18 19:52:39 +02:00
|
|
|
.catch((err: Error) => {
|
|
|
|
this.setState({
|
|
|
|
viewIsLoading: false,
|
|
|
|
viewDidError: true,
|
|
|
|
viewDidErrorCode: err.message
|
|
|
|
});
|
|
|
|
this.props.enqueueSnackbar(
|
|
|
|
"Couldn't get conversation: " + err.name,
|
|
|
|
{ variant: "error" }
|
|
|
|
);
|
2019-04-01 03:56:41 +02:00
|
|
|
});
|
2019-09-18 19:52:39 +02:00
|
|
|
this.client
|
|
|
|
.get(`/statuses/${this.state.conversationId}/context`)
|
|
|
|
.then((resp: any) => {
|
|
|
|
let context: Context = resp.data;
|
|
|
|
let posts = this.state.posts;
|
|
|
|
let array: any[] = [];
|
|
|
|
if (posts) {
|
|
|
|
array = array
|
|
|
|
.concat(context.ancestors)
|
|
|
|
.concat(posts)
|
|
|
|
.concat(context.descendants);
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
posts: array as [Status],
|
|
|
|
viewIsLoading: false,
|
|
|
|
viewDidLoad: true,
|
|
|
|
viewDidError: false
|
|
|
|
});
|
2019-04-01 03:56:41 +02:00
|
|
|
})
|
2019-09-18 19:52:39 +02:00
|
|
|
.catch((err: Error) => {
|
|
|
|
this.setState({
|
|
|
|
viewIsLoading: false,
|
|
|
|
viewDidError: true,
|
|
|
|
viewDidErrorCode: err.message
|
|
|
|
});
|
|
|
|
this.props.enqueueSnackbar(
|
|
|
|
"Couldn't get conversation: " + err.name,
|
|
|
|
{ variant: "error" }
|
|
|
|
);
|
|
|
|
});
|
2019-04-01 03:56:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(props: any) {
|
|
|
|
if (props.match.params.conversationId !== this.state.conversationId) {
|
2019-09-18 19:52:39 +02:00
|
|
|
this.getContext();
|
2019-04-01 03:56:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
2019-09-18 19:52:39 +02:00
|
|
|
this.getContext();
|
2019-04-01 03:56:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
2019-09-18 19:52:39 +02:00
|
|
|
const where: HTMLElement | null = document.getElementById(
|
|
|
|
`post_${this.state.conversationId}`
|
|
|
|
);
|
|
|
|
if (
|
|
|
|
where &&
|
|
|
|
this.state.posts &&
|
|
|
|
this.state.posts[0].id !== this.state.conversationId
|
|
|
|
) {
|
2019-04-01 03:56:41 +02:00
|
|
|
window.scrollTo(0, where.getBoundingClientRect().top);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-09-18 19:52:39 +02:00
|
|
|
const { classes } = this.props;
|
2019-04-01 03:56:41 +02:00
|
|
|
return (
|
|
|
|
<div className={classes.pageLayoutMaxConstraints}>
|
2019-09-18 19:52:39 +02:00
|
|
|
{this.state.posts ? (
|
2019-04-01 03:56:41 +02:00
|
|
|
<div>
|
2019-09-18 19:52:39 +02:00
|
|
|
{this.state.posts.map((post: Status) => {
|
|
|
|
return (
|
|
|
|
<Post
|
|
|
|
key={post.id}
|
|
|
|
post={post}
|
|
|
|
client={this.client}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<span />
|
|
|
|
)}
|
|
|
|
{this.state.viewDidError ? (
|
|
|
|
<Paper className={classes.errorCard}>
|
|
|
|
<Typography variant="h4">Bummer.</Typography>
|
|
|
|
<Typography variant="h6">
|
|
|
|
Something went wrong when loading this conversation.
|
|
|
|
</Typography>
|
|
|
|
<Typography>
|
|
|
|
{this.state.viewDidErrorCode
|
|
|
|
? this.state.viewDidErrorCode
|
|
|
|
: ""}
|
|
|
|
</Typography>
|
|
|
|
</Paper>
|
|
|
|
) : (
|
|
|
|
<span />
|
|
|
|
)}
|
|
|
|
{this.state.viewIsLoading ? (
|
|
|
|
<div style={{ textAlign: "center" }}>
|
|
|
|
<CircularProgress
|
|
|
|
className={classes.progress}
|
|
|
|
color="primary"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<span />
|
|
|
|
)}
|
2019-04-01 03:56:41 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withStyles(styles)(withSnackbar(Conversation));
|