Skip to content

Instantly share code, notes, and snippets.

View itsMapleLeaf's full-sized avatar

Darius itsMapleLeaf

View GitHub Profile
@itsMapleLeaf
itsMapleLeaf / tree.txt
Last active March 11, 2023 08:43
folder structure
src/web
├── auth
│ ├── auth-context.tsx
│ ├── auth-store.ts
│ └── login-form.tsx
├── channel
│ ├── active-channel-list.tsx
│ ├── channel-browser-page.tsx
│ ├── channel-icon.tsx
│ ├── channel-page.tsx
@itsMapleLeaf
itsMapleLeaf / retry.ts
Created February 20, 2022 22:54
retry function
export async function retry<T>(
times: number,
fn: () => Promise<T>,
): Promise<T> {
let lastError: unknown
while (times > 0) {
try {
return await fn()
} catch (error) {
lastError = error
export class Maybe<Value> {
constructor(private readonly value: Value | undefined | null) {}
static of<T>(value: T): Maybe<T> {
return new Maybe(value)
}
static empty<T>(): Maybe<T> {
return new Maybe<T>(undefined)
}
@itsMapleLeaf
itsMapleLeaf / restart-cinnamon.sh
Created December 11, 2021 04:24
cinnamon restart script
#!/bin/bash
pkill -HUP -f "cinnamon --replace"
killall -HUP cinnamon
export DISPLAY=":0"
cinnamon &
# https://unix.stackexchange.com/questions/30370/how-to-get-the-pid-of-the-last-executed-command-in-shell-script
disown $!
@itsMapleLeaf
itsMapleLeaf / 1-before.ts
Last active September 18, 2021 20:57
guard pattern
get: async (context) => {
const user = await createSessionHelpers(context).getUser()
if (!user) {
return redirect("/login")
}
const id = context.params?.bucketId
if (!id) {
return notFound()
}
@itsMapleLeaf
itsMapleLeaf / timeout.js
Created August 26, 2021 14:43
how to settimeout with hooks
useEffect(() => {
const timeout = setTimeout(() => {
doSomething()
}, 5000)
return () => clearTimeout(timeout)
}, [])

Ensure your react components have a function name so they show up in devtools

// has no name
const Comp = forwardRef(() => {})
export default Comp

// has a name
const Comp = () => {}
export default forwardRef(Comp)
@itsMapleLeaf
itsMapleLeaf / debounce-throttle.ts
Created August 10, 2021 20:05
react debounce and throttle hooks
import { useEffect, useRef, useState } from "react"
export function useDebouncedValue<T>(
sourceValue: T,
ms: number | undefined
): T {
const [value, setValue] = useState(sourceValue)
useEffect(() => {
if (ms == null) return
@itsMapleLeaf
itsMapleLeaf / range.ts
Created July 23, 2021 15:37
range with a union of tuples for args
type RangeArgs =
| [end: number]
| [start: number, end: number]
| [start: number, end: number, step: number]
function* range(...args: RangeArgs): Generator<number> {
let start: number
let end: number
let step = 1
@itsMapleLeaf
itsMapleLeaf / createReducer.ts
Created July 17, 2021 20:43
create a typesafe reducer with a map of immer action functions
import { Draft, produce } from "immer"
type ValueOf<T> = T[keyof T]
type ActionType<
ActionMap extends Record<string, (state: any, arg: any) => void>
> = ValueOf<
{
[K in keyof ActionMap]: ActionMap[K] extends (
state: any,