2022-08-08 15:43:47 -07:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
|
|
|
import { useParams } from 'react-router-dom';
|
|
|
|
|
|
|
|
import { fetchAboutPage } from 'soapbox/actions/about';
|
|
|
|
import { useSoapboxConfig, useSettings, useAppDispatch } from 'soapbox/hooks';
|
|
|
|
|
|
|
|
import { languages } from '../preferences';
|
|
|
|
|
|
|
|
/** Displays arbitary user-uploaded HTML on a page at `/about/:slug` */
|
|
|
|
const AboutPage: React.FC = () => {
|
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
const { slug } = useParams<{ slug?: string }>();
|
|
|
|
|
|
|
|
const settings = useSettings();
|
|
|
|
const soapboxConfig = useSoapboxConfig();
|
|
|
|
|
|
|
|
const [pageHtml, setPageHtml] = useState<string>('');
|
|
|
|
const [locale, setLocale] = useState<string>(settings.get('locale'));
|
|
|
|
|
|
|
|
const { aboutPages } = soapboxConfig;
|
|
|
|
|
|
|
|
const page = aboutPages.get(slug || 'about');
|
|
|
|
const defaultLocale = page?.get('default') as string | undefined;
|
|
|
|
const pageLocales = page?.get('locales', []) as string[];
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchLocale = Boolean(page && locale !== defaultLocale && pageLocales.includes(locale));
|
|
|
|
dispatch(fetchAboutPage(slug, fetchLocale ? locale : undefined)).then(html => {
|
|
|
|
setPageHtml(html);
|
|
|
|
}).catch(error => {
|
|
|
|
// TODO: Better error handling. 404 page?
|
|
|
|
setPageHtml('<h1>Page not found</h1>');
|
|
|
|
});
|
|
|
|
}, [locale, slug]);
|
|
|
|
|
|
|
|
const alsoAvailable = (defaultLocale) && (
|
2022-11-16 07:15:18 -08:00
|
|
|
<div>
|
2022-08-08 15:43:47 -07:00
|
|
|
<FormattedMessage id='about.also_available' defaultMessage='Available in:' />
|
|
|
|
{' '}
|
2022-11-16 07:15:18 -08:00
|
|
|
<ul className='p-0 inline list-none'>
|
|
|
|
<li className="inline after:content-['_·_']">
|
2022-08-08 15:43:47 -07:00
|
|
|
<a href='#' onClick={() => setLocale(defaultLocale)}>
|
|
|
|
{/* @ts-ignore */}
|
|
|
|
{languages[defaultLocale] || defaultLocale}
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
{
|
|
|
|
pageLocales?.map(locale => (
|
2022-11-16 07:15:18 -08:00
|
|
|
<li className="inline after:content-['_·_'] last:after:content-none" key={locale}>
|
2022-08-08 15:43:47 -07:00
|
|
|
<a href='#' onClick={() => setLocale(locale)}>
|
|
|
|
{/* @ts-ignore */}
|
|
|
|
{languages[locale] || locale}
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2022-11-16 10:03:33 -08:00
|
|
|
<div className='prose dark:prose-invert mx-auto py-20'>
|
2022-11-16 07:15:18 -08:00
|
|
|
<div dangerouslySetInnerHTML={{ __html: pageHtml }} />
|
|
|
|
|
|
|
|
{alsoAvailable}
|
2022-08-08 15:43:47 -07:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AboutPage;
|