Refactor raised hand indicator and add tests.

This commit is contained in:
Half-Shot
2024-10-28 12:32:33 +00:00
parent 7f268a3e10
commit a23d256fb6
6 changed files with 162 additions and 70 deletions

View File

@@ -0,0 +1,39 @@
.raisedHandWidget {
display: flex;
background-color: var(--cpd-color-bg-subtle-primary);
border-radius: var(--cpd-radius-pill-effect);
color: var(--cpd-color-icon-secondary);
border: 1px solid var(--cpd-color-yellow-1200);
}
.raisedHandWidget > p {
padding: var(--cpd-space-2x);
margin-top: auto;
margin-bottom: auto;
width: 4em;
}
.raisedHand {
margin: var(--cpd-space-2x);
padding: var(--cpd-space-2x);
padding-block: var(--cpd-space-2x);
color: var(--cpd-color-icon-secondary);
background-color: var(--cpd-color-icon-secondary);
display: flex;
align-items: center;
border-radius: var(--cpd-radius-pill-effect);
user-select: none;
overflow: hidden;
box-shadow: var(--small-drop-shadow);
box-sizing: border-box;
max-inline-size: 100%;
max-width: fit-content;
}
.raisedHand > span {
width: var(--cpd-space-8x);
height: var(--cpd-space-8x);
display: inline-block;
text-align: center;
font-size: 22px;
}

View File

@@ -0,0 +1,36 @@
/*
Copyright 2024 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only
Please see LICENSE in the repository root for full details.
*/
import { describe, expect, test } from "vitest";
import { render, configure } from "@testing-library/react";
import { RaisedHandIndicator } from "./RaisedHandIndicator";
configure({
defaultHidden: true,
});
describe("RaisedHandIndicator", () => {
test("renders nothing when no hand has been raised", async () => {
const { container } = render(<RaisedHandIndicator />);
expect(container.firstChild).toBeNull();
});
test("renders an indicator when a hand has been raised", async () => {
const dateTime = new Date();
const { container } = render(
<RaisedHandIndicator raisedHandTime={dateTime} />,
);
expect(container.firstChild).toMatchSnapshot();
});
test("renders an indicator when a hand has been raised with the expected time", async () => {
const dateTime = new Date(new Date().getTime() - 60000);
const { container } = render(
<RaisedHandIndicator raisedHandTime={dateTime} />,
);
expect(container.firstChild).toMatchSnapshot();
});
});

View File

@@ -0,0 +1,44 @@
import { useEffect, useState } from "react";
import styles from "./RaisedHandIndicator.module.css";
export function RaisedHandIndicator({
raisedHandTime,
}: {
raisedHandTime?: Date;
}) {
const [raisedHandDuration, setRaisedHandDuration] = useState("");
useEffect(() => {
if (!raisedHandTime) {
return;
}
const calculateTime = () => {
const totalSeconds = Math.ceil(
(new Date().getTime() - raisedHandTime.getTime()) / 1000,
);
const seconds = totalSeconds % 60;
const minutes = Math.floor(totalSeconds / 60);
setRaisedHandDuration(
`${minutes < 10 ? "0" : ""}${minutes}:${seconds < 10 ? "0" : ""}${seconds}`,
);
};
const to = setInterval(calculateTime, 1000);
calculateTime();
return (): void => clearInterval(to);
}, [setRaisedHandDuration, raisedHandTime]);
if (raisedHandTime) {
return (
<div className={styles.raisedHandWidget}>
<div className={styles.raisedHand}>
<span role="img" aria-label="raised hand">
</span>
</div>
<p>{raisedHandDuration}</p>
</div>
);
}
return null;
}

View File

@@ -0,0 +1,41 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`RaisedHandIndicator > renders an indicator when a hand has been raised 1`] = `
<div
class="raisedHandWidget"
>
<div
class="raisedHand"
>
<span
aria-label="raised hand"
role="img"
>
</span>
</div>
<p>
00:01
</p>
</div>
`;
exports[`RaisedHandIndicator > renders an indicator when a hand has been raised with the expected time 1`] = `
<div
class="raisedHandWidget"
>
<div
class="raisedHand"
>
<span
aria-label="raised hand"
role="img"
>
</span>
</div>
<p>
01:01
</p>
</div>
`;