Skip to content

Commit ef9e68c

Browse files
authored
Add /me command to chat (#172)
1 parent feb672d commit ef9e68c

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

src/components/Chat.js

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ const useStyles = makeStyles((theme) => ({
8080
margin: "0 -4px",
8181
padding: "0 4px 0 2px",
8282
},
83+
action: {
84+
fontStyle: "italic",
85+
},
8386
}));
8487

8588
const makeMentionRE = (username) => {
@@ -88,6 +91,8 @@ const makeMentionRE = (username) => {
8891
return new RegExp(`@(all|${username})(\\W|$)`, "iu");
8992
};
9093

94+
const emojiRE = /:([a-z0-9_+-]+):/g;
95+
9196
/** A chat sidebar element, opens lobby chat when the `gameId` prop is not set. */
9297
function Chat({
9398
title,
@@ -129,14 +134,17 @@ function Chat({
129134

130135
function handleSubmit(event) {
131136
event.preventDefault();
132-
const trimmed = input.trim();
133-
if (trimmed) {
137+
let text = input.trim();
138+
if (text) {
139+
if (text.startsWith("/slap ")) {
140+
text = `/me slaps ${text.slice(6)} around a bit with a large trout`;
141+
}
134142
firebase
135143
.database()
136144
.ref(databasePath)
137145
.push({
138146
user: user.id,
139-
message: censorText(trimmed),
147+
message: censorText(text),
140148
time: firebase.database.ServerValue.TIMESTAMP,
141149
});
142150
}
@@ -210,7 +218,7 @@ function Chat({
210218
};
211219

212220
const processText = (text) => {
213-
return text.replace(/:([a-z0-9_+-]+):/g, (m, n) => emoji[n] || m);
221+
return text.replace(emojiRE, (m, n) => emoji[n] || m);
214222
};
215223

216224
return (
@@ -243,13 +251,29 @@ function Chat({
243251
item.time,
244252
<Typography variant="body2">
245253
{formatTime(item.time)}
246-
<User
247-
id={item.user}
248-
component={InternalLink}
249-
to={`/profile/${item.user}`}
250-
underline="none"
251-
/>
252-
: {item.message}
254+
{item.message.startsWith("/me ") ? (
255+
<span className={classes.action}>
256+
*{" "}
257+
<User
258+
id={item.user}
259+
style={{ color: "inherit", fontWeight: "inherit" }}
260+
showIcon={false}
261+
component={InternalLink}
262+
to={`/profile/${item.user}`}
263+
/>
264+
{item.message.slice(3)}
265+
</span>
266+
) : (
267+
<>
268+
<User
269+
id={item.user}
270+
component={InternalLink}
271+
to={`/profile/${item.user}`}
272+
underline="none"
273+
/>
274+
: {item.message}
275+
</>
276+
)}
253277
</Typography>
254278
)}
255279
{user.admin && (

src/components/User.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { makeStyles, useTheme } from "@material-ui/core/styles";
22
import Security from "@material-ui/icons/Security";
3-
import WhatshotIcon from "@material-ui/icons/Whatshot";
43

54
import useFirebaseRef from "../hooks/useFirebaseRef";
65
import useStats from "../hooks/useStats";
@@ -24,8 +23,8 @@ function User({
2423
style,
2524
component,
2625
render,
27-
forcePatron,
2826
showRating,
27+
showIcon = true,
2928
...other
3029
}) {
3130
const theme = useTheme();
@@ -53,7 +52,7 @@ function User({
5352
{loadingStats ? "⋯" : Math.round(stats[showRating].rating)}
5453
</span>
5554
)}
56-
{user.admin ? (
55+
{showIcon && user.admin && (
5756
<Security
5857
fontSize="inherit"
5958
style={{
@@ -64,19 +63,6 @@ function User({
6463
color: "inherit",
6564
}}
6665
/>
67-
) : (
68-
(user.patron || forcePatron) && (
69-
<WhatshotIcon
70-
fontSize="inherit"
71-
style={{
72-
display: "inline",
73-
position: "relative",
74-
left: "-0.1em",
75-
top: "0.15em",
76-
color: "inherit",
77-
}}
78-
/>
79-
)
8066
)}
8167
<span>{user.name}</span>
8268
</Component>

0 commit comments

Comments
 (0)