From 1a2847d9530ce5ba092a1eaa60a138f1a277ec69 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Wed, 23 Oct 2024 01:08:32 +0200 Subject: [PATCH] - move console logic to console tab - colorize logs when color is set --- .../src/repl/components/panel/ConsoleTab.jsx | 45 +++++++++++++++++-- website/src/repl/components/panel/Panel.jsx | 30 +------------ 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/website/src/repl/components/panel/ConsoleTab.jsx b/website/src/repl/components/panel/ConsoleTab.jsx index f0fca584..409da05e 100644 --- a/website/src/repl/components/panel/ConsoleTab.jsx +++ b/website/src/repl/components/panel/ConsoleTab.jsx @@ -1,18 +1,51 @@ +import { logger } from '@strudel/core'; +import useEvent from '@src/useEvent.mjs'; import cx from '@src/cx.mjs'; +import { nanoid } from 'nanoid'; +import { useCallback, useState } from 'react'; -export function ConsoleTab({ log }) { +export function ConsoleTab() { + const [log, setLog] = useState([]); + useLogger( + useCallback((e) => { + const { message, type, data } = e.detail; + setLog((l) => { + const lastLog = l.length ? l[l.length - 1] : undefined; + const id = nanoid(12); + // if (type === 'loaded-sample' && lastLog.type === 'load-sample' && lastLog.url === data.url) { + if (type === 'loaded-sample') { + // const loadIndex = l.length - 1; + const loadIndex = l.findIndex(({ data: { url }, type }) => type === 'load-sample' && url === data.url); + l[loadIndex] = { message, type, id, data }; + } else if (lastLog && lastLog.message === message) { + l = l.slice(0, -1).concat([{ message, type, count: (lastLog.count ?? 1) + 1, id, data }]); + } else { + l = l.concat([{ message, type, id, data }]); + } + return l.slice(-20); + }); + }, []), + ); return ( -
-