Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

refactor(frontend): Phase 5 §8.3/§8.2 — useLockBodyScroll hook + markdown link hardening

Juan Denis9bd6d18f2ce2
diff --git a/frontend/src/components/ai/ChatDrawer.jsx b/frontend/src/components/ai/ChatDrawer.jsx
index 655bb253..1686e8a3 100644
--- a/frontend/src/components/ai/ChatDrawer.jsx
+++ b/frontend/src/components/ai/ChatDrawer.jsx
@@ -1,6 +1,7 @@
-import { useCallback, useEffect, useRef, useState } from 'react';
+import { useCallback, useRef, useState } from 'react';
import { useServerkitAI } from '../../contexts/AIContext';
import useMediaQuery from '../../hooks/useMediaQuery';
+import { useLockBodyScroll } from '../../hooks/useLockBodyScroll';
import useFocusTrap from '../../hooks/ai/useFocusTrap';
import DrawerHeader from './DrawerHeader';
import ContextChip from './ContextChip';
@@ -27,12 +28,7 @@ const ChatDrawer = () => {
useFocusTrap(panelRef, { active: true, onEscape: close });
// Lock body scroll on mobile (full-screen sheet).
- useEffect(() => {
- if (!isMobile) return undefined;
- const prev = document.body.style.overflow;
- document.body.style.overflow = 'hidden';
- return () => { document.body.style.overflow = prev; };
- }, [isMobile]);
+ useLockBodyScroll(isMobile);
const startResize = useCallback((e) => {
if (isMobile) return;
diff --git a/frontend/src/components/file-manager/PreviewDrawer.jsx b/frontend/src/components/file-manager/PreviewDrawer.jsx
index 8d467562..b80d45e1 100644
--- a/frontend/src/components/file-manager/PreviewDrawer.jsx
+++ b/frontend/src/components/file-manager/PreviewDrawer.jsx
@@ -1,4 +1,4 @@
-import { useEffect } from 'react';
+import { useLockBodyScroll } from '@/hooks/useLockBodyScroll';
import { X, Edit3, Download, EyeOff, Lock, Trash2, Copy } from 'lucide-react';
import { Button } from '@/components/ui/button';
import FileIcon from './FileIcon';
@@ -31,12 +31,7 @@ export default function PreviewDrawer({
}) {
// Lock body scroll only for the overlay drawer; the inline pane scrolls
// within its own column and must not freeze the page.
- useEffect(() => {
- if (!file || inline) return;
- const prev = document.body.style.overflow;
- document.body.style.overflow = 'hidden';
- return () => { document.body.style.overflow = prev; };
- }, [file, inline]);
+ useLockBodyScroll(Boolean(file) && !inline);
if (!file) return null;
diff --git a/frontend/src/hooks/useLockBodyScroll.js b/frontend/src/hooks/useLockBodyScroll.js
new file mode 100644
index 00000000..f180086f
--- /dev/null
+++ b/frontend/src/hooks/useLockBodyScroll.js
@@ -0,0 +1,34 @@
+import { useEffect } from 'react';
+
+// Lock body scroll while `locked` is true. Ref-counted so nested locks (e.g. a
+// modal opening over an already-locked drawer) compose correctly — the body
+// overflow is only restored once the LAST active lock is released.
+//
+// useLockBodyScroll(isOpen);
+//
+// Replaces the hand-rolled `document.body.style.overflow` toggles that were
+// duplicated across DashboardLayout, PreviewDrawer, and ChatDrawer.
+
+let lockCount = 0;
+let originalOverflow = '';
+
+export function useLockBodyScroll(locked = true) {
+ useEffect(() => {
+ if (!locked || typeof document === 'undefined') return undefined;
+
+ if (lockCount === 0) {
+ originalOverflow = document.body.style.overflow;
+ document.body.style.overflow = 'hidden';
+ }
+ lockCount += 1;
+
+ return () => {
+ lockCount -= 1;
+ if (lockCount === 0) {
+ document.body.style.overflow = originalOverflow;
+ }
+ };
+ }, [locked]);
+}
+
+export default useLockBodyScroll;
diff --git a/frontend/src/layouts/DashboardLayout.jsx b/frontend/src/layouts/DashboardLayout.jsx
index 59dce960..a89dce73 100644
--- a/frontend/src/layouts/DashboardLayout.jsx
+++ b/frontend/src/layouts/DashboardLayout.jsx
@@ -10,6 +10,7 @@ import AIAssistant from '../components/ai/AIAssistant';
import PluginLoader from '../plugins/PluginLoader';
import { refreshContributions, useContributions } from '../plugins/contributions';
import useMediaQuery from '../hooks/useMediaQuery';
+import { useLockBodyScroll } from '../hooks/useLockBodyScroll';
import api from '../services/api';
import SystemNotices from '../components/SystemNotices';
@@ -64,12 +65,7 @@ const DashboardLayout = () => {
}, [isMobile]);
// Lock body scroll behind the drawer while it's open on mobile.
- useEffect(() => {
- if (!(isMobile && navOpen)) return undefined;
- const previous = document.body.style.overflow;
- document.body.style.overflow = 'hidden';
- return () => { document.body.style.overflow = previous; };
- }, [isMobile, navOpen]);
+ useLockBodyScroll(isMobile && navOpen);
// Load plugin contributions once we're authenticated. Subscribers
// (Sidebar, CommandPalette, ExtensionRoutes, PageTitleUpdater) all
diff --git a/frontend/src/lib/ai/markdown.js b/frontend/src/lib/ai/markdown.js
index fbd17fff..ef39b079 100644
--- a/frontend/src/lib/ai/markdown.js
+++ b/frontend/src/lib/ai/markdown.js
@@ -46,7 +46,13 @@ export function renderMarkdownToHtml(src) {
text = text.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>');
text = text.replace(
/\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g,
- (_m, label, url) => `<a href="${url}" target="_blank" rel="noopener noreferrer">${label}</a>`,
+ (_m, label, url) => {
+ // The surrounding text is already HTML-escaped, but `"` is not, and a
+ // crafted URL could otherwise break out of the href attribute. Encode
+ // quotes so the value stays contained.
+ const safeUrl = url.replace(/"/g, '%22').replace(/'/g, '%27');
+ return `<a href="${safeUrl}" target="_blank" rel="noopener noreferrer">${label}</a>`;
+ },
);
// 5) Paragraphs + line breaks.