Skip to content

Instantly share code, notes, and snippets.

View arabold's full-sized avatar
🦀
Making things. Breaking things.

Andre Rabold arabold

🦀
Making things. Breaking things.
View GitHub Profile
@arabold
arabold / action.yml
Created March 10, 2024 13:12
GitHub Action: Increment Version
name: 🔄 Increment Version
description: Increment the repository version number
inputs:
namespace:
description: "Use to create a named sub-version. This value will be prepended to tags created for this version."
required: false
channel:
description: "Denote the channel or pre-release version, i.e. alpha, beta, rc, etc."
required: false
@arabold
arabold / useDataStoreQuery.tsx
Last active February 23, 2023 18:07
Simplify querying the Amplify DataStore using React hooks
import {
DataStore,
PersistentModel,
PersistentModelConstructor,
ProducerModelPredicate,
SortPredicate,
ProducerPaginationInput
} from '@aws-amplify/datastore'
import { useCallback, useEffect, useState } from 'react'
export type SemaphoreFunction = (done: () => void) => void;
/**
* Based on https://www.derpturkey.com/simple-semaphore-in-nodejs/
*/
export default class Semaphore {
readonly max: number;
private _fns: Array<SemaphoreFunction>;
private _active: number;
/**
* Wrap async handler functions for use with `onPress` and similar React callbacks, which are typically
* not safe to use with promises. `useAsyncFunc` gracefully handles any promise errors.
*
* Note that this hook, unlike `useCallback`, does not memoize the callback function itself. To create
* a memoized function wrap it with `useCallback` instead.
* @example
* ```tsx
import Intl from "intl";
import React, { useMemo } from "react";
import useLocalization from "./useLocalization";
export interface NumberFormatProps {
/** The value to format */
value: number;
/** Override system locale (not recommended) */
locale?: string;
import Intl from "intl";
import React, { useMemo } from "react";
import useLocalization from "./useLocalization";
export interface DateTimeFormatProps {
/** The value to format */
value: Date | string | number;
/** Override system locale (not recommended) */
locale?: string;
import { useCallback, useRef, useState } from "react";
export type Dispatch<TState> = (action: ThunkAction<TState>) => Promise<void>
/** A thunk action that can be dispatched by the {@link useThunkReducer}. */
export type ThunkAction<TState> = (
dispatch: Dispatch<TState>,
getState: () => Readonly<TState>,
) => Promise<TState | undefined | void> | TState | undefined | void;
import produce, { Draft } from "immer";
import { useCallback, useRef, useState } from "react";
export type GetStateFunc<T> = () => Readonly<T>;
export type SetStateFunc<T> = (callback: T | ((draft: Draft<T>) => void)) => void;
/**
*
* @param initialValue
*/
@arabold
arabold / Logger.ts
Last active January 23, 2024 00:20
Simple, opinionated Logger for use with Node.js and Web Browsers
/**
* Simple, opinionated Logger for use with Node.js and Web Browsers with no
* additional dependencies.
*
* The logger supports and auto-detects the following environments:
* - Node.js (with and without JSDOM)
* = Browser
* - Electron
* - React Native - Logs to console or browser window depending on whether a debugger is connected
* - Unit Test & CI/CD - Logs are disabled by default
import { Dispatch, useState } from "react";
// Type definitions based on original React `Reducer<S, A>`
export type AsyncReducer<S, A> = (prevState: S, action: A) => Promise<S> | S;
export type AsyncReducerState<R extends AsyncReducer<any, any>> = R extends AsyncReducer<infer S, any> ? S : never;
export type AsyncReducerAction<R extends AsyncReducer<any, any>> = R extends AsyncReducer<any, infer A> ? A : never;
export const useAsyncReducer = <R extends AsyncReducer<any, any>>(
reducer: R,
initialState: AsyncReducerState<R>,