This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { FunctionComponent, PropsWithChildren } from 'react' | |
export const DirectionalBlur: FunctionComponent< | |
PropsWithChildren<{ | |
spread?: number | |
blur?: number | |
rotationDeg?: number | |
}> | |
> = ({ children, spread = 100, blur = 0.5, rotationDeg = 0 }) => { | |
return ( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Utility type to make complex types more readable in tooltips and error messages. | |
*/ | |
type Prettify<T> = { | |
[K in keyof T]: T[K]; | |
} & {}; | |
type ObjectEntry<T extends object> = Prettify<{ [K in keyof T]: [K, T[K]] }[keyof T]>; | |
type FromEntries<T extends Array<[PropertyKey, unknown]>> = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function opRead(path: `op://${string}/${string}/${string}`) { | |
return await new Response(Bun.spawn(["op", "read", path]).stdout).text(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
export function Join(props: { items: Array<React.ReactNode>; glue?: React.ReactNode; lastGlue?: React.ReactNode }) { | |
const glue = props.glue ?? ', ' | |
const last = props.lastGlue ?? glue | |
return ( | |
<> | |
{props.items.map((item, i, list) => ( | |
<React.Fragment key={i}> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function loadPaginated<R>(loader: (page: number) => Promise<R[]>) { | |
const results: R[] = [] | |
while (true) { | |
const found = await loader(i) | |
results.push(...found) | |
if (!found.length) { | |
break | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function useSwitchingInterval<Item>(items: readonly Item[], seconds = 2) { | |
const [active, setActive] = useState(0) | |
useEffect(() => { | |
let timeout: null | NodeJS.Timeout = null | |
const next = () => { | |
setActive((a) => a + 1) | |
timeout = setTimeout(next, seconds * 1000) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare namespace React { | |
interface CSSProperties { | |
[key: `--${string}`]: string | number | null | undefined | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function memo<Value>( | |
key: string, | |
loader: () => Value | Promise<Value> | |
) { | |
const g = global as unknown as { | |
memory: Record<string, { value: Value | Promise<Value>; timestamp: number }> | |
} | |
const memory = g.memory ?? {} | |
g.memory = memory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ArrayifyFields<Original> = Original extends Record<string, unknown> | |
? { | |
[key in keyof Original]: Original[key][] | |
} | |
: never | |
export function arrayifyFields<Item extends Record<string, unknown>>(items: Item[]) { | |
return items.reduce((grouped, item) => { | |
const entries = Object.entries(item).map( | |
([key, value]) => [key, grouped ? [grouped[key], value] : [value]] as const, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare module 'react' { | |
interface CSSProperties { | |
[key: `--${string}`]?: string | number | undefined | |
} | |
} |
NewerOlder