Skip to content

Latest commit

 

History

History
227 lines (171 loc) · 10.3 KB

File metadata and controls

227 lines (171 loc) · 10.3 KB

The lib folder in a project is typically used for utility functions, helper methods, and other reusable code that doesn't fit neatly into other categories like components, services, or hooks. These utilities are often used across multiple parts of the application and help keep the codebase DRY (Don't Repeat Yourself).

Here are some common uses for the lib folder:

  1. Utility Functions: General-purpose functions that can be used throughout the application.

    • Example: Formatting dates, manipulating arrays, or handling string operations.
  2. Helper Methods: Functions that assist with specific tasks or operations.

    • Example: Functions to handle API responses, validation logic, or data transformations.
  3. Configuration: Configuration files or constants that are used across the application.

    • Example: API endpoints, environment variables, or application-wide settings.
  4. Custom Hooks: Reusable React hooks that encapsulate specific logic.

    • Example: Hooks for fetching data, managing forms, or handling authentication.
  5. Context Providers: Context and provider components for managing global state.

    • Example: Theme context, authentication context, or user settings context.

In your specific case, the cn function imported from @/lib/utils is likely a utility function used for conditionally combining class names. This is a common pattern in React applications to dynamically apply classes based on certain conditions.

Here's an example of what the cn function might look like:

// src/lib/utils.ts
export function cn(...classes: (string | boolean | undefined)[]) {
	return classes.filter(Boolean).join(" ");
}

This function takes multiple class names as arguments and joins them into a single string, filtering out any falsy values. This is useful for conditionally applying classes in your components.

Overall, the lib folder helps organize and centralize reusable code, making it easier to maintain and reducing duplication across the codebase.

Sure, here are examples of each of the things mentioned above that you might include in the lib folder:

1. Utility Functions

Utility functions are general-purpose functions that can be used throughout the application.

Example: formatDate.ts

// src/lib/formatDate.ts
import { format } from "date-fns";

export function formatDate(
	date: Date,
	dateFormat: string = "yyyy-MM-dd"
): string {
	return format(date, dateFormat);
}

2. Helper Methods

Helper methods assist with specific tasks or operations.

Example: apiHelpers.ts

// src/lib/apiHelpers.ts
export async function fetchData(url: string) {
	const response = await fetch(url);
	if (!response.ok) {
		throw new Error("Network response was not ok");
	}
	return response.json();
}

3. Configuration

Configuration files or constants that are used across the application.

Example: config.ts

// src/lib/config.ts
export const API_BASE_URL = "https://api.example.com";
export const APP_NAME = "MyApp";

4. Custom Hooks

Reusable React hooks that encapsulate specific logic.

Example: useFetch.ts

// src/lib/useFetch.ts
import { useState, useEffect } from "react";

export function useFetch(url: string) {
	const [data, setData] = useState(null);
	const [loading, setLoading] = useState(true);
	const [error, setError] = useState(null);

	useEffect(() => {
		async function fetchData() {
			try {
				const response = await fetch(url);
				const result = await response.json();
				setData(result);
			} catch (error) {
				setError(error);
			} finally {
				setLoading(false);
			}
		}

		fetchData();
	}, [url]);

	return { data, loading, error };
}

5. Context Providers

Context and provider components for managing global state.

Example: ThemeContext.tsx

// src/lib/ThemeContext.tsx
import { createContext, useContext, useState, ReactNode } from "react";

type Theme = "light" | "dark";

interface ThemeContextProps {
	theme: Theme;
	toggleTheme: () => void;
}

const ThemeContext = createContext<ThemeContextProps | undefined>(undefined);

export const ThemeProvider = ({ children }: { children: ReactNode }) => {
	const [theme, setTheme] = useState<Theme>("light");

	const toggleTheme = () => {
		setTheme((prevTheme) => (prevTheme === "light" ? "dark" : "light"));
	};

	return (
		<ThemeContext.Provider value={{ theme, toggleTheme }}>
			{children}
		</ThemeContext.Provider>
	);
};

export const useTheme = () => {
	const context = useContext(ThemeContext);
	if (!context) {
		throw new Error("useTheme must be used within a ThemeProvider");
	}
	return context;
};

Example Usage in CalendarDateRangePicker.tsx

Here's how you might use some of these utilities in your CalendarDateRangePicker.tsx file:

import { CalendarIcon } from "lucide-react";
import { addDays, format } from "date-fns";
import { DateRange } from "react-day-picker";

import { cn } from "@/lib/utils";
import { Calendar } from "@/components/ui/calendar";
import {
	Popover,
	PopoverContent,
	PopoverTrigger,
} from "@/components/ui/popover";
import { useState } from "react";
import { formatDate } from "@/lib/formatDate"; // Utility function
import { API_BASE_URL } from "@/lib/config"; // Configuration
import { useFetch } from "@/lib/useFetch"; // Custom hook

export function CalendarDateRangePicker({
	className,
}: React.HTMLAttributes<HTMLDivElement>) {
	const [date, setDate] = useState<DateRange | undefined>({
		from: new Date(2023, 0, 20),
		to: addDays(new Date(2023, 0, 20), 20),
	});

	const { data, loading, error } = useFetch(`${API_BASE_URL}/some-endpoint`);

	return (
		<div className={cn("grid gap-2 h-full", className)}>
			<Popover>
				<PopoverTrigger asChild>
					<button
						id='date'
						className={cn(
							"w-[260px] text-left font-normal flex items-center justify-center text-[.8rem] border border-borderColor",
							!date && "text-muted-foreground"
						)}>
						<CalendarIcon className='mr-2 h-4 w-4' />
						{date?.from ? (
							<span>{formatDate(date.from, "MMM dd, yyyy")}</span>
						) : (
							"Select Date"
						)}
					</button>
				</PopoverTrigger>
				<PopoverContent>
					<Calendar
						selected={date}
						onSelect={setDate}
						numberOfMonths={2}
					/>
				</PopoverContent>
			</Popover>
		</div>
	);
}

In this example, the formatDate utility function is used to format the date, the API_BASE_URL constant is used for fetching data, and the useFetch custom hook is used to fetch data from an API. This demonstrates how you can organize and reuse code effectively using the lib folder.