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

72 lines
2.0 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
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-07-25 09:56:45 -04:00
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
2025-07-25 09:56:45 -04:00
const data = await response.json();
if (data?.msg) setRandomMsg(data.msg.replace(/<br\s*\/?>/gi, "\n"));
2025-07-25 09:56:45 -04:00
} catch (err) {
console.error("Failed to fetch random message:", err);
}
};
2025-06-22 09:50:36 -04:00
useEffect(() => void getRandomMsg(), []);
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>
{randomMsg && (
<button
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}
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
width="16"
height="16"
aria-hidden="true"
focusable="false"
className="inline-block"
>
<path
d="M21 12a9 9 0 1 1-2.64-6.36"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
<polyline
points="21 3 21 9 15 9"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</button>
)}
2025-07-25 09:56:45 -04:00
</div>
);
}