Skip to content

Instantly share code, notes, and snippets.

View itsMapleLeaf's full-sized avatar

Darius itsMapleLeaf

View GitHub Profile
@itsMapleLeaf
itsMapleLeaf / example.tsx
Last active February 15, 2023 12:32
SolidStart-style server actions in Remix
function CreateTodoButton() {
const submit = CreateTodoAction.useSubmit()
const isSubmitting = CreateTodoAction.useSubmissions().length > 0
const params = useParams()
return (
<button
type="button"
title="Create Todo"
disabled={isSubmitting}
@itsMapleLeaf
itsMapleLeaf / the.md
Created October 27, 2022 17:10
thoughts on Next.js fetch

I think it's fair to expect divergence with an API that was made for browsers being mimicked elsewhere; here it's more about faithfulness and predictability

node-fetch, cross-fetch, undici fetch, deno fetch, etc. these are all faithful to the original API, and behave in ways that wouldn't surprise you if you're familiar with fetch. the more unfamiliar nooks and crannies of the API (e.g. ReadableStream) is sort of a rats nest unfortunately 😅 but that's beside the point

here's the difference with Next.js: now I'm not writing fetch with fetch in mind, I'm writing fetch with Next.js's caching behaviors in mind, and a hidden fetch call multiple levels down in the stack can unexpectedly change that behavior in potentially unexpected ways

happy to be convinced otherwise though. I know y'all give this a lot of thought, but this was just really out of left field for me 🫠

@itsMapleLeaf
itsMapleLeaf / sanitize-json.ts
Created July 20, 2022 18:54
sanitize json function
type JsonValue =
| string
| number
| boolean
| null
| JsonValue[]
| JsonObject
type JsonObject = { [key: string]: JsonValue }
const { contextBridge } = require('electron')
const versionsGlobal = {
node: () => process.versions.node,
chrome: () => process.versions.chrome,
electron: () => process.versions.electron,
}
contextBridge.exposeInMainWorld('versions', versionsGlobal)
@itsMapleLeaf
itsMapleLeaf / use-timer.ts
Created July 17, 2022 03:20
React useTimer hook
import { useRef, useState } from "react"
import { useEvent } from "./use-event"
export function useTimer(duration: number) {
const [running, setRunning] = useState(false)
const timeoutRef = useRef<ReturnType<typeof setTimeout>>()
const start = useEvent(() => {
clearTimeout(timeoutRef.current)
setRunning(true)
@itsMapleLeaf
itsMapleLeaf / use-event.ts
Created July 17, 2022 03:18
React useEvent hook
import { useCallback, useLayoutEffect, useRef } from "react"
export function useEvent<Args extends unknown[], Return>(
callback: (...args: Args) => Return,
) {
const ref = useRef((...args: Args): Return => {
throw new Error("Attempt to call event callback during render")
})
useLayoutEffect(() => {
@itsMapleLeaf
itsMapleLeaf / example.tsx
Created July 16, 2022 01:10
have the thing exist immediately, but don't side effect on creation
function Example() {
const [store] = useState(() => createStore())
useEffect(() => {
store.connect()
return () => store.disconnect()
}, [store])
// do the thing with store, assuming it updates
}
@itsMapleLeaf
itsMapleLeaf / build.log
Last active July 1, 2022 21:49
bug with nixpacks, pnpm, and engines.node, build log
❯ nixpacks build .
=== Building (nixpacks v0.1.7) ===
=> Packages
-> nodejs-16_x
-> nodePackages.pnpm { nodejs = nodejs-16_x }
=> Install
-> pnpm i --frozen-lockfile
=> Build
-> pnpm run build
=> Start
@itsMapleLeaf
itsMapleLeaf / use-window-event.ts
Created June 26, 2022 22:43
useWindowEvent react hook
import { useEffect } from "react"
export function useWindowEvent<Event extends keyof WindowEventMap>(
event: Event,
handler: (event: WindowEventMap[Event]) => void,
options?: boolean | AddEventListenerOptions,
) {
useEffect(() => {
window.addEventListener(event, handler, options)
return () => {
@itsMapleLeaf
itsMapleLeaf / note-suspense.jsx
Last active May 29, 2022 04:55
suspense for mutation?
import { useState, useTransition } from "react"
export function NoteScreen({ noteId }) {
const [resource, setResource] = useState(() =>
createResource(getNote(noteId)),
)
const note = resource.read()
const [text, setText] = useState(note.text)
const [pending, startTransition] = useTransition()