Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Move FAQ into a separate, reusable component

Blue Mousec25b2d2cce3e
diff --git a/web/app/components/marketing/FAQ.tsx b/web/app/components/marketing/FAQ.tsx
index 9613c486b..11cbb86d6 100644
--- a/web/app/components/marketing/FAQ.tsx
+++ b/web/app/components/marketing/FAQ.tsx
@@ -1,12 +1,11 @@
import _map from 'lodash/map'
-import { CaretDownIcon } from '@phosphor-icons/react'
import { useMemo } from 'react'
import { Trans, useTranslation } from 'react-i18next'
import { Link } from 'react-router'
import sanitizeHtml from 'sanitize-html'
import { DOCS_URL, PLAN_LIMITS, TRIAL_DAYS, DISCORD_URL } from '~/lib/constants'
-import { cn } from '~/utils/generic'
+import { FAQ as FAQAccordion } from '~/ui/FAQ'
import routesPath from '~/utils/routes'
interface FAQProps {
@@ -78,83 +77,64 @@ const FAQ = ({ includeGAQuestions = false }: FAQProps) => {
})),
}
+ const formattedItems = useMemo(() => {
+ return items.map((item) => ({
+ question: <Trans t={t} i18nKey={`${item.i18nKey}.q`} />,
+ answer: (
+ <Trans
+ t={t}
+ i18nKey={`${item.i18nKey}.a`}
+ values={values}
+ components={{
+ dataPolicyUrl: (
+ <Link
+ to={routesPath.dataPolicy}
+ className='underline decoration-dashed hover:decoration-solid'
+ aria-label='Data policy'
+ onClick={onLinkClick}
+ />
+ ),
+ apiDocumentationUrl: (
+ <a
+ href={DOCS_URL}
+ className='underline decoration-dashed hover:decoration-solid'
+ target='_blank'
+ rel='noopener noreferrer'
+ aria-label='API documentation (opens in a new tab)'
+ onClick={onLinkClick}
+ />
+ ),
+ contactUsUrl: (
+ <Link
+ to={routesPath.contact}
+ className='underline decoration-dashed hover:decoration-solid'
+ aria-label='Contact us'
+ onClick={onLinkClick}
+ />
+ ),
+ discordUrl: (
+ <a
+ href={DISCORD_URL}
+ className='underline decoration-dashed hover:decoration-solid'
+ target='_blank'
+ rel='noopener noreferrer'
+ aria-label='Discord (opens in a new tab)'
+ onClick={onLinkClick}
+ />
+ ),
+ }}
+ />
+ ),
+ }))
+ }, [items, t, values])
+
return (
<section className='relative mx-auto max-w-5xl px-2 py-14 lg:px-8'>
<h2 className='text-center text-3xl font-extrabold text-slate-900 sm:text-4xl dark:text-white'>
{t('main.faq.title')}
</h2>
<div className='mt-8 flex flex-col'>
- {_map(
- items,
- (item: { q: string; a: string; i18nKey: string }, idx: number) => {
- const showTopBorder = idx !== 0
-
- return (
- <details
- key={item.i18nKey}
- className={cn(
- 'group w-full text-left',
- showTopBorder &&
- 'border-t border-gray-200 dark:border-white/10',
- )}
- >
- <summary className='flex w-full cursor-pointer items-center justify-between px-4 py-4'>
- <span className='text-base font-medium text-slate-900 group-hover:underline dark:text-white'>
- <Trans t={t} i18nKey={`${item.i18nKey}.q`} />
- </span>
- <CaretDownIcon className='size-4 text-slate-900 transition-transform group-open:rotate-180 dark:text-gray-200' />
- </summary>
- <div className='px-4'>
- <p className='pb-4 text-sm whitespace-pre-line text-slate-900 dark:text-gray-100'>
- <Trans
- t={t}
- i18nKey={`${item.i18nKey}.a`}
- values={values}
- components={{
- dataPolicyUrl: (
- <Link
- to={routesPath.dataPolicy}
- className='underline decoration-dashed hover:decoration-solid'
- aria-label='Data policy'
- onClick={onLinkClick}
- />
- ),
- apiDocumentationUrl: (
- <a
- href={DOCS_URL}
- className='underline decoration-dashed hover:decoration-solid'
- target='_blank'
- rel='noopener noreferrer'
- aria-label='API documentation (opens in a new tab)'
- onClick={onLinkClick}
- />
- ),
- contactUsUrl: (
- <Link
- to={routesPath.contact}
- className='underline decoration-dashed hover:decoration-solid'
- aria-label='Contact us'
- onClick={onLinkClick}
- />
- ),
- discordUrl: (
- <a
- href={DISCORD_URL}
- className='underline decoration-dashed hover:decoration-solid'
- target='_blank'
- rel='noopener noreferrer'
- aria-label='Discord (opens in a new tab)'
- onClick={onLinkClick}
- />
- ),
- }}
- />
- </p>
- </div>
- </details>
- )
- },
- )}
+ <FAQAccordion items={formattedItems} withPadding />
</div>
<script
type='application/ld+json'
diff --git a/web/app/ui/FAQ.tsx b/web/app/ui/FAQ.tsx
new file mode 100644
index 000000000..a795bb2fa
--- /dev/null
+++ b/web/app/ui/FAQ.tsx
@@ -0,0 +1,88 @@
+import { CaretDownIcon } from '@phosphor-icons/react'
+import _map from 'lodash/map'
+import type { ReactNode } from 'react'
+
+import { cn } from '~/utils/generic'
+
+export interface FAQItem {
+ question: string | ReactNode
+ answer: string | ReactNode
+}
+
+interface FAQProps {
+ items: FAQItem[]
+ className?: string
+ /**
+ * If true, will inject JSON-LD structured data.
+ * Only works optimally if question and answer are strings.
+ */
+ withStructuredData?: boolean
+ /**
+ * Whether to add horizontal padding to the items.
+ */
+ withPadding?: boolean
+}
+
+export function FAQ({
+ items,
+ className,
+ withStructuredData = false,
+ withPadding = false,
+}: FAQProps) {
+ return (
+ <div className={cn('flex flex-col', className)}>
+ {_map(items, (item, idx) => {
+ const showTopBorder = idx !== 0
+
+ return (
+ <details
+ key={idx}
+ className={cn(
+ 'group w-full text-left',
+ showTopBorder && 'border-t border-gray-200 dark:border-white/10',
+ )}
+ >
+ <summary
+ className={cn(
+ 'flex w-full cursor-pointer items-center justify-between py-4',
+ withPadding && 'px-4',
+ )}
+ >
+ <span className='text-base font-medium text-slate-900 group-hover:underline dark:text-white'>
+ {item.question}
+ </span>
+ <CaretDownIcon className='size-4 shrink-0 text-slate-900 transition-transform group-open:rotate-180 dark:text-gray-200' />
+ </summary>
+ <div className={cn('pb-4', withPadding && 'px-4')}>
+ <p className='text-sm whitespace-pre-line text-slate-900 dark:text-gray-100'>
+ {item.answer}
+ </p>
+ </div>
+ </details>
+ )
+ })}
+
+ {withStructuredData && (
+ <script
+ type='application/ld+json'
+ dangerouslySetInnerHTML={{
+ __html: JSON.stringify({
+ '@context': 'https://schema.org',
+ '@type': 'FAQPage',
+ mainEntity: items.map((item) => ({
+ '@type': 'Question',
+ name: typeof item.question === 'string' ? item.question : '',
+ acceptedAnswer: {
+ '@type': 'Answer',
+ text: typeof item.answer === 'string' ? item.answer : '',
+ },
+ })),
+ })
+ .replace(/</g, '\\u003c')
+ .replace(/\u2028|\u2029/g, ''),
+ }}
+ />
+ )}
+ </div>
+ )
+}