Skip to content

Commit f202777

Browse files
saagar210claude
andcommitted
refactor(components): extract qualityThresholdsState helpers
Move the response-quality-thresholds sync wiring out of AnalyticsTab.tsx into a pure helper module: readCurrentThresholds (initial value) + subscribeToQualityThresholds (event listeners with returned cleanup). Shell useEffect shrinks to two lines. Wave 5.2. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent d98cb8c commit f202777

2 files changed

Lines changed: 34 additions & 20 deletions

File tree

src/components/Analytics/AnalyticsTab.tsx

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import {
88
ResponseQualitySummary,
99
} from "../../hooks/useAnalytics";
1010
import { useInsightsOps } from "../../hooks/useInsightsOps";
11+
import type { ResponseQualityThresholds } from "../../features/analytics/qualityThresholds";
1112
import {
12-
getResponseQualityThresholds,
13-
RESPONSE_QUALITY_THRESHOLDS_UPDATED_EVENT,
14-
ResponseQualityThresholds,
15-
} from "../../features/analytics/qualityThresholds";
13+
readCurrentThresholds,
14+
subscribeToQualityThresholds,
15+
} from "./qualityThresholdsState";
1616
import type { KbGapCandidate } from "../../types/insights";
1717
import { ArticleDetailPanel } from "./ArticleDetailPanel";
1818
import { PilotDiagnosticsSection } from "./PilotDiagnosticsSection";
@@ -55,7 +55,7 @@ export function AnalyticsTab({
5555
const [qualityDrilldown, setQualityDrilldown] =
5656
useState<ResponseQualityDrilldownExamples | null>(null);
5757
const [qualityThresholds, setQualityThresholds] =
58-
useState<ResponseQualityThresholds>(() => getResponseQualityThresholds());
58+
useState<ResponseQualityThresholds>(() => readCurrentThresholds());
5959
const [kbUsage, setKbUsage] = useState<ArticleUsage[]>([]);
6060
const [lowRatingData, setLowRatingData] = useState<LowRatingAnalysis | null>(
6161
null,
@@ -126,21 +126,8 @@ export function AnalyticsTab({
126126
}, [loadData]);
127127

128128
useEffect(() => {
129-
const syncThresholds = () =>
130-
setQualityThresholds(getResponseQualityThresholds());
131-
syncThresholds();
132-
window.addEventListener(
133-
RESPONSE_QUALITY_THRESHOLDS_UPDATED_EVENT,
134-
syncThresholds,
135-
);
136-
window.addEventListener("storage", syncThresholds);
137-
return () => {
138-
window.removeEventListener(
139-
RESPONSE_QUALITY_THRESHOLDS_UPDATED_EVENT,
140-
syncThresholds,
141-
);
142-
window.removeEventListener("storage", syncThresholds);
143-
};
129+
setQualityThresholds(readCurrentThresholds());
130+
return subscribeToQualityThresholds(setQualityThresholds);
144131
}, []);
145132

146133
const overviewContent = (() => {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
getResponseQualityThresholds,
3+
RESPONSE_QUALITY_THRESHOLDS_UPDATED_EVENT,
4+
type ResponseQualityThresholds,
5+
} from "../../features/analytics/qualityThresholds";
6+
7+
export function readCurrentThresholds(): ResponseQualityThresholds {
8+
return getResponseQualityThresholds();
9+
}
10+
11+
export function subscribeToQualityThresholds(
12+
onChange: (next: ResponseQualityThresholds) => void,
13+
): () => void {
14+
const handler = () => onChange(getResponseQualityThresholds());
15+
if (typeof window === "undefined") {
16+
return () => undefined;
17+
}
18+
window.addEventListener(RESPONSE_QUALITY_THRESHOLDS_UPDATED_EVENT, handler);
19+
window.addEventListener("storage", handler);
20+
return () => {
21+
window.removeEventListener(
22+
RESPONSE_QUALITY_THRESHOLDS_UPDATED_EVENT,
23+
handler,
24+
);
25+
window.removeEventListener("storage", handler);
26+
};
27+
}

0 commit comments

Comments
 (0)