Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor([consolidate-client-url-normalize]): extract shared client URL normalize/detect helper

[._.]/ Adam Eivyb2111bb735ba
diff --git a/client/src/components/QuickBrainCapture.jsx b/client/src/components/QuickBrainCapture.jsx
index 71ed09f47f..9ac1c1fb64 100644
--- a/client/src/components/QuickBrainCapture.jsx
+++ b/client/src/components/QuickBrainCapture.jsx
@@ -3,20 +3,14 @@ import { Link } from 'react-router-dom';
import { Send } from 'lucide-react';
import toast from './ui/Toast';
import * as api from '../services/api';
-
-const URL_PATTERN = /^(https?:\/\/|git@)/i;
-const DOMAIN_PATTERN = /^\S+\.\S+$/;
+import { isUrl as isUrlShared, normalizeUrl } from '../utils/urlNormalize';
export default function QuickBrainCapture() {
const [input, setInput] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const submittingRef = useRef(false);
- const isUrl = useMemo(() => {
- const trimmed = input.trim();
- if (!trimmed) return false;
- return URL_PATTERN.test(trimmed) || DOMAIN_PATTERN.test(trimmed);
- }, [input]);
+ const isUrl = useMemo(() => isUrlShared(input), [input]);
const handleSubmit = async (e) => {
e?.preventDefault();
@@ -30,10 +24,7 @@ export default function QuickBrainCapture() {
setInput('');
if (isUrl) {
- let url = text;
- if (!url.startsWith('http://') && !url.startsWith('https://') && !url.startsWith('git@')) {
- url = 'https://' + url;
- }
+ const url = normalizeUrl(text, { allowGit: true });
const result = await api.createBrainLink({ url }).catch(err => {
if (err.message?.includes('already exists')) {
toast.error('This URL is already saved');
diff --git a/client/src/components/brain/tabs/FeedsTab.jsx b/client/src/components/brain/tabs/FeedsTab.jsx
index cffb178011..bf4104e304 100644
--- a/client/src/components/brain/tabs/FeedsTab.jsx
+++ b/client/src/components/brain/tabs/FeedsTab.jsx
@@ -12,6 +12,7 @@ import {
import BrailleSpinner from '../../BrailleSpinner';
import toast from '../../ui/Toast';
import { timeAgo } from '../../../utils/formatters';
+import { normalizeUrl } from '../../../utils/urlNormalize';
export default function FeedsTab({ onRefresh }) {
const [inputUrl, setInputUrl] = useState('');
@@ -46,10 +47,8 @@ export default function FeedsTab({ onRefresh }) {
e.preventDefault();
if (!inputUrl.trim() || adding) return;
- let url = inputUrl.trim();
- if (!url.startsWith('http://') && !url.startsWith('https://')) {
- url = 'https://' + url;
- }
+ // FeedsTab intentionally does NOT treat git@ as already-normalized.
+ const url = normalizeUrl(inputUrl, { allowGit: false });
setAdding(true);
const result = await api.addFeed(url).catch(() => null);
diff --git a/client/src/components/brain/tabs/LinksTab.jsx b/client/src/components/brain/tabs/LinksTab.jsx
index ed83453ebb..9916da572e 100644
--- a/client/src/components/brain/tabs/LinksTab.jsx
+++ b/client/src/components/brain/tabs/LinksTab.jsx
@@ -30,19 +30,11 @@ import { useAutoRefetch } from '../../../hooks/useAutoRefetch';
import BucketBoard from '../links/BucketBoard';
import { LINK_DND_TYPE } from '../links/bucketColors';
import { reorderLinksInBucket } from '../links/bucketReorder';
+import { normalizeUrl as normalizeUrlShared } from '../../../utils/urlNormalize';
/** Normalize a user-entered URL the way the quick-add form does. */
function normalizeUrl(raw) {
- let url = raw.trim();
- if (!url) return null;
- if (!url.startsWith('http://') && !url.startsWith('https://') && !url.startsWith('git@')) {
- if (url.includes('github.com') || url.includes('.')) {
- url = 'https://' + url;
- } else {
- return null;
- }
- }
- return url;
+ return normalizeUrlShared(raw, { allowGit: true, requireDot: true });
}
const LINK_TYPE_COLORS = {
diff --git a/client/src/utils/urlNormalize.js b/client/src/utils/urlNormalize.js
new file mode 100644
index 0000000000..9f96961c69
--- /dev/null
+++ b/client/src/utils/urlNormalize.js
@@ -0,0 +1,59 @@
+// Shared client-side URL normalization + detection helpers.
+//
+// Three brain-capture surfaces (LinksTab, QuickBrainCapture, FeedsTab) each
+// prepended `https://` to bare URLs with near-identical-but-subtly-different
+// logic. This module consolidates that logic while preserving each call site's
+// behavior via options:
+// - `allowGit` — treat `git@…` strings as already-normalized (LinksTab,
+// QuickBrainCapture do this; FeedsTab does NOT).
+// - `requireDot`— only prepend `https://` when the value looks domain-like
+// (contains a `.` or `github.com`), returning null otherwise
+// (LinksTab's quick-add guard). FeedsTab/QuickBrainCapture
+// prepend unconditionally for non-scheme input.
+
+const URL_SCHEME_PATTERN = /^(https?:\/\/|git@)/i;
+const DOMAIN_PATTERN = /^\S+\.\S+$/;
+
+/**
+ * Detect whether a raw string should be treated as a URL/link rather than
+ * free text. Mirrors QuickBrainCapture's detection: an explicit scheme
+ * (http/https/git@) OR a domain-like single token (`foo.bar`).
+ *
+ * @param {string} raw
+ * @returns {boolean}
+ */
+export function isUrl(raw) {
+ const trimmed = (raw ?? '').trim();
+ if (!trimmed) return false;
+ return URL_SCHEME_PATTERN.test(trimmed) || DOMAIN_PATTERN.test(trimmed);
+}
+
+/**
+ * Normalize a user-entered URL by prepending `https://` when no scheme is
+ * present. Behavior is tunable to match each historical call site.
+ *
+ * @param {string} raw
+ * @param {object} [options]
+ * @param {boolean} [options.allowGit=true] Treat `git@…` as already-normalized.
+ * @param {boolean} [options.requireDot=false] Only prepend (else return null)
+ * when the value contains a `.` or `github.com`.
+ * @returns {string|null} The normalized URL, or null when input is empty (or,
+ * with requireDot, when it doesn't look domain-like).
+ */
+export function normalizeUrl(raw, { allowGit = true, requireDot = false } = {}) {
+ let url = (raw ?? '').trim();
+ if (!url) return null;
+
+ const hasScheme =
+ url.startsWith('http://') ||
+ url.startsWith('https://') ||
+ (allowGit && url.startsWith('git@'));
+
+ if (!hasScheme) {
+ if (requireDot && !(url.includes('github.com') || url.includes('.'))) {
+ return null;
+ }
+ url = 'https://' + url;
+ }
+ return url;
+}
diff --git a/client/src/utils/urlNormalize.test.js b/client/src/utils/urlNormalize.test.js
new file mode 100644
index 0000000000..d601f6082a
--- /dev/null
+++ b/client/src/utils/urlNormalize.test.js
@@ -0,0 +1,123 @@
+import { describe, it, expect } from 'vitest';
+import { isUrl, normalizeUrl } from './urlNormalize';
+
+describe('isUrl', () => {
+ it('detects explicit http/https schemes', () => {
+ expect(isUrl('http://example.com')).toBe(true);
+ expect(isUrl('https://example.com')).toBe(true);
+ expect(isUrl('HTTPS://EXAMPLE.COM')).toBe(true);
+ });
+
+ it('detects git@ ssh remotes', () => {
+ expect(isUrl('[email protected]:owner/repo.git')).toBe(true);
+ });
+
+ it('detects bare domain-like tokens', () => {
+ expect(isUrl('example.com')).toBe(true);
+ expect(isUrl('sub.example.co.uk/path')).toBe(true);
+ });
+
+ it('rejects free text and whitespace-only input', () => {
+ expect(isUrl('just a thought')).toBe(false);
+ expect(isUrl('hello')).toBe(false);
+ expect(isUrl(' ')).toBe(false);
+ expect(isUrl('')).toBe(false);
+ });
+
+ it('rejects domain-like tokens that contain whitespace', () => {
+ // DOMAIN_PATTERN requires a single non-space token
+ expect(isUrl('foo. bar')).toBe(false);
+ });
+
+ it('trims before detecting', () => {
+ expect(isUrl(' example.com ')).toBe(true);
+ });
+
+ it('tolerates null/undefined', () => {
+ expect(isUrl(null)).toBe(false);
+ expect(isUrl(undefined)).toBe(false);
+ });
+});
+
+describe('normalizeUrl', () => {
+ it('returns null for empty / whitespace-only input', () => {
+ expect(normalizeUrl('')).toBe(null);
+ expect(normalizeUrl(' ')).toBe(null);
+ expect(normalizeUrl(null)).toBe(null);
+ expect(normalizeUrl(undefined)).toBe(null);
+ });
+
+ it('leaves existing http/https schemes untouched', () => {
+ expect(normalizeUrl('http://example.com')).toBe('http://example.com');
+ expect(normalizeUrl('https://example.com')).toBe('https://example.com');
+ });
+
+ it('trims surrounding whitespace', () => {
+ expect(normalizeUrl(' https://example.com ')).toBe('https://example.com');
+ });
+
+ // --- allowGit option (LinksTab + QuickBrainCapture behavior) ---
+ describe('allowGit: true (default)', () => {
+ it('treats git@ as already-normalized', () => {
+ expect(normalizeUrl('[email protected]:owner/repo.git', { allowGit: true }))
+ .toBe('[email protected]:owner/repo.git');
+ });
+
+ it('prepends https to a bare domain', () => {
+ expect(normalizeUrl('example.com', { allowGit: true })).toBe('https://example.com');
+ });
+
+ it('defaults allowGit to true', () => {
+ expect(normalizeUrl('[email protected]:owner/repo.git'))
+ .toBe('[email protected]:owner/repo.git');
+ });
+ });
+
+ // --- FeedsTab behavior: no git@ handling ---
+ describe('allowGit: false (FeedsTab)', () => {
+ it('prepends https to a git@ string (does NOT treat it as a scheme)', () => {
+ expect(normalizeUrl('[email protected]:owner/repo.git', { allowGit: false }))
+ .toBe('https://[email protected]:owner/repo.git');
+ });
+
+ it('still leaves http/https untouched', () => {
+ expect(normalizeUrl('http://feed.example.com/rss', { allowGit: false }))
+ .toBe('http://feed.example.com/rss');
+ });
+
+ it('prepends https to a bare feed host with no dot requirement', () => {
+ expect(normalizeUrl('feedhost', { allowGit: false })).toBe('https://feedhost');
+ });
+ });
+
+ // --- requireDot option (LinksTab quick-add guard) ---
+ describe('requireDot: true (LinksTab)', () => {
+ it('prepends https when the value contains a dot', () => {
+ expect(normalizeUrl('example.com', { allowGit: true, requireDot: true }))
+ .toBe('https://example.com');
+ });
+
+ it('prepends https when the value contains github.com (even without a dot token match)', () => {
+ expect(normalizeUrl('github.com', { allowGit: true, requireDot: true }))
+ .toBe('https://github.com');
+ });
+
+ it('returns null for a bare word with no dot', () => {
+ expect(normalizeUrl('notaurl', { allowGit: true, requireDot: true })).toBe(null);
+ });
+
+ it('still treats git@ as already-normalized under requireDot', () => {
+ expect(normalizeUrl('[email protected]:owner/repo.git', { allowGit: true, requireDot: true }))
+ .toBe('[email protected]:owner/repo.git');
+ });
+ });
+
+ describe('parity with original LinksTab normalizeUrl', () => {
+ const linksTabNormalize = (raw) => normalizeUrl(raw, { allowGit: true, requireDot: true });
+ it('matches: empty -> null', () => expect(linksTabNormalize(' ')).toBe(null));
+ it('matches: bare word -> null', () => expect(linksTabNormalize('hello')).toBe(null));
+ it('matches: domain -> https', () => expect(linksTabNormalize('foo.bar')).toBe('https://foo.bar'));
+ it('matches: git@ -> unchanged', () =>
+ expect(linksTabNormalize('git@host:repo')).toBe('git@host:repo'));
+ });
+});