-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRelatedPuzzleGroup.tsx
More file actions
159 lines (149 loc) · 4.74 KB
/
Copy pathRelatedPuzzleGroup.tsx
File metadata and controls
159 lines (149 loc) · 4.74 KB
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
import { faCaretDown } from "@fortawesome/free-solid-svg-icons/faCaretDown";
import { faCaretRight } from "@fortawesome/free-solid-svg-icons/faCaretRight";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useCallback, useEffect, useRef, useState } from "react";
import styled from "styled-components";
import type { TagType } from "../../lib/models/Tags";
import type { PuzzleGroup } from "../../lib/puzzle-sort-and-group";
import { useHuntPuzzleListCollapseGroup } from "../hooks/persisted-state";
import RelatedPuzzleList from "./RelatedPuzzleList";
import Tag from "./Tag";
export const PuzzleGroupDiv = styled.div`
&:not(:last-child) {
margin-bottom: 16px;
}
`;
const PuzzleGroupHeader = styled.div`
display: block;
&:hover {
cursor: pointer;
}
min-height: 32px;
`;
const PuzzleListWrapper = styled.div`
padding-left: 1.25em;
`;
const NoSharedTagLabel = styled.div`
display: inline-flex;
align-items: center;
line-height: 24px;
margin: 2px 4px 2px 0;
padding: 0 6px;
border-radius: 4px;
background-color: transparent;
color: #808080;
`;
const RelatedPuzzleGroup = ({
huntId,
group,
noSharedTagLabel = "(no tag)",
allTags,
bookmarked,
includeCount,
canUpdate,
suppressedTagIds,
trackPersistentExpand,
}: {
huntId: string;
group: PuzzleGroup;
// noSharedTagLabel is used to label the group only if sharedTag is undefined.
noSharedTagLabel?: string;
bookmarked: Set<string>;
allTags: TagType[];
includeCount?: boolean;
canUpdate: boolean;
suppressedTagIds: string[];
trackPersistentExpand: boolean;
}) => {
const [persistentCollapsed, setPersistentCollapsed] =
useHuntPuzzleListCollapseGroup(
huntId,
suppressedTagIds.join("-") || noSharedTagLabel,
);
const [nonPersistentCollapsed, setNonPersistentCollapsed] = useState(false);
const lastTrackPersistentExpand = useRef(trackPersistentExpand);
useEffect(() => {
if (trackPersistentExpand !== lastTrackPersistentExpand.current) {
lastTrackPersistentExpand.current = trackPersistentExpand;
setNonPersistentCollapsed(false);
}
}, [trackPersistentExpand]);
const toggleCollapse = useCallback(() => {
if (trackPersistentExpand) {
setPersistentCollapsed((prevCollapsed) => !prevCollapsed);
} else {
setNonPersistentCollapsed((prevCollapsed) => !prevCollapsed);
}
}, [setPersistentCollapsed, trackPersistentExpand]);
const collapsed = trackPersistentExpand
? persistentCollapsed
: nonPersistentCollapsed;
const { puzzles: relatedPuzzles, sharedTags } = group;
const puzzlePlural = relatedPuzzles.length === 1 ? "puzzle" : "puzzles";
const countString = `(${relatedPuzzles.length} other ${puzzlePlural})`;
const allSuppressedTagIds = [
...suppressedTagIds,
...sharedTags.map((tag) => tag._id),
];
return (
<PuzzleGroupDiv>
<PuzzleGroupHeader
data-group-name={sharedTags.map((tag) => tag.name).join("&")}
onClick={toggleCollapse}
>
<FontAwesomeIcon
fixedWidth
icon={collapsed ? faCaretRight : faCaretDown}
/>
{sharedTags.length ? (
sharedTags.map((sharedTag) => (
<Tag
tag={sharedTag}
key={sharedTag._id}
linkToSearch={false}
popoverRelated={false}
/>
))
) : (
<NoSharedTagLabel>{noSharedTagLabel}</NoSharedTagLabel>
)}
{includeCount && <span>{countString}</span>}
</PuzzleGroupHeader>
{collapsed ? null : (
<PuzzleListWrapper>
<RelatedPuzzleList
relatedPuzzles={relatedPuzzles}
bookmarked={bookmarked}
allTags={allTags}
canUpdate={canUpdate}
sharedTags={sharedTags}
suppressedTagIds={allSuppressedTagIds}
/>
{group.subgroups.map((subgroup) => {
const subgroupSuppressedTagIds = [...allSuppressedTagIds];
subgroupSuppressedTagIds.push(
...subgroup.sharedTags.map((tag) => tag._id),
);
return (
<RelatedPuzzleGroup
key={
subgroup.sharedTags.map((t) => t._id).join("-") || "ungrouped"
}
huntId={huntId}
group={subgroup}
noSharedTagLabel={noSharedTagLabel}
bookmarked={bookmarked}
allTags={allTags}
includeCount={includeCount}
canUpdate={canUpdate}
suppressedTagIds={subgroupSuppressedTagIds}
trackPersistentExpand={trackPersistentExpand}
/>
);
})}
</PuzzleListWrapper>
)}
</PuzzleGroupDiv>
);
};
export default RelatedPuzzleGroup;