Skip to content

Instantly share code, notes, and snippets.

View KATT's full-sized avatar

Alex / KATT KATT

View GitHub Profile

I've been working with React since 2015 so it's more than "a feeling", so trying to put words on my reasoning below. Obviously open for input and to be proven wrong.

Why colocating code is often preferable over breaking it up into files

Optimize for code deletability confidence in changes

  • Once you remove pages/X.jsx and everything is vendored - you'll automatically delete all the dependencies without leaving any traces
  • With our linting, you will prompted to delete unused code automatically
import { Router } from 'next/router';
import { useEffect } from 'react';
import { publicConfig } from '~/shared/config';
import { trpc } from '~/utils/trpc';
let initVersion = publicConfig.GIT_COMMIT;
function useDetectVersionChange() {
const healthQuery = trpc.health.useQuery(undefined, {
refetchInterval: 10_000,
/**
* @link https://raw.githubusercontent.com/NaturalCycles/js-lib/master/src/promise/pProps.ts
* Promise.all for Object instead of Array.
*
* Inspired by Bluebird Promise.props() and https://github.com/sindresorhus/p-props
*
* Improvements:
*
* - Exported as { promiseProps }, so IDE auto-completion works
@KATT
KATT / trpc.ts
Last active July 14, 2022 04:52
infer UseTRPCQueryOptions
import {
createReactQueryHooks,
TRPCClientErrorLike,
UseTRPCMutationOptions,
UseTRPCQueryOptions,
} from '@trpc/react';
import type { inferProcedureInput, inferProcedureOutput } from '@trpc/server';
import { NextPageContext } from 'next';
// ℹ️ Type-only import:
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export
import { useEffect, useState } from 'react';
/**
* Use the last value that isn't `null` or `undefined`.
* Useful for instance in modals where you don't want the selected item to flicker when the modal is closed.
*/
export function useLastNonNullableValue<T>(currentValue: T) {
const [value, setValue] = useState(currentValue);
useEffect(() => {
setValue((stored) => currentValue ?? stored);
@KATT
KATT / output.ts
Created January 31, 2022 13:50
Output data validation for tRPC
import { z } from 'zod';
import { TRPCError } from '@trpc/server';
/**
* Output parser. Example use-cases:
* - Resolvers where you touch sensitive data that you don't wanna risk leaking
* - Hairy resolvers where it's hard to follow what the resolver returns
* @param schema Zod Schema
* @param output The output
* @returns A parsed output, guaranteed to conform to the schema
@KATT
KATT / ErrorBoundary.tsx
Last active May 12, 2022 21:00
A suspense hook for tRPC
import React, { Component, ErrorInfo, ReactNode } from 'react';
import NextError from 'next/error';
import { TRPCClientErrorLike } from '@trpc/client';
import { AppRouter } from 'server/routers/_app';
interface Props {
children: ReactNode;
}
interface State {
@KATT
KATT / 0-README.md
Last active August 5, 2022 20:48
`useRouterQuery()` hook to get query params on first render
@KATT
KATT / usage.ts
Last active December 12, 2022 18:25
Test helper utility to expect a function or a promise to throw
// usage examples
// callback
const err1 = await waitError(() => { /* do something that throws */ })
// async callback
const err2 = await waitError(async () => { /* do something async that throws */ })
// expect a promise instance to throw
const err3 = await waitError(promise)
@KATT
KATT / main.yml
Last active July 12, 2021 11:04
Github Actions CI: Hasura + Auth0 + Next.js + Cypress + Playwright
name: Cypress
on: [push]
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 20
services: