Skip to content

chore(logger): incorporates withLogger implementation #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/app/snippets/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import React from 'react';
import Head from "next/head";
import { Card } from "../../components/Card";
import Card from "../../components/Card";
import { mockSnippets } from '@/constants';

export default function SnippetsPage() {

function SnippetsPage() {
return (
<div className="min-h-screen bg-black">
<Head>
Expand All @@ -30,3 +31,7 @@ export default function SnippetsPage() {
</div>
);
}

SnippetsPage.displayName = 'SnippetsPage';

export default SnippetsPage;
27 changes: 23 additions & 4 deletions src/components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,32 @@ import { useMotionValue } from "framer-motion";
import { CardPattern } from "./CardPattern";
import { useRouter } from "next/navigation";
import { CardProps } from "@/types/snippet.types";
import { withLogger } from "@/utils/withLogger";



export const Card = ({ snippet }: CardProps) => {
const Card = ({ snippet }: CardProps) => {
const router = useRouter();
const mouseX = useMotionValue(0);
const mouseY = useMotionValue(0);
const [randomString, setRandomString] = useState("");
const [isHovered, setIsHovered] = useState(false);

const componentName = 'CardComponent';

useEffect(() => {
const str = generateRandomString(1500);
setRandomString(str);
console.log(`🎯 [${componentName}] Random string generated on mount`);
}, []);

// Log state changes
useEffect(() => {
console.log(`🎯 [${componentName}] Hover state changed:`, isHovered);
}, [isHovered]);

useEffect(() => {
console.log(`🎯 [${componentName}] Random string updated:`, randomString.length, 'characters');
}, [randomString]);

function onMouseMove({
currentTarget,
clientX,
Expand All @@ -34,10 +45,13 @@ export const Card = ({ snippet }: CardProps) => {
mouseY.set(clientY - top);
const str = generateRandomString(1500);
setRandomString(str);

console.log(`🖱️ [${componentName}] Mouse moved:`, { x: mouseX, y: mouseY });
}

const handleClick = (e: React.MouseEvent) => {
e.preventDefault();
console.log(`🔗 Card clicked, navigating to:`, `/snippets/${snippet.id}`);
router.push(`/snippets/${snippet.id}`, { scroll: false });
};

Expand All @@ -60,6 +74,7 @@ export const Card = ({ snippet }: CardProps) => {
mouseY={mouseY.get()}
randomString={randomString}
/>

<div className="relative z-10 flex-1 flex flex-col">
<h3 className="text-xl font-bold text-white mb-2 group-hover:text-blue-400 transition-colors">
{snippet.title}
Expand Down Expand Up @@ -107,4 +122,8 @@ function generateRandomString(length: number) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
};
}

Card.displayName = 'CardComponent';

export default withLogger(Card);
18 changes: 0 additions & 18 deletions src/components/ui/select-content.tsx

This file was deleted.

18 changes: 0 additions & 18 deletions src/components/ui/select-trigger.tsx

This file was deleted.

10 changes: 0 additions & 10 deletions src/components/ui/select-value.tsx

This file was deleted.

59 changes: 59 additions & 0 deletions src/utils/withLogger.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { useEffect, useRef } from "react";

export function withLogger<P extends object>(WrappedComponent: React.ComponentType<P>) {
return function LoggerComponent(props: P) {
const componentName = WrappedComponent.displayName || WrappedComponent.name || 'Component';
const renderCount = useRef(0);
const previousProps = useRef<P | null>(null);

// Increment render count
renderCount.current += 1;

useEffect(() => {
console.log(`🚀 [${componentName}] Component mounted at:`, new Date().toISOString());
console.log(`📦 [${componentName}] Initial Props:`, props);

// Log performance timing
const startTime = performance.now();

return () => {
const endTime = performance.now();
console.log(`💀 [${componentName}] Component unmounted at:`, new Date().toISOString());
console.log(`⏱️ [${componentName}] Component was mounted for:`, `${(endTime - startTime).toFixed(2)}ms`);
console.log(`🔄 [${componentName}] Total renders:`, renderCount.current);
};
}, []);

// Log every re-render with render count
useEffect(() => {
if (renderCount.current > 1) {
console.log(`🔄 [${componentName}] Re-render #${renderCount.current} at:`, new Date().toISOString());
console.log(`📦 [${componentName}] Current Props:`, props);

// Check if props actually changed
if (previousProps.current) {
const propsChanged = JSON.stringify(previousProps.current) !== JSON.stringify(props);
if (propsChanged) {
console.log(`🔀 [${componentName}] Props changed!`);
console.log(`📦 [${componentName}] Previous Props:`, previousProps.current);
console.log(`📦 [${componentName}] New Props:`, props);
} else {
console.log(`🔄 [${componentName}] Re-render triggered by internal state change (props unchanged)`);
}
}
}

// Store current props for next comparison
previousProps.current = JSON.parse(JSON.stringify(props));
});

// Detailed prop change logging
useEffect(() => {
if (renderCount.current > 1) {
console.log(`📝 [${componentName}] Props effect triggered - props may have changed:`, props);
}
}, [props]);

return <WrappedComponent {...props} />;
};
}