Skip to content

Instantly share code, notes, and snippets.

@jckw
jckw / saga.ts
Created February 19, 2024 14:38
A simple Saga implementation with TypeScript
class Saga<TContext> {
private operations: Array<{
up: (context: any) => Promise<any>
down: (context: any) => Promise<void>
}> = []
add<TNewContext>(
up: (context: TContext) => Promise<TNewContext>,
down: (context: Partial<TContext & TNewContext>) => Promise<void>
): Saga<TContext & TNewContext> {
@jckw
jckw / main.tf
Created February 10, 2024 16:46
Minimal Terraform config for a Firebase app with Google Identity Platform, Cloud Run, and Secrets Manager
terraform {
required_providers {
google-beta = {
source = "hashicorp/google-beta"
version = "5.15.0"
}
}
}
variable "project_id" {
@jckw
jckw / dict.sh
Last active September 13, 2023 20:38
Dictate to OpenAI's Whisper API from the cmdline
#!/bin/bash
# @author: github.com/jckw
# @dependencies:
# - sox (brew install sox)
# Define constants
TMP_FILE="/tmp/openai_recording.mp3"
OPENAI_KEY="YOUR_OPENAI_API_KEY"
@jckw
jckw / auth.context.ts
Created July 21, 2023 17:52
A simple and good auth context with an xstate state machine
import { useActor, useInterpret } from "@xstate/react"
import { assign, createMachine, InterpreterFrom } from "xstate"
import { createContext, useContext, useEffect, ReactNode } from "react"
import apiClient from "@/api/client"
import { useRouter } from "next/router"
import { useToast } from "@chakra-ui/react"
// Warning, if you are altering this machine, you will probably need to manually refresh
// the page. Because it is defined outside of React, it will not hot reload.
const authMachine = createMachine(
@jckw
jckw / namedCartesian.ts
Created May 2, 2023 22:27
Named Cartesian arrays for use with jest.
import cartesian from "fast-cartesian"
type ArrayToUnion<T extends readonly unknown[]> = T[number]
type ObjectValuesToUnion<T extends Record<string, readonly unknown[]>> = {
[K in keyof T]: ArrayToUnion<T[K]>
}
const namedCartesian = <T extends Record<string, readonly unknown[]>>(
fields: T,
@jckw
jckw / pilot.yml
Created April 17, 2023 15:49
Github Action to create and release iOS and Android internal testing builds / pilots (e.g. TestFlight)
name: Create iOS and Android beta builds
on: [workflow_dispatch]
jobs:
safety-checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node@16
@jckw
jckw / pilot.yml
Created April 17, 2023 15:49
Github Action to create and release iOS and Android internal testing builds / pilots (e.g. TestFlight)
name: Create iOS and Android beta builds
on: [workflow_dispatch]
jobs:
safety-checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node@16
@jckw
jckw / Dockerfile
Last active May 12, 2022 10:13
A minimal Dockerfile for Remix applications that creates a 262MB image!
FROM node:16-bullseye-slim as base
WORKDIR /remix
ENV NODE_ENV production
# 1.1 Install node_modules for build
FROM base as build-deps
WORKDIR /remix
ADD package.json yarn.lock ./
@jckw
jckw / variants.ts
Last active February 23, 2023 18:42
Stitches-inspired variant helper function for Tailwind. Complete with TypeScript autocomplete for variant fields.
type Classnames = string[] | string
type VariantConfig = {
[variant: string]: { [option: string]: Classnames }
}
type Config<V extends VariantConfig> = {
base: Classnames
variants: V
compoundVariants?: ({
@jckw
jckw / utils.ts
Created January 12, 2022 22:43
GraphQL Utils for using Urql SSR with Next.js and getServerSideProps
import {
GetServerSideProps,
GetServerSidePropsContext,
GetServerSidePropsResult,
NextPage,
} from 'next'
import { initUrqlClient, SSRData, withUrqlClient } from 'next-urql'
import {
cacheExchange,
Client,