Skip to content

Commit d048ebb

Browse files
committed
refactor: simplify useAnimatedPlaceholder and remove unused useEffect in useDateRange
- Refactored the `useAnimatedPlaceholder` hook to streamline the timeout logic and reduce redundancy. - Removed an unused `useEffect` in the `useDateRange` hook to clean up the codebase. - Enhanced error handling in the `useFilterManagement` hook by adding a try-catch block around the API call for better resilience.
1 parent 278af5e commit d048ebb

3 files changed

Lines changed: 39 additions & 42 deletions

File tree

apps/frontend/src/hooks/useAnimatedPlaceholder.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,40 @@ export const useAnimatedPlaceholder = ({
4343
let timeoutId: ReturnType<typeof setTimeout>;
4444

4545
if (phase === 'typing') {
46-
if (currentText.length < currentExample.length) {
47-
timeoutId = setTimeout(() => {
48-
setCurrentText(currentExample.slice(0, currentText.length + 1));
49-
}, typingSpeed);
50-
} else {
51-
timeoutId = setTimeout(() => {
52-
setPhase('pausing');
53-
}, pauseDuration);
54-
}
46+
timeoutId = setTimeout(() => {
47+
setCurrentText((prevText) => {
48+
if (prevText.length < currentExample.length) {
49+
return currentExample.slice(0, prevText.length + 1);
50+
} else {
51+
setPhase('pausing');
52+
return prevText;
53+
}
54+
});
55+
}, typingSpeed);
5556
} else if (phase === 'pausing') {
5657
timeoutId = setTimeout(() => {
5758
setPhase('deleting');
5859
}, pauseDuration);
5960
} else if (phase === 'deleting') {
60-
if (currentText.length > 0) {
61-
timeoutId = setTimeout(() => {
62-
setCurrentText(currentText.slice(0, -1));
63-
}, deletingSpeed);
64-
} else {
65-
setCurrentIndex((prevIndex) => (prevIndex + 1) % examples.length);
66-
setPhase('typing');
67-
}
61+
timeoutId = setTimeout(() => {
62+
setCurrentText((prevText) => {
63+
if (prevText.length > 0) {
64+
return prevText.slice(0, -1);
65+
} else {
66+
setCurrentIndex((prevIndex) => (prevIndex + 1) % examples.length);
67+
setPhase('typing');
68+
return prevText;
69+
}
70+
});
71+
}, deletingSpeed);
6872
}
6973

7074
return () => {
7175
if (timeoutId) {
7276
clearTimeout(timeoutId);
7377
}
7478
};
75-
}, [currentText, currentIndex, phase, examples, isActive, typingSpeed, pauseDuration, deletingSpeed]);
79+
}, [currentIndex, phase, examples, isActive, typingSpeed, pauseDuration, deletingSpeed]);
7680

7781
return currentText;
7882
};

apps/frontend/src/hooks/useDateRange.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,6 @@ export const useDateRange = ({ t }: UseDateRangeProps) => {
4949

5050
const committedTab = dateRangeSettings.committedTab;
5151

52-
useEffect(() => {
53-
const startDateString = dateRange[0]?.toISOString();
54-
const endDateString = dateRange[1]?.toISOString();
55-
56-
setDateRangeSettings((prev) => ({
57-
...prev,
58-
datePicker: {
59-
startDate: startDateString || null,
60-
endDate: endDateString || null,
61-
},
62-
}));
63-
}, [dateRange, setDateRangeSettings]);
64-
6552
const setDateRangeTab = (tab: number) => {
6653
setDateRangeSettings((prev) => ({
6754
...prev,

apps/frontend/src/hooks/useFilterManagement.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,24 @@ export const useFilterManagement = ({
312312

313313
// Also try API call if authenticated
314314
if (authToken) {
315-
const response = await fetch(
316-
getApiUrl(`/chats/${currentChatId}/filters`),
317-
{
318-
headers: {
319-
Authorization: `Bearer ${authToken}`,
320-
},
315+
try {
316+
const response = await fetch(
317+
getApiUrl(`/chats/${currentChatId}/filters`),
318+
{
319+
headers: {
320+
Authorization: `Bearer ${authToken}`,
321+
},
322+
}
323+
);
324+
325+
if (response.ok) {
326+
const apiFilters = await response.json();
327+
console.log("✅ API filters loaded:", apiFilters.length);
328+
} else if (response.status === 401) {
329+
console.warn("⚠️ Unauthorized - auth token may be expired or invalid");
321330
}
322-
);
323-
324-
if (response.ok) {
325-
const apiFilters = await response.json();
326-
console.log("✅ API filters loaded:", apiFilters.length);
331+
} catch (apiError) {
332+
console.warn("⚠️ Failed to load filters from API (continuing with localStorage):", apiError);
327333
}
328334
}
329335
} catch (error) {

0 commit comments

Comments
 (0)