Skip to content

Instantly share code, notes, and snippets.

View danecando's full-sized avatar
🔨

Dane Grant danecando

🔨
View GitHub Profile
@danecando
danecando / api-client-endpoints.ts
Created October 15, 2023 22:46
Type safety using json schemas from server to client application
import type { RecordResponse, ListResponse, PaginatedListResponse } from "@pkg/core/api";
import { UserMapper, type UserEntity } from "@pkg/core/user";
export type EntityIdResponse = RecordResponse<{ id: string }>;
/**
* start:users endpoints
*/
export type UserEntityResponse = RecordResponse<UserEntity>;
export type UserEntityListResponse = ListResponse<UserEntity>;
@danecando
danecando / tailwind-color-vars.js
Created September 5, 2022 13:09
A simple plugin that exposes all tailwind colors as css variables on :root
function ({ addBase, theme }) {
function extractColorVars(colorObj, colorGroup = '') {
return Object.keys(colorObj).reduce((vars, colorKey) => {
const value = colorObj[colorKey];
const newVars =
typeof value === 'string'
? { [`--color${colorGroup}-${colorKey}`]: value }
: extractColorVars(value, `-${colorKey}`);
@danecando
danecando / query.ts
Last active July 18, 2022 01:04
Meetings layout EdgeDB query experiment
type QueryResult = {
nextMeeting?: Meeting;
scheduledMeetings: {
items: Meeting[];
count: number;
hasMore: boolean;
};
attendedMeetings: {
items: MeetingFields[];
count: number;
@danecando
danecando / useAsyncState.ts
Last active July 14, 2022 14:50
A utility hook for managing async effects in a component
import * as React from 'react';
enum Async {
START,
SUCCESS,
FAILURE,
}
interface AsyncState<T> {
isLoading: boolean;
@danecando
danecando / useCurrentItemScroller.ts
Last active June 23, 2022 00:33
React hook for auto scrolling a list of children that have a aria-current attribute
import type {RefObject} from 'react';
import {useEffect} from 'react';
const defer = (fn: () => void) => setTimeout(fn, 0);
export function useCurrentItemScroller(containerRef: RefObject<HTMLElement>) {
useEffect(() => {
const containerEl = containerRef.current;
let observer: MutationObserver | undefined;
if (containerEl) {
@danecando
danecando / SerializeType.ts
Last active June 7, 2022 11:12
SerializeType utility for converting a type to JSON
// credit @colinhacks
// https://github.com/remix-run/remix/pull/1254
type JSONPrimitives = string | number | boolean | null;
type NonJSONPrimitives = undefined | Function | symbol;
export type SerializeType<T> = T extends JSONPrimitives
? T
: T extends undefined
? undefined
@danecando
danecando / fargate-copilot-deploy.yml
Created May 22, 2022 23:45
GitHub Action: Deploy AWS Fargate app with AWS Copilot
deploy:
name: 🚀 Deploy
runs-on: ubuntu-latest
steps:
- name: 🛑 Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.9.1
- name: ⬇️ Checkout repo
uses: actions/checkout@v3
@danecando
danecando / injection_thread.cpp
Created May 21, 2022 00:13
The first code that I ever open sourced on gamedeception.net
// Some dll injection code
// November 21, 2004
// by SpuN [ http://spun.gamedeception.net ]
// injection_thread.cpp
DWORD WINAPI InjectionThread(LPVOID lpParam)
{
while(1)
{
@danecando
danecando / useFirebaseUploader.ts
Last active July 10, 2022 01:45
React hook for uploading files with Firebase JS client API
import type {
StorageReference,
UploadMetadata,
UploadTask,
} from 'firebase/storage';
import * as React from 'react';
import { ref, uploadBytesResumable, getDownloadURL } from 'firebase/storage';
import { getStorage } from '~/services/firebase';
export enum UploadState {
@danecando
danecando / FormStore.js
Created June 18, 2018 05:29
Mobx store for managing form state (built for my react-native app)
import { reduce, find } from 'lodash';
import { observable, action, computed, extendObservable } from 'mobx';
import Validator from 'validatorjs';
import en from 'validatorjs/src/lang/en';
Validator.setMessages('en', en);
class FormStore {