Skip to content

Instantly share code, notes, and snippets.

View james2doyle's full-sized avatar

James Doyle james2doyle

View GitHub Profile
@james2doyle
james2doyle / ClientGate.tsx
Last active March 25, 2024 22:42
React.useSyncExternalStore is exactly what we need to avoid hydration errors, and moreover, if we transition to a page with useSyncExternalStore on the client, the clientSnapshot will immediately be taken. Taken from: https://tkdodo.eu/blog/avoiding-hydration-mismatches-with-use-sync-external-store
/**
* ClientGate - a component that will only render on the client, where you can safely access browser only APIs
*
* Usage: `<ClientGate>{() => `Hello Client ${window.title}`}</ClientGate>`
* @see https://tkdodo.eu/blog/avoiding-hydration-mismatches-with-use-sync-external-store#usesyncexternalstore
*/
const emptySubscribe = () => () => {};
const ClientGate = ({ children }: React.PropsWithChildren) => {
@james2doyle
james2doyle / .phpactor.yml
Created March 22, 2024 22:25
A yaml config for phpactor that can be used in Laravel. Supports laravel-ide-helper files
indexer:
stub_paths:
- "%application_root%/_ide_helper.php"
- "%application_root%/_ide_helper_models.php"
- "%application_root%/vendor/laravel/framework/src/Illuminate/Support/Facades"
- "%application_root%/vendor/laravel/framework/src/Illuminate/Database/Eloquent"
- "%application_root%/vendor/laravel/framework/src/Illuminate/Http"
- "%application_root%/vendor/laravel/framework/src/Illuminate/Routing"
- "%application_root%/vendor/laravel/framework/src/Illuminate/View"
- "%application_root%/vendor/laravel/framework/src/Illuminate/Foundation"
@james2doyle
james2doyle / ffmpeg-convert.sh
Last active March 15, 2024 18:57
Downscale a folder of mp4 files using ffmpeg. This loop takes an input video file, scales it to a height of 720 pixels while maintaining the aspect ratio, encodes the video using H.264 and the audio using AAC, and saves the output as an MP4 file with the same base name as the input file, but with "0-" prepended to it.
#!/usr/bin/env bash
# The provided ffmpeg command performs several operations:
#`-vf "scale=-1:720,setsar=1"`: The `-vf` option stands for "video filter" and allows you to apply various video processing operations. In this case, two filters are applied:
# - `scale=-1:720`: This filter scales the video vertically to a height of 720 pixels, while maintaining the original aspect ratio. The `-1` value for the width means that the width will be calculated automatically based on the new height.
# - `setsar=1`: This filter sets the sample aspect ratio (SAR) to 1, which ensures that the video is displayed with square pixels.
#`-c:v libx264`: This option specifies the video codec to be used for the output file. In this case, it's the x264 codec, which is a popular and widely-used H.264 video codec.
#`-crf 20`: The Constant Rate Factor (CRF) is a quality setting for the x264 codec. A lower CRF value (e.g., 20) results in higher quality and larger file size, while a higher CRF value (e.g., 28) results in lower qual
@james2doyle
james2doyle / get-script-promise.js
Created November 17, 2016 18:37
Async load a script in the page and run it. Uses promises
// this function will work cross-browser for loading scripts asynchronously
function loadScript(src) {
return new Promise(function(resolve, reject) {
const s = document.createElement('script');
let r = false;
s.type = 'text/javascript';
s.src = src;
s.async = true;
s.onerror = function(err) {
reject(err, s);
@james2doyle
james2doyle / simple-json-reponse.php
Last active March 7, 2024 06:02
A simple JSON response function for PHP. Used in various PhileCMS plugins.
<?php
function json_response($code = 200, $message = null)
{
// clear the old headers
header_remove();
// set the actual code
http_response_code($code);
// set the header to make sure cache is forced
header("Cache-Control: no-transform,public,max-age=300,s-maxage=900");
@james2doyle
james2doyle / cheatsheet-markdown.md
Last active February 26, 2024 04:07
a markdown cheatsheet showing all the elements and syntax. Stolen from the Mou.app for OSX http://mouapp.com/

Mou

Mou icon

Overview

Mou, the missing Markdown editor for web developers.

Syntax

@james2doyle
james2doyle / getBrowser.php
Last active February 20, 2024 06:06
a PHP function to sniff the user agent of the browser and return a nice object with all the information
<?php
// http://www.php.net/manual/en/function.get-browser.php#101125
function getBrowser() {
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version= "";
// First get the platform?
if (preg_match('/linux/i', $u_agent)) {
@james2doyle
james2doyle / slim-stream-route.php
Created January 10, 2018 06:05
Create a streaming download of a large file with Slim PHP using the build in Stream class
<?php
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\Stream;
$app->get('/stream', function (Request $request, Response $response, array $args) {
// a 100mb file
$path = '../public/files/document.pdf';
@james2doyle
james2doyle / clipboard.lua
Created March 22, 2019 16:15
A clipboard plugin for Hammerspoon that uses the chooser to show/select items in the clipboard
--[[
https://github.com/VFS/.hammerspoon
]]--
--[[
This is my attempt to implement a jumpcut replacement in Lua/Hammerspoon.
It monitors the clipboard/pasteboard for changes, and stores the strings you copy to the transfer area.
You can access this history on the menu (Unicode scissors icon).
Clicking on any item will add it to your transfer area.
If you open the menu while pressing option/alt, you will enter the Direct Paste Mode. This means that the selected item will be
@james2doyle
james2doyle / search-paracs-url-decode.js
Last active February 2, 2024 23:20
Decode a URL into an object without any libraries using URLSearchParams and Object.fromEntries
Object.fromEntries(new URLSearchParams(window.location.search).entries())