Skip to content

Instantly share code, notes, and snippets.

View esamattis's full-sized avatar

Esa-Matti Suuronen esamattis

View GitHub Profile
$ cat Cargo.toml
[package]
name = "rustdepstest"
version = "0.1.0"
edition = "2021"
[dependencies]
swc = "0.264.38"
$ cargo tree
@esamattis
esamattis / ColdWater
Last active February 9, 2023 11:31
Reading Hydrodigit-S1 water meter with iM871A-USB and wmbusmeters to Home Asisstant sensor
# /etc/wmbusmeters.d/ColdWater
name=ColdWater
# the id printed on the meter
id=030xxxxx
key=NOKEY
driver=hydrodigit
@esamattis
esamattis / trpc-test-link.ts
Last active January 12, 2023 08:45
tRPC local / test link for testing
@esamattis
esamattis / Dockerfile
Last active December 21, 2023 20:21
How to deploy a single app to a container from a pnpm monorepo using `pnpm deploy` in Github Actions
FROM node:14-slim
ENV NODE_ENV=production
COPY pnpm-deploy-output /app
WORKDIR /app
ENTRYPOINT ["/app/entrypoint.sh"]
@esamattis
esamattis / scroll-into-view.ts
Created August 24, 2022 21:59
scrollIntoViewIfNeeded() that works with `overflow: scroll` parent divs etc. as well
function getScrollContainer(node: HTMLElement | null): HTMLElement | null {
if (!node) {
return null;
}
if (node.scrollHeight > node.clientHeight) {
if (node === document.body) {
return document.documentElement;
}
return node;
@esamattis
esamattis / view.tsx
Last active June 7, 2022 22:16
Correctly typed "as" prop for React with working refs
interface ViewProps<T> {
as?: T;
ref?: React.Ref<
T extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[T] : T
>;
children?: React.ReactNode;
}
declare function ViewType<T extends ElementType = "div">(
props: React.ComponentPropsWithoutRef<T> & ViewProps<T>,
@esamattis
esamattis / NoHydrate.tsx
Created June 5, 2022 14:54
Prevent React hydration on specific element (HACK)
/**
* Render a div which preserves its server-side content on browser hydration.
*/
function NoHydrate(props: { id?: string; children: React.ReactNode }) {
const id = props.id ?? "no-hydrate";
const container = useRef<HTMLDivElement>(null);
const save = useRef<Node[]>();
// During the first render capture clones of the children. Reading the DOM
// during React render is a hack but the only way to access the DOM before
@esamattis
esamattis / typed-loaders.ts
Created May 19, 2022 20:59
TypeScript inference for Remix.run loaders and actions
import { json } from "@remix-run/node";
import { useActionData, useLoaderData } from "@remix-run/react";
export function useTypedLoaderData<T extends (arg: any) => any>(): Awaited<
ReturnType<T>
> {
return useLoaderData();
}
export function useTypedActionData<T extends (arg: any) => any>():
@esamattis
esamattis / page.tsx
Created May 17, 2022 11:21
Type inference for Remix loaders
import { DataFunctionArgs } from "@remix-run/node";
export async function loader({ request }: DataFunctionArgs) {
const user = getUser(request);
return typedJson({
email: user.email,
});
}
@esamattis
esamattis / launch.json
Last active April 11, 2024 07:06
Debug jest tests in vscode with pnpm
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"runtimeExecutable": "sh", // <-- The important bit!
"program": "node_modules/.bin/jest",
"args": ["${relativeFile}"],