import { createMemo, createSignal, For } from "solid-js" import { useTheme } from "@tui/context/theme" import { useKeybind } from "@tui/context/keybind" import { TIPS } from "./tips" import { EmptyBorder } from "./border" type TipPart = { text: string; highlight: boolean } function parseTip(tip: string): TipPart[] { const parts: TipPart[] = [] const regex = /\{highlight\}(.*?)\{\/highlight\}/g let lastIndex = 0 let match while ((match = regex.exec(tip)) !== null) { if (match.index > lastIndex) { parts.push({ text: tip.slice(lastIndex, match.index), highlight: false }) } parts.push({ text: match[1], highlight: true }) lastIndex = regex.lastIndex } if (lastIndex < tip.length) { parts.push({ text: tip.slice(lastIndex), highlight: false }) } return parts } const [tipIndex, setTipIndex] = createSignal(Math.floor(Math.random() * TIPS.length)) export function randomizeTip() { setTipIndex(Math.floor(Math.random() * TIPS.length)) } const BOX_WIDTH = 42 const TITLE = " 🅘 Did you know? " export function DidYouKnow() { const { theme } = useTheme() const keybind = useKeybind() const tipParts = createMemo(() => parseTip(TIPS[tipIndex()])) const dashes = createMemo(() => { // ╭─ + title + ─...─ + ╮ = BOX_WIDTH // 1 + 1 + title.length + dashes + 1 = BOX_WIDTH return Math.max(0, BOX_WIDTH - 2 - TITLE.length - 1) }) return ( ╭─ {TITLE} {"─".repeat(dashes())}╮ {(part) => {part.text}} {keybind.print("tips_toggle")} hide tips ) }