Skip to content

Commit 53ca84c

Browse files
committed
clean up code
1 parent c55a1fb commit 53ca84c

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

frontend/src/components/profile-info-tooltip.tsx

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ interface ProfileInfoTooltipProps {
1111
className?: string;
1212
}
1313

14-
// Profile ID validation: only allow alphanumeric, hyphens, underscores, max 64 chars
1514
function isValidProfileId(profileId: string): boolean {
1615
return typeof profileId === 'string' &&
1716
profileId.length > 0 &&
1817
profileId.length <= 64 &&
19-
/^[a-zA-Z0-9_-]+$/.test(profileId);
18+
/^[a-zA-Z0-9_\- ]+$/.test(profileId);
2019
}
2120

2221

@@ -25,7 +24,7 @@ function getPortFromAdvanced(profile: LocalLoginProfile): string | null {
2524
const defaultPortItem = databaseTypeDropdownItems.find(item => item.id === dbType);
2625

2726
if (!defaultPortItem?.extra?.Port) {
28-
return null; // No default port found, hide this info
27+
return null;
2928
}
3029

3130
const defaultPort = defaultPortItem.extra.Port;
@@ -40,27 +39,26 @@ function getPortFromAdvanced(profile: LocalLoginProfile): string | null {
4039

4140
function getLastAccessedTime(profileId: string): string | null {
4241
if (!isValidProfileId(profileId)) {
43-
return null; // Invalid profile ID, hide this info
42+
return null;
4443
}
4544

4645
try {
4746
const lastAccessed = localStorage.getItem(`whodb_profile_last_accessed_${profileId}`);
4847
if (lastAccessed) {
4948
const date = new Date(lastAccessed);
5049
if (isNaN(date.getTime())) {
51-
return null; // Invalid date, hide this info
50+
return null;
5251
}
5352
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
5453
const formattedTimeZone = timeZone.replace(/_/g, ' ').split('/').join(' / ');
5554
return `${date.toLocaleDateString()} ${date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })} (${formattedTimeZone})`;
5655
}
5756
} catch (error) {
58-
// Silently fail - return null to hide this info
57+
return null;
5958
}
6059
return null;
6160
}
6261

63-
// Portal container reuse - create once and reuse
6462
let tooltipPortalContainer: HTMLDivElement | null = null;
6563

6664
function getTooltipPortalContainer(): HTMLDivElement {
@@ -80,30 +78,26 @@ export const ProfileInfoTooltip: FC<ProfileInfoTooltipProps> = ({ profile, class
8078
const port = getPortFromAdvanced(profile);
8179
const lastAccessed = getLastAccessedTime(profile.Id);
8280

83-
// If no information is available, don't render the component
8481
const hasInfo = port !== null || lastAccessed !== null;
8582
if (!hasInfo) {
8683
return null;
8784
}
8885

89-
// Show tooltip to the right of the icon
9086
const showTooltip = useCallback(() => {
9187
if (btnRef.current) {
9288
const rect = btnRef.current.getBoundingClientRect();
9389
setTooltipPos({
9490
top: rect.top + rect.height / 2,
95-
left: rect.right + 12, // 12px gap to the right
91+
left: rect.right + 12,
9692
});
9793
}
9894
setIsVisible(true);
9995
}, []);
10096

101-
// Hide tooltip
10297
const hideTooltip = useCallback(() => {
10398
setIsVisible(false);
10499
}, []);
105100

106-
// Memoized event handlers to prevent recreation
107101
const handleClickAway = useCallback((event: MouseEvent) => {
108102
if (
109103
btnRef.current &&
@@ -117,7 +111,6 @@ export const ProfileInfoTooltip: FC<ProfileInfoTooltipProps> = ({ profile, class
117111
if (event.key === "Escape") setIsVisible(false);
118112
}, []);
119113

120-
// Optimized event listeners - only add when visible, use stable handlers
121114
useEffect(() => {
122115
if (!isVisible) return;
123116

@@ -130,7 +123,6 @@ export const ProfileInfoTooltip: FC<ProfileInfoTooltipProps> = ({ profile, class
130123
};
131124
}, [isVisible, handleClickAway, handleKeyDown]);
132125

133-
// Memoize portal container to prevent recreation
134126
const portalContainer = useMemo(() => getTooltipPortalContainer(), []);
135127

136128
const tooltip = isVisible && tooltipPos
@@ -193,15 +185,14 @@ export const ProfileInfoTooltip: FC<ProfileInfoTooltipProps> = ({ profile, class
193185
);
194186
};
195187

196-
// Utility function to update last accessed time with validation
197188
export function updateProfileLastAccessed(profileId: string): void {
198189
if (!isValidProfileId(profileId)) {
199-
return; // Silently fail for invalid profile IDs
190+
return;
200191
}
201192

202193
try {
203194
localStorage.setItem(`whodb_profile_last_accessed_${profileId}`, new Date().toISOString());
204195
} catch (error) {
205-
// Silently fail - localStorage may be full or disabled
196+
206197
}
207198
}

0 commit comments

Comments
 (0)