Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add times and mentions to chat #44

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/components/Chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import useStats from "../hooks/useStats";
import useStorage from "../hooks/useStorage";
import { UserContext } from "../context";

const useStyles = makeStyles({
const useStyles = makeStyles((theme) => ({
chatPanel: {
display: "flex",
flexDirection: "column",
Expand Down Expand Up @@ -53,7 +53,23 @@ const useStyles = makeStyles({
visibility: "visible",
},
},
});
messageTime: {
color: "gray",
fontSize: "xx-small",
marginRight: "0.4em",
verticalAlign: "middle",
},
mentioned: {
backgroundColor: theme.mentioned.background,
borderRight: `solid ${theme.mentioned.border}`,
},
}));

const makeUserRE = (username) => {
username = username.replace(/[\\^$.*+?()[\]{}|]/g, "\\$&");
username = username.replace(/^anonymous /i, "($&)?");
return new RegExp(`@(all|${username})\\b`, "i");
};

/** A chat sidebar element, opens lobby chat when the `gameId` prop is not set. */
function Chat({
Expand Down Expand Up @@ -90,6 +106,11 @@ function Chat({
[databasePath, messageLimit]
);
const messages = useFirebaseQuery(messagesQuery);
const userRE = useMemo(() => makeUserRE(user.name), [user.name]);

const addMentioned = (cls, message) => {
return userRE.test(message) ? `${cls} ${classes.mentioned}` : cls;
};

function handleSubmit(event) {
event.preventDefault();
Expand Down Expand Up @@ -160,6 +181,17 @@ function Chat({
);
};

const formatTime = (time) => {
const opts = { timeStyle: "short", hour12: false };
return (
showMessageTimes && (
<span className={classes.messageTime}>
{new Date(time).toLocaleTimeString(undefined, opts)}
</span>
)
);
};

return (
<section
className={classes.chatPanel}
Expand All @@ -184,11 +216,12 @@ function Chat({
<div
key={key}
style={{ display: "flex", flexDirection: "row" }}
className={classes.message}
className={addMentioned(classes.message, item.message)}
>
{timeTooltip(
item.time,
<Typography variant="body2" gutterBottom>
{formatTime(item.time)}
<User
id={item.user}
component={InternalLink}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/LobbyPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function LobbyPage() {
<Box clone order={{ xs: 3, md: 1 }} className={classes.chatColumn}>
<Grid item xs={12} sm={12} md={3}>
<Paper className={classes.chatColumnPaper}>
<Chat title="Lobby Chat" messageLimit={30} showMessageTimes />
<Chat title="Lobby Chat" messageLimit={50} showMessageTimes />
</Paper>
</Grid>
</Box>
Expand Down
9 changes: 8 additions & 1 deletion src/themes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const darkTheme = createTheme({
main: "#ff4284",
dark: "#c51162",
},

action: {
hover: "#363636",
},
Expand All @@ -34,6 +33,10 @@ export const darkTheme = createTheme({
caretColor: "#fff",
background: "#262626",
},
mentioned: {
background: "#383838",
border: "#ffd700",
},
pie: {
noGames: "#rgba(0, 0, 0, 0.12)",
},
Expand Down Expand Up @@ -69,6 +72,10 @@ export const lightTheme = createTheme({
caretColor: "black",
background: "#fff",
},
mentioned: {
background: "#fffedc",
border: "#ffd700",
},
pie: {
noGames: "rgba(0, 0, 0, 0.12)",
},
Expand Down