+import axios from 'axios';
+import React, { useEffect, useState } from 'react';
+import { Helmet } from 'react-helmet';
+import { useParams } from 'react-router-dom';
+
+import NotFound from './NotFound';
+import CanonicalLinks from '../components/common/CanonicalLinks';
+import ErrorBoundary from '../components/common/ErrorBoundary';
+import ErrorMessage from '../components/common/ErrorMessage';
+import Loading from '../components/common/Loading';
+import Profile from '../components/users/Profile';
+
+const User = () => {
+ const params = useParams();
+ const { id } = params;
+
+ const [error, setError] = useState(null);
+ const [loading, setLoading] = useState(true);
+ const [user, setUser] = useState(null);
+
+ useEffect(() => {
+ setLoading(true);
+ const ctrl = new AbortController();
+ axios
+ .get(`/api/users/${id}`, { signal: ctrl.signal })
+ .then(response => {
+ setError(null);
+ setLoading(false);
+ setUser(response.data);
+ })
+ .catch(error => {
+ setError(error);
+ setLoading(false);
+ setUser(null);
+ });
+ return () => {
+ ctrl.abort();
+ };
+ }, [id]);
+
+ useEffect(() => {
+ const cb = (e) => {
+ if (e.user) {
+ setUser(user => e.user.id === user.id ? { ...user, ...e.user } : user);
+ }
+ };
+ window.Echo.channel('App.Control')
+ .listen('UserChanged', cb);
+ return () => {
+ window.Echo.channel('App.Control')
+ .stopListening('UserChanged', cb);
+ };
+ }, []);
+
+ if (loading) {
+ return <Loading />;
+ }
+
+ if (error) {
+ return <ErrorMessage error={error} />;
+ }
+
+ if (!user) {
+ return <NotFound />;
+ }
+
+ return <ErrorBoundary>
+ <Helmet>
+ <title>{user.nickname || user.username}</title>
+ </Helmet>
+ <CanonicalLinks base={`/users/${user.id}`} />
+ <Profile user={user} />
+ </ErrorBoundary>;
+};
+
+export default User;