Skip to content

Instantly share code, notes, and snippets.

View devhammed's full-sized avatar
💭
Changing the world, one dollar sign in my PHP code at a time!

Hammed Oyedele devhammed

💭
Changing the world, one dollar sign in my PHP code at a time!
View GitHub Profile
@devhammed
devhammed / ContentNegotiator.php
Created December 4, 2025 09:33
Laravel Content Negotiator Middleware (e.g. /glossaries for HTML & /glossaries.json for JSON)
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class ContentNegotiator
{
@devhammed
devhammed / use-controlled-state.ts
Last active November 27, 2025 07:32
Use Controlled State hook manages a value that can be either controlled or uncontrolled. It returns the current state and a setter that updates internal state when uncontrolled and always calls an optional onChange callback.
import { useCallback, useState } from 'react';
export function useControlledState<T>(
controlledValue: T | undefined,
initialValue: T,
onChange?: (value: T) => void,
): [T, (value: T | ((prev: T) => T)) => void] {
const [internalState, setInternalState] = useState(initialValue);
const isControlled = controlledValue !== undefined;
@devhammed
devhammed / data-table.tsx
Last active November 27, 2025 09:29
ShadCN UI Data Table with pagination (server, client, infinite), row selection, column visibility, column filtering (server, client), column sorting (server, client).
import { Table, TableBody, TableCell, TableFooter, TableHead, TableHeader, TableRow } from '@/components/ui/table';
import { useControlledState } from '@/hooks/use-controlled-state';
import { cn } from '@/lib/utils';
import { InfiniteScroll } from '@inertiajs/react';
import {
ColumnDef,
ColumnFiltersState,
flexRender,
getCoreRowModel,
getFilteredRowModel,
@devhammed
devhammed / parse-separated.ts
Created November 13, 2025 13:57
Parse separated string (CSV, TSV, SSV) according to [RFC-4180](https://www.ietf.org/rfc/rfc4180.txt).
function parseSeparated(text: string, delimiter: string = ','): string[][] {
const rows: string[][] = [];
let field = '';
let row: string[] = [];
let inQuotes = false;
for (let i = 0; i < text.length; i++) {
const char = text[i];
const next = text[i + 1];
@devhammed
devhammed / use-object-url.ts
Last active November 10, 2025 19:59
React Hook For Managing Object URLs.
import { useEffect, useState } from 'react';
export type BlobType = File | Blob | MediaSource | null;
export function useObjectUrl(initialObject: BlobType | (() => BlobType) = null) {
const [objectUrl, setObjectUrl] = useState<string | null>(null);
const [object, setObject] = useState<BlobType>(initialObject);
useEffect(() => {
@devhammed
devhammed / BackgroundPollingJob.php
Created November 7, 2025 00:43
Laravel Background Polling Queue Job is a job that checks for the status of something in any source like external API or database and re-queue itself if the item is not at the desirable status yet.
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class BackgroundPollingJob implements ShouldQueue
@devhammed
devhammed / tap.ts
Created October 31, 2025 14:07
Implementation Of Laravel "tap" Helper In TypeScript
type TapProxy<T extends object> = {
[K in keyof T]: T[K] extends (...args: infer A) => unknown
? (...args: A) => T
: T[K];
};
function tap<T extends object>(obj: T): TapProxy<T>;
function tap<T extends object>(obj: T, fn: (target: T) => void): T;
function tap<T extends object>(
obj: T,
@devhammed
devhammed / Laravel.Dockerfile
Last active November 14, 2025 12:40
Laravel Docker Setup (NGINX, PHP-FPM, Queue Worker, Scheduler, Reverb)
# Stage 1: Build
FROM php:8.4-cli AS build
# Setup build root
WORKDIR /app
# Install dependencies
RUN apt-get update && apt-get install -y \
git \
unzip \
@devhammed
devhammed / scroll-area.tsx
Created October 10, 2025 17:50
Shadcn UI Scroll Area with Overflow Fading Support
import { cn } from '@/lib/utils';
import * as ScrollAreaPrimitive from '@radix-ui/react-scroll-area';
import * as React from 'react';
import { useEffect, useRef, useState } from 'react';
const ScrollArea = React.forwardRef<
React.ComponentRef<typeof ScrollAreaPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root> & { fade?: boolean }
>(({ className, fade, children, ...props }, ref) => {
const viewPortRef = useRef<HTMLDivElement | null>(null);
@devhammed
devhammed / use-api-form.ts
Last active November 21, 2025 07:34
Laravel Inertia JSON API Form Helper Hook
import { FormDataKeys, FormDataType, Method } from '@inertiajs/core';
// @ts-expect-error TS2307
import type { Response } from '@inertiajs/core/types/response';
import type { InertiaFormProps } from '@inertiajs/react';
import { useForm } from '@inertiajs/react';
import axios, { AxiosProgressEvent, AxiosRequestConfig, AxiosResponse } from 'axios';
import { cloneDeep } from 'lodash-es';
import { useCallback, useMemo, useRef, useState } from 'react';
type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';