Skip to content

Instantly share code, notes, and snippets.

View ChaseH88's full-sized avatar

Chase Harrison ChaseH88

View GitHub Profile
@ChaseH88
ChaseH88 / useStepWizard.ts
Created October 18, 2023 17:49
React and TypeScript hook that allows you to build out your own step wizard component.
import React, { useState, useCallback, useMemo, ReactNode } from 'react';
interface Step {
component: ReactNode;
name: string;
}
interface StepWizardHook {
currentStep: Step;
next: () => void;
import { useMemo } from 'react'
type ClassValue = string | { [key: string]: boolean } | undefined | null | false
const useClassNames = (...classes: ClassValue[]): string => {
const className = useMemo(
() =>
classes
.filter(Boolean)
.map((classValue) => {
@ChaseH88
ChaseH88 / useScrollToElementOnMount.ts
Created March 6, 2023 02:52
This hook uses useLayoutEffect to scroll to an element with a specific ID extracted from the URL, but only after the content has finished loading.
const useScrollToElementOnMount = (loading: boolean) => {
useLayoutEffect(() => {
const isDirectLink = !document.referrer
const hash = window.location.hash
if (isDirectLink && hash) {
const elementId = hash.substring(1)
const element = document.getElementById(elementId)
if (element && !loading) {
element.scrollIntoView({ behavior: 'smooth' })
}
@ChaseH88
ChaseH88 / chatgpt-voice-playback.js
Created February 20, 2023 18:39
This is a script that can be used directly in the console of ChatGPT to create voice playback buttons so you can hear the responses.
// ==UserScript==
// @name Add Button Before Element - ChatGPT
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Add HTML button before an element on the page.
// @author Chase Harrison
// @match https://chat.openai.com/*
// @grant none
// ==/UserScript==
@ChaseH88
ChaseH88 / sunrise-sunset-api.js
Last active February 14, 2023 14:13
Sunrise/sunset API that provides sunset and sunrise times for a given latitude and longitude.
// https://sunrise-sunset.org/api
let url = `https://api.sunrise-sunset.org/json?lat=${30.6502082}&lng=${-87.9074242}date=today`;
let response = await fetch(url);
let { results } = await response.json();
let final = Object.entries(results).map(([key, val]) => {
let date = new Date();
let time = new Date(new Date(`${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}-${val}`));
time.setHours(time.getHours() - date.getTimezoneOffset()/60);
@ChaseH88
ChaseH88 / useAppState.ts
Created May 10, 2022 08:44
This hook will get you the global app state using Redux
import { useSelector } from 'react-redux';
import { RootState } from '../state';
type States = keyof RootState;
/**
* This hook will get you the global app state
* @returns App State
* @param stateName
*/
@ChaseH88
ChaseH88 / fadeOutRef.ts
Created March 2, 2022 11:49
Fade Out Element - Simply pass a ref into the div. Then onClick, you can utilize this function to fade out the element as it is removed from the DOM.
/**
* Fade out elements as they're deleted
* @param el - the element to focus
* @param speed - the speed of the focus
* @example
const ref = useRef<HTMLDivElement>(null);
...
fadeOut(ref.current, 500)
*/
const fadeOut = (el: HTMLDivElement, speed: number) => {
@ChaseH88
ChaseH88 / presigned-url.ts
Created December 23, 2021 17:37
S3 File Upload with AWS SDK (with custom metadata)
import dotenv from 'dotenv';
dotenv.config();
import type { NextApiRequest, NextApiResponse } from 'next';
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
interface PresignedURLResponse {
success: boolean,
data: string
}
@ChaseH88
ChaseH88 / useRef-example.ts
Created December 17, 2021 16:58
useRef - TypeScript Example
// Main Component
let myRef = useRef<HTMLDivElement>(null);
// Child Component
interface MYINTERFACE {
// ...props
}
const Notifications = React.forwardRef<HTMLDivElement, MYINTERFACE>(({ MYPROPS }, ref) => {
return <div>My Component</div>
@ChaseH88
ChaseH88 / useClickOutside.ts
Created December 17, 2021 13:18
Hook to check if an element is clicked or not clicked.
import { useRef, useCallback, useEffect, MutableRefObject } from 'react';
/**
* Hook to check if an element is clicked or not clicked.
* When the element isn't clicked, a callback function is fired.
*
* @param ref Ref element to listen to
* @param handler Call function that fires when element is outside click area
*/
const useClickOutside = (ref: MutableRefObject<HTMLElement | null>, handler: (e: MouseEvent) => any): void => {