Files
codey.lol/src/components/RandomMsg.jsx

52 lines
1.3 KiB
React
Raw Normal View History

2025-07-25 09:56:45 -04:00
import { useState, useEffect } from "react";
2025-06-22 09:50:36 -04:00
import { API_URL } from "../config";
2025-07-25 09:56:45 -04:00
import ReplayIcon from "@mui/icons-material/Replay";
2025-07-25 09:56:45 -04:00
export default function RandomMsg() {
const [randomMsg, setRandomMsg] = useState("");
2025-06-22 09:50:36 -04:00
2025-07-25 09:56:45 -04:00
const getRandomMsg = async () => {
try {
const response = await fetch(`${API_URL}/randmsg`, {
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8",
},
});
2025-06-22 09:50:36 -04:00
2025-07-25 09:56:45 -04:00
const data = await response.json();
if (data?.msg) {
const formattedMsg = data.msg.replace(/<br\s*\/?>/gi, "\n");
setRandomMsg(formattedMsg);
}
} catch (err) {
console.error("Failed to fetch random message:", err);
}
};
2025-06-22 09:50:36 -04:00
2025-07-25 09:56:45 -04:00
useEffect(() => {
2025-06-22 09:50:36 -04:00
getRandomMsg();
2025-07-25 09:56:45 -04:00
}, []);
2025-06-22 09:50:36 -04:00
2025-07-25 09:56:45 -04:00
return (
<div className="random-msg-container">
<div className="random-msg">
{randomMsg && (
<small>
<i>{randomMsg}</i>
</small>
)}
</div>
<button
id="new-msg"
aria-label="New footer message"
type="button"
className="flex items-center justify-center px-2 py-1 rounded-md hover:bg-neutral-200 dark:hover:bg-neutral-800 transition-opacity new-msg-button"
onClick={getRandomMsg}
>
<ReplayIcon fontSize="small" />
</button>
</div>
);
}