/
MeetingCard.tsx
175 lines (160 loc) · 5.53 KB
/
MeetingCard.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
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
175
import React, { useMemo, useCallback, useState } from "react";
import styled from "@emotion/styled";
import Highlighter from "react-highlight-words";
import { removeStopwords } from "stopword";
import { useAppConfigContext, useLanguageConfigContext } from "../../../app";
import FileService from "../../../networking/FileService";
import Event from "../../../models/Event";
import useFetchData from "../../../containers/FetchDataContainer/useFetchData";
import AbsoluteBox from "../../Shared/AbsoluteBox";
import PlaceholderWrapper from "../../Shared/PlaceHolder";
import { strings } from "../../../assets/LocalizedStrings";
import { TAG_CONNECTOR } from "../../../constants/StyleConstants";
import cleanText from "../../../utils/cleanText";
export interface MeetingCardProps {
/** The event for the meeting */
event: Event;
/** Tags for the event */
tags: string[];
/** A context span if the event was found through searching */
excerpt?: string;
/** The highest value gram of the context span */
gram?: string;
/** The query used to find this meeting */
query?: string;
}
const Meeting = styled.section({
marginBottom: 0,
});
// first img is static, second img is animated
const Images = styled.div({
position: "relative",
"& > img": {
position: "absolute",
inset: 0,
},
"& > img:first-of-type": {
zIndex: 1,
},
"& > img:last-of-type": {
// overlay animated img over static img
zIndex: 2,
// initially, animated img is invisible
opacity: 0,
},
"& > img:last-of-type:hover": {
// on hover, animated img is visible
opacity: 1,
},
});
const MeetingCard = ({ event, tags, excerpt, gram, query }: MeetingCardProps) => {
const { language } = useLanguageConfigContext();
const { firebaseConfig, municipality } = useAppConfigContext();
const [staticThumbNailIsLoading, setStaticThumbNailIsLoading] = useState(true);
const fetchEventThumbNails = useCallback(async () => {
const fileService = new FileService(firebaseConfig);
const { networkService } = fileService;
const thumbNailPromises: Promise<string | undefined>[] = [
Promise.resolve(undefined),
Promise.resolve(undefined),
];
if (event.static_thumbnail_ref) {
thumbNailPromises[0] = fileService
.getFileById(event.static_thumbnail_ref)
.then((file) => networkService.getDownloadUrl(file.uri))
.catch(() => Promise.resolve(undefined));
}
if (event.hover_thumbnail_ref) {
thumbNailPromises[1] = fileService
.getFileById(event.hover_thumbnail_ref)
.then((file) => networkService.getDownloadUrl(file.uri))
.catch(() => Promise.resolve(undefined));
}
const [staticThumbNailUrl, hoverThumbNailUrl] = await Promise.all(thumbNailPromises);
return Promise.resolve({
staticThumbNailUrl,
hoverThumbNailUrl,
});
}, [firebaseConfig, event.static_thumbnail_ref, event.hover_thumbnail_ref]);
const { state: eventThumbNailsState } = useFetchData(
{
isLoading: false,
error: null,
hasFetchRequest: true,
},
fetchEventThumbNails
);
const meetingDate = event.event_datetime?.toLocaleDateString(language, {
timeZone: municipality.timeZone,
month: "long",
day: "numeric",
year: "numeric",
});
const committee = event.body?.name;
const searchWords = useMemo(() => {
const cleanedQuery = cleanText(query || "");
// Phrases that should be highlighted in the excerpt
const phrases = removeStopwords(cleanedQuery.split(" "));
if (gram && gram.length > 0) {
phrases.push(gram);
}
if (phrases.length === 0) {
return [];
}
return [new RegExp(`\\b(${phrases.join("|")})`, "g")];
}, [query, gram]);
const imgAlt = `${event.body?.name} - ${meetingDate}`;
const tagString = tags.map((tag) => tag.toLowerCase()).join(TAG_CONNECTOR);
return (
<Meeting className="mzp-c-card mzp-c-card-medium mzp-has-aspect-16-9">
<div className="mzp-c-card-block-link">
<div className="mzp-c-card-media-wrapper">
<AbsoluteBox>
<PlaceholderWrapper contentIsLoading={staticThumbNailIsLoading}>
<Images>
<img
className="mzp-c-card-image"
src={eventThumbNailsState.data?.staticThumbNailUrl}
alt={imgAlt}
onLoad={() => setStaticThumbNailIsLoading(false)}
/>
<img
className="mzp-c-card-image"
src={eventThumbNailsState.data?.hoverThumbNailUrl}
alt={imgAlt}
/>
</Images>
</PlaceholderWrapper>
</AbsoluteBox>
</div>
<div className="mzp-c-card-content">
<div className="mzp-c-card-tag">{strings.committee}</div>
<h2 className="mzp-c-card-title">{meetingDate}</h2>
<p className="mzp-c-card-desc">{committee}</p>
{excerpt && (
<p
className="mzp-c-card-desc"
style={{
fontStyle: "italic",
marginTop: "1rem",
}}
>
<Highlighter
caseSensitive={false}
searchWords={searchWords}
textToHighlight={`"${excerpt}"`}
/>
</p>
)}
{tags.length > 0 && (
<>
<p className="mzp-c-card-meta">{strings.keywords}</p>
<p className="mzp-c-card-desc">{tagString}</p>
</>
)}
</div>
</div>
</Meeting>
);
};
export default MeetingCard;