Skip to content

Instantly share code, notes, and snippets.

View bryanmylee's full-sized avatar
🔬
Learning new things!

Bryan Lee bryanmylee

🔬
Learning new things!
View GitHub Profile
@bryanmylee
bryanmylee / commitTransaction.ts
Created December 9, 2024 15:56
Commit any set of mutations to PostgreSQL via an RPC as a transaction
import { PostgrestSingleResponse, SupabaseClient } from "@supabase/supabase-js";
// generated from `supabase gen types typescript`
import { Database, TablesInsert, TablesUpdate } from "./types";
export type PublicTableName = keyof Database["public"]["Tables"];
export type TableMutationPatch<TName extends PublicTableName> = Partial<{
inserts: TablesInsert<TName>[];
updates: TablesUpdate<TName>[];
deletes: string[];
@bryanmylee
bryanmylee / fetching-data.tsx
Last active September 28, 2024 06:40
Data loading skeleton best practices
function App() {
const itemId: string | null = /* --snip-- */;
return <Item id={itemId} />;
}
interface ItemProps {
id: string | null;
}
@bryanmylee
bryanmylee / tryResult.ts
Last active November 14, 2024 17:12
Handle errors more declaratively without try catch
/**
* Handle errors more declaratively without try catch.
*
* Non-async functions returning `T` will return `Result<T, unknown>`, and
* async functions returning `Promise<T>` will return
* `Promise<Result<T, unknown>>`.
*
* Supports overloaded function definitions. Refer to
* {@link OverloadedResultFn}.
*
@bryanmylee
bryanmylee / Unmasked.ts
Last active August 3, 2023 05:44
`Unmasked` utility for GraphQL fragments
type Decr = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
type Obj = Record<string, unknown>;
type Prettify<T> = {
[K in keyof T]: T[K];
} & NonNullable<unknown>;
type ValuesOf<T> = T[keyof T];
@bryanmylee
bryanmylee / context.ts
Created April 7, 2023 10:37
Svelte type-safe context
import {getContext, setContext} from 'svelte';
interface Context<TValue> {
get: () => TValue | null;
set: (currentValue: TValue) => void;
}
/**
* Allow strongly typed Svelte context getters and setters while using a unique
* Symbol instead of a string key to avoid any potential key conflicts.
@bryanmylee
bryanmylee / BottomSheet.tsx
Last active March 30, 2024 17:53
Tamagui Bottom Sheet
import {Anchor, Button, H1, Paragraph, Separator, Sheet, Theme, XStack, YStack} from '@slipbox/ui';
import {ChevronDown, ChevronUp} from '@tamagui/lucide-icons';
import {useState} from 'react';
function SheetDemo() {
const [open, setOpen] = useState(false);
const [position, setPosition] = useState(0);
return (
<>
<Button
@bryanmylee
bryanmylee / Podfile
Created July 26, 2022 13:41
React Native 0.69 with Firebase
require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
platform :ios, '12.4'
install! 'cocoapods', :deterministic_uuids => false
production = ENV["PRODUCTION"] == "1"
target 'Wave' do
pod 'Firebase', :modular_headers => true
@bryanmylee
bryanmylee / batch-squoosh.sh
Created June 5, 2022 08:29
Batch compress images with Squoosh
find $1 -name "*.jpg" -exec bash -c $'file="{}"; npx @squoosh/cli --webp \'{"quality":75,"target_size":0,"target_PSNR":0,"method":4,"sns_strength":50,"filter_strength":60,"filter_sharpness":0,"filter_type":1,"partitions":0,"segments":4,"pass":1,"show_compressed":0,"preprocessing":0,"autofilter":0,"partition_limit":0,"alpha_compression":1,"alpha_filtering":1,"alpha_quality":100,"lossless":0,"exact":0,"image_hint":0,"emulate_jpeg_size":0,"thread_level":0,"low_memory":0,"near_lossless":100,"use_delta_palette":0,"use_sharp_yuv":0}\' -d ./output/ "$file"' \;
@bryanmylee
bryanmylee / ReturnTypes.ts
Created April 23, 2022 09:48
Given an array of functions, get the type for an array of the function return types
type ReturnTypes<T extends Array<(...a: any[]) => any>> = {
[P in keyof T]: T[P] extends (...a: any[]) => infer R ? R : never
}
@bryanmylee
bryanmylee / type_factory_method.py
Created March 15, 2022 13:00
Add type hints to a Python factory method
from typing import Type, TypeVar
T = TypeVar('T', bound='TrivialClass')
class TrivialClass:
# ...
@classmethod
def from_int(cls: Type[T], int_arg: int) -> T:
# ...