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 / use-animation-frame.ts
Created March 3, 2026 17:51
React Use Animation Frame Hook
import { DependencyList, useCallback, useEffect, useRef } from 'react';
const raf =
typeof window !== 'undefined'
? window.requestAnimationFrame ||
((cb: FrameRequestCallback) => window.setTimeout(() => cb(Date.now()), 1_000 / 60))
: () => undefined;
const caf = typeof window !== 'undefined' ? window.cancelAnimationFrame || window.clearTimeout : () => undefined;
@devhammed
devhammed / bundle_monorepo_packages.ts
Last active March 2, 2026 18:46
Adonis.js "buildFinished" hook that copies monorepo packages into the build folder.
import { hooks } from '@adonisjs/core/app'
import { cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
import path from 'node:path'
const PACKAGE_PREFIX = 'flowshub-'
export default hooks.buildFinished(async (bundler) => {
const repoRoot = path.resolve(bundler.cwdPath, '../../')
const buildDir = path.join(bundler.cwdPath, 'build')
@devhammed
devhammed / transmit.ts
Last active February 26, 2026 10:33
AdonisJS v7 Transmit Setup (you must uninstall the official package then register the custom provider and preload file).
import type { HttpContext } from '@adonisjs/core/http'
import app from '@adonisjs/core/services/app'
import router from '@adonisjs/core/services/router'
import { RuntimeException } from '@poppinss/utils/exception'
const transmit = await app.container.make('transmit')
transmit.authorize<{ id: string }>('users/:id', async (ctx: HttpContext, params) => {
const userId = ctx.auth.user?.id
@devhammed
devhammed / FormatsResponse.php
Created February 25, 2026 07:24
Laravel Response Formatter
<?php
declare(strict_types=1);
namespace App\Concerns;
use Closure;
use Inertia\Inertia;
use Inertia\Response;
use InertiaUI\Modal\Modal;
@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];