/
index.tsx
106 lines (96 loc) · 3.27 KB
/
index.tsx
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
import { Button } from "../../atoms/button";
import { LanguageMenu } from "./language-menu";
import { useIsServer } from "../../../hooks";
import { useUserData } from "../../../user-context";
import { Doc, DocMetadata } from "../../../../../libs/types/document";
import "./index.scss";
import BookmarkMenu from "./bookmark-menu";
import { Overlay, useUIStatus } from "../../../ui-context";
import { useEffect, useState } from "react";
import { KeyedMutator } from "swr";
import { SWRInfiniteResponse } from "swr/infinite";
import { Item } from "../../../plus/collections/api";
// "swr/infinite" doesn't export InfiniteKeyedMutator directly
type InfiniteKeyedMutator<T> = SWRInfiniteResponse<
T extends (infer I)[] ? I : T
>["mutate"];
export const ArticleActions = ({
doc,
showTranslations = true,
item,
scopedMutator,
}: {
doc?: Doc | DocMetadata;
showTranslations?: boolean;
item?: Item;
scopedMutator?: KeyedMutator<Item[][]> | InfiniteKeyedMutator<Item[][]>;
}) => {
const [showArticleActionsMenu, setShowArticleActionsMenu] = useState(false);
const userData = useUserData();
const isServer = useIsServer();
const { toggleMobileOverlay } = useUIStatus();
const isAuthenticated = userData && userData.isAuthenticated;
const translations = doc?.other_translations || [];
const native = doc?.native;
function toggleArticleActionsMenu() {
setShowArticleActionsMenu(!showArticleActionsMenu);
}
useEffect(
() => toggleMobileOverlay(Overlay.ArticleActions, showArticleActionsMenu),
[showArticleActionsMenu, toggleMobileOverlay]
);
// @TODO we will need the following when including the language drop-down
// const translations = doc.other_translations || [];
return (
(((translations && !!translations.length) ||
(!isServer && isAuthenticated)) && (
<>
<div
className={`article-actions${
showArticleActionsMenu ? " show-actions" : ""
}`}
>
<Button
type="action"
aria-label="Article actions"
extraClasses="article-actions-toggle"
onClickHandler={toggleArticleActionsMenu}
icon={showArticleActionsMenu ? "cancel" : "ellipses"}
>
<span className="article-actions-dialog-heading">
Article Actions
</span>
</Button>
<ul className="article-actions-entries">
<>
{!isServer && isAuthenticated && (
<li className="article-actions-entry">
<BookmarkMenu
doc={doc}
item={item}
scopedMutator={scopedMutator}
/>
</li>
)}
{showTranslations &&
translations &&
!!translations.length &&
native && (
<li className="article-actions-entry">
<LanguageMenu
onClose={() =>
showArticleActionsMenu && toggleArticleActionsMenu()
}
translations={translations}
native={native}
/>
</li>
)}
</>
</ul>
</div>
</>
)) ||
null
);
};