From 2f0d52e71cc49bbdd3d41070a8a8f8fc5b0fe09d Mon Sep 17 00:00:00 2001 From: martylukyy <35452459+martylukyy@users.noreply.github.com> Date: Fri, 1 Sep 2023 21:56:12 +0200 Subject: [PATCH] enhancement(web): IRC logs view (#1066) * enhancement(web): IRC logs view * revert sorting changes and implement auto scroll * replace setTimeout with useEffect and dep add option menu for scroll on new log toggle. prevent duplicate log entries when toggling settings.scrollOnNewLog through clearing logs once * linting --- web/src/components/Checkbox.tsx | 2 +- web/src/screens/settings/Irc.tsx | 114 +++++++++++++++++++++++-------- 2 files changed, 88 insertions(+), 28 deletions(-) diff --git a/web/src/components/Checkbox.tsx b/web/src/components/Checkbox.tsx index e814b5f..e135a89 100644 --- a/web/src/components/Checkbox.tsx +++ b/web/src/components/Checkbox.tsx @@ -15,7 +15,7 @@ interface CheckboxProps { export const Checkbox = ({ label, description, value, setValue }: CheckboxProps) => (
- + {label} {description === undefined ? null : ( diff --git a/web/src/screens/settings/Irc.tsx b/web/src/screens/settings/Irc.tsx index d26a303..becdbff 100644 --- a/web/src/screens/settings/Irc.tsx +++ b/web/src/screens/settings/Irc.tsx @@ -11,6 +11,7 @@ import { toast } from "react-hot-toast"; import { ArrowsPointingInIcon, ArrowsPointingOutIcon, + Cog6ToothIcon, EllipsisHorizontalIcon, ExclamationCircleIcon, PencilSquareIcon, @@ -25,6 +26,7 @@ import { EmptySimple } from "@components/emptystates"; import { DeleteModal } from "@components/modals"; import Toast from "@components/notifications/Toast"; import { SettingsContext } from "@utils/Context"; +import { Checkbox } from "@components/Checkbox"; // import { useForm } from "react-hook-form"; export const ircKeys = { @@ -169,6 +171,7 @@ const IrcSettings = () => { ? Collapse : Expand } +
@@ -417,7 +420,7 @@ const ChannelItem = ({ network, channel }: ChannelItemProps) => {
-
@@ -609,9 +612,26 @@ interface EventsProps { } export const Events = ({ network, channel }: EventsProps) => { + + const [logs, setLogs] = useState([]); const [settings] = SettingsContext.use(); - const messagesEndRef = useRef(null); + useEffect(() => { + // Following RFC4648 + const key = window.btoa(`${network.id}${channel.toLowerCase()}`) + .replaceAll("+", "-") + .replaceAll("/", "_") + .replaceAll("=", ""); + const es = APIClient.irc.events(key); + + es.onmessage = (event) => { + const newData = JSON.parse(event.data) as IrcEvent; + setLogs((prevState) => [...prevState, newData]); + }; + + return () => es.close(); + }, [settings]); + const [isFullscreen, toggleFullscreen] = useToggle(false); useEffect(() => { @@ -628,11 +648,22 @@ export const Events = ({ network, channel }: EventsProps) => { }; }, [isFullscreen, toggleFullscreen]); - const [logs, setLogs] = useState([]); + const messagesEndRef = useRef(null); - // const scrollToBottom = () => { - // messagesEndRef.current?.scrollIntoView({ behavior: "smooth", block: "end", inline: "end" }); - // }; + useEffect(() => { + const scrollToBottom = () => { + if (messagesEndRef.current) { + messagesEndRef.current.scrollTop = messagesEndRef.current.scrollHeight; + } + }; + if (settings.scrollOnNewLog) + scrollToBottom(); + }, [logs]); + + // Add a useEffect to clear logs div when settings.scrollOnNewLog changes to prevent duplicate entries. + useEffect(() => { + setLogs([]); + }, [settings.scrollOnNewLog]); // const { handleSubmit, register , resetField } = useForm({ // defaultValues: { msg: "" }, @@ -656,25 +687,6 @@ export const Events = ({ network, channel }: EventsProps) => { // cmdMutation.mutate(payload); // }; - useEffect(() => { - // Following RFC4648 - const key = window.btoa(`${network.id}${channel.toLowerCase()}`) - .replaceAll("+", "-") - .replaceAll("/", "_") - .replaceAll("=", ""); - const es = APIClient.irc.events(key); - - es.onmessage = (event) => { - const newData = JSON.parse(event.data) as IrcEvent; - setLogs((prevState) => [...prevState, newData]); - - // if (settings.scrollOnNewLog) - // scrollToBottom(); - }; - - return () => es.close(); - }, [settings]); - return (
{ "overflow-y-auto rounded-lg min-w-full bg-gray-100 dark:bg-gray-900 overflow-auto", isFullscreen ? "max-w-full h-full p-2 border-gray-300 dark:border-gray-700" : "px-2 py-1 aspect-[2/1]" )} + ref={messagesEndRef} > {logs.map((entry, idx) => (
{ settings.hideWrappedText ? "truncate hover:text-ellipsis hover:whitespace-normal" : "" )} > - {entry.nick}: {entry.msg} + [{simplifyDate(entry.time)}] {entry.nick}: {entry.msg}
))} -
{/*
*/} @@ -733,3 +745,51 @@ export const Events = ({ network, channel }: EventsProps) => { }; export default IrcSettings; + +const IRCLogsDropdown = () => { + const [settings, setSettings] = SettingsContext.use(); + + const onSetValue = ( + key: "scrollOnNewLog", + newValue: boolean + ) => setSettings((prevState) => ({ + ...prevState, + [key]: newValue + })); + + return ( + + + + + + +
+ + {() => ( + onSetValue("scrollOnNewLog", newValue)} + /> + )} + + +
+
+
+
+ ); +};