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 / get-media-duration.ts
Last active February 3, 2026 10:16
Get Media File Duration In Seconds (With Low-Budget Caching)
/**
* Get media file duration in seconds.
*/
export function getMediaDuration(file: File): Promise<number> {
if (!window.getMediaDurationCache) {
window.getMediaDurationCache = new WeakMap();
}
const cached = window.getMediaDurationCache.get(file);
@devhammed
devhammed / use-isomorphic-layout-effect.ts
Created February 3, 2026 08:40
Use Effect That Works On Server and Browser.
import * as React from 'react';
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
export { useIsomorphicLayoutEffect };
@devhammed
devhammed / combobox.tsx
Last active February 5, 2026 21:17
ShadCN UI Combobox (based on Command and Popover components).
import {
Command,
CommandEmpty,
CommandGroup,
CommandItem,
CommandList,
} from '@/components/ui/command'
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
import { useControlledState } from '@/hooks/use-controlled-state'
import { useIsomorphicLayoutEffect } from '@/hooks/use-isomorphic-layout-effect'
@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 February 3, 2026 11:53
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 { useIsomorphicLayoutEffect } from '@/hooks/use-isomorphic-layout-effect';
import { useCallback, useRef, useState } from 'react';
import { flushSync } from 'react-dom';
export function useControlledState<T>(
initialValue: T,
controlledValue?: T,
onChange?: (value: T) => void,
): [T, (value: T | ((prev: T) => T)) => void] {
const [internalValue, setInternalValue] = useState(initialValue);
@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 \