summaryrefslogtreecommitdiffstats
path: root/src/components/templates/page/page-layout.tsx
blob: ac021ba3716d50cc91422088312055fd97bc188d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import Heading from '@components/atoms/headings/heading';
import Sidebar from '@components/atoms/layout/sidebar';
import PageFooter, {
  type PageFooterProps,
} from '@components/molecules/layout/page-footer';
import PageHeader, {
  type PageHeaderProps,
} from '@components/molecules/layout/page-header';
import Breadcrumb, {
  type BreadcrumbItem,
} from '@components/molecules/nav/breadcrumb';
import CommentForm from '@components/organisms/forms/comment-form';
import CommentsList, {
  type CommentsListProps,
} from '@components/organisms/layout/comments-list';
import TableOfContents from '@components/organisms/widgets/table-of-contents';
import useIsMounted from '@utils/hooks/use-is-mounted';
import { FC, ReactNode, useRef } from 'react';
import { useIntl } from 'react-intl';
import Layout, { LayoutProps } from '../layout/layout';
import styles from './page-layout.module.scss';

export type PageLayoutProps = {
  /**
   * True if the page accepts new comments. Default: false.
   */
  allowComments?: boolean;
  /**
   * The breadcrumb items.
   */
  breadcrumb: BreadcrumbItem[];
  /**
   * The main content of the page.
   */
  children: ReactNode;
  /**
   * The page comments
   */
  comments?: CommentsListProps['comments'];
  /**
   * The footer metadata.
   */
  footerMeta?: PageFooterProps['meta'];
  /**
   * The header metadata.
   */
  headerMeta?: PageHeaderProps['meta'];
  /**
   * The page introduction.
   */
  intro?: PageHeaderProps['intro'];
  /**
   * True if it is homepage. Default: false.
   */
  isHome?: LayoutProps['isHome'];
  /**
   * The page title.
   */
  title: PageHeaderProps['title'];
  /**
   * An array of widgets to put in the last sidebar.
   */
  widgets?: ReactNode[];
  /**
   * Show the table of contents. Default: false.
   */
  withToC?: boolean;
};

/**
 * PageLayout component
 *
 * Render the pages layout.
 */
const PageLayout: FC<PageLayoutProps> = ({
  children,
  allowComments = false,
  breadcrumb,
  comments,
  footerMeta,
  headerMeta,
  intro,
  isHome = false,
  widgets,
  title,
  withToC = false,
}) => {
  const intl = useIntl();
  const commentsTitle = intl.formatMessage({
    defaultMessage: 'Comments',
    description: 'PageLayout: comments title',
    id: '+dJU3e',
  });
  const commentFormTitle = intl.formatMessage({
    defaultMessage: 'Leave a comment',
    description: 'PageLayout: comment form title',
    id: 'kzIYoQ',
  });

  const bodyRef = useRef<HTMLDivElement>(null);
  const isMounted = useIsMounted(bodyRef);

  const hasComments = Array.isArray(comments) && comments.length > 0;
  const hasCommentsSection = hasComments || allowComments;
  const articleModifier = hasCommentsSection
    ? 'article--has-comments'
    : 'article--no-comments';

  const saveComment = () => {
    return null;
  };

  return (
    <Layout
      isHome={isHome}
      className={`${styles.article} ${styles[articleModifier]}`}
    >
      <Breadcrumb
        items={breadcrumb}
        className={styles.breadcrumb}
        itemClassName={styles.breadcrumb__items}
      />
      <PageHeader
        title={title}
        intro={intro}
        meta={headerMeta}
        className={styles.header}
      />
      {withToC && (
        <Sidebar className={`${styles.sidebar} ${styles['sidebar--first']}`}>
          {isMounted && bodyRef.current && (
            <TableOfContents wrapper={bodyRef.current} />
          )}
        </Sidebar>
      )}
      {typeof children === 'string' ? (
        <div
          ref={bodyRef}
          className={styles.body}
          dangerouslySetInnerHTML={{ __html: children }}
        />
      ) : (
        <div ref={bodyRef} className={styles.body}>
          {children}
        </div>
      )}
      <PageFooter meta={footerMeta} className={styles.footer} />
      <Sidebar className={`${styles.sidebar} ${styles['sidebar--last']}`}>
        {widgets}
      </Sidebar>
      {hasCommentsSection && (
        <div className={styles.comments}>
          {hasComments && (
            <section className={styles.comments__section}>
              <Heading level={2}>{commentsTitle}</Heading>
              <CommentsList
                saveComment={saveComment}
                comments={comments}
                depth={2}
              />
            </section>
          )}
          {allowComments && (
            <section className={styles.comments__section}>
              <CommentForm saveComment={saveComment} title={commentFormTitle} />
            </section>
          )}
        </div>
      )}
    </Layout>
  );
};

export default PageLayout;