Skip to content

Commit b1d2c88

Browse files
authored
[dashboards] Fix source filter firing navigation on every keystroke (#131)
* [dashboards] Fix source filter firing navigation on every keystroke * Sync sourceInput when currentSource changes externally Fixes stale input state on browser back/forward or external URL changes, as caught by @shansen3.
1 parent 8117230 commit b1d2c88

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

dashboards/open-brain-dashboard-next/components/ThoughtsFilter.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import { useRouter } from "next/navigation";
4-
import { useCallback } from "react";
4+
import { useCallback, useEffect, useState } from "react";
55

66
const IMPORTANCE_OPTIONS = [1, 2, 3, 4, 5];
77

@@ -17,6 +17,9 @@ export function ThoughtsFilter({
1717
currentImportance: number | undefined;
1818
}) {
1919
const router = useRouter();
20+
const [sourceInput, setSourceInput] = useState(currentSource);
21+
22+
useEffect(() => { setSourceInput(currentSource); }, [currentSource]);
2023

2124
const applyFilters = useCallback(
2225
(overrides: Record<string, string>) => {
@@ -58,8 +61,16 @@ export function ThoughtsFilter({
5861
<label className="block text-xs text-text-muted mb-1">Source</label>
5962
<input
6063
type="text"
61-
value={currentSource}
62-
onChange={(e) => applyFilters({ source_type: e.target.value })}
64+
value={sourceInput}
65+
onChange={(e) => setSourceInput(e.target.value)}
66+
onBlur={() => {
67+
if (sourceInput !== currentSource)
68+
applyFilters({ source_type: sourceInput });
69+
}}
70+
onKeyDown={(e) => {
71+
if (e.key === "Enter")
72+
applyFilters({ source_type: sourceInput });
73+
}}
6374
placeholder="e.g. chatgpt_import"
6475
className="bg-bg-elevated border border-border rounded-lg px-3 py-2 text-sm text-text-primary placeholder-text-muted focus:outline-none focus:border-violet w-44"
6576
/>
@@ -89,9 +100,10 @@ export function ThoughtsFilter({
89100

90101
{(currentType || currentSource || currentImportance) && (
91102
<button
92-
onClick={() =>
93-
applyFilters({ type: "", source_type: "", importance_min: "" })
94-
}
103+
onClick={() => {
104+
setSourceInput("");
105+
applyFilters({ type: "", source_type: "", importance_min: "" });
106+
}}
95107
className="text-xs text-text-muted hover:text-danger transition-colors pb-2"
96108
>
97109
Clear filters

0 commit comments

Comments
 (0)