Skip to content

Instantly share code, notes, and snippets.

View csandman's full-sized avatar

Chris Sandvik csandman

View GitHub Profile
csandman /
Last active December 15, 2023 00:17
The priorities of different .env files used in different React scripts

What other .env files can be used?

Note: this feature is available with react-scripts@1.0.0 and higher.

  • .env: Default.
  • .env.local: Local overrides. This file is loaded for all environments except test.
  • .env.development, .env.test, .env.production: Environment-specific settings.
  • .env.development.local, .env.test.local, .env.production.local: Local overrides of environment-specific settings.

Files on the left have more priority than files on the right:

csandman / debounce-swr.js
Last active November 17, 2023 09:12
Debounce useSwr
// originally from:
import useSWR from 'swr';
import useDebounce from 'hooks/use-debounce';
const App = () => {
const [search, setSearch] = useState('');
const debouncedSearch = useDebounce(search, 1000);
const { data: res, isValidating } = useSWR(
() => debouncedSearch ? `/api/suggestion?value=${debouncedSearch}` : null,
csandman /
Last active October 11, 2023 10:23
Chakra UI React Select

Chakra React Select

UPDATE: I finally made an NPM package for this component! It is made with TypeScript, and now has a fully customizable styling system. It is also far ahead of this wrapper at this point. Check it out here if you'd like:

In order to use this component, you can implement it and use it like you would normally use react-select. It should accept all of the props that the original takes, however customizing the theme or the components could break this implementation. There are also a few extra things you can do with this wrapper that pull from the chakra library.

  • You can pass the size prop with either sm, md, or lg. These will reflect the sizes available on the Chakra <Input /> component (with the exception of xs because it's too small to work).
  • In your options objects, you can add the key isFixed to emulate the exa
csandman / path.ts
Created July 20, 2023 04:06
The `Path` TypeScript utility function used in react-hook-form to generate a type for every possible object dot path
/* eslint-disable @typescript-eslint/no-explicit-any */
type Primitive = null | undefined | string | number | boolean | symbol | bigint;
* Checks whether T1 can be exactly (mutually) assigned to T2
* @typeParam T1 - type to check
* @typeParam T2 - type to check against
* ```
* IsEqual<string, string> = true
* IsEqual<'foo', 'foo'> = true
csandman / makemkvcon-usage.txt
Created February 18, 2020 01:25
Documentation for makemkvcon
makemkvcon [options] Command Parameters
General options:
Output all messages to file. Following special file names are recognized:
-stdout - stdout
-stderr - stderr
-null - disable output
Default is stdout
csandman / minimum-contrast.js
Last active May 17, 2022 07:20
Calculate the minimum shade or tint of a color in order to get a W3C compliant contrast between them
function getMinContrastColor(rgb, isLargeFont) {
const minContrastRatio = isLargeFont ? 3.0 : 4.5;
let contrastIsDark = isContrastDark(rgb);
let i = 0.01;
let contrastRgb = [];
let contrastRatio = 0;
while (contrastRatio < minContrastRatio && i < 1) {
contrastRgb = calculateGradient(rgb, contrastIsDark, i);
csandman / itunes.js
Last active February 22, 2022 20:24
Grab the response from the iTunes API for a book search and get the hi-res cover url in the process
import { URLSearchParams } from "url";
* Check whether a URL string is in a proper format
* @param {string} str A URL string to check for the correct structure
* @returns
const isUrl = (str) => {
if (typeof str !== "string") {
csandman / query-string.js
Created April 16, 2021 19:54
Utility functions for parsing a query parameter string and stringify-ing a query parameter object
* Stringify an object containing query parameters to be used in a request
* @param {object} paramObj An object containing query paramaters
* @returns {string} A string in the format of a query param
export const stringifyQuery = (paramObj) =>
new URLSearchParams(paramObj).toString();
// milliseconds in 1 second
export const MS_IN_S = 1000;
// seconds in 1 minute
export const S_IN_M = 60;
// minutes in 1 hour
export const M_IN_H = 60;
// hours in 1 day
export const H_IN_D = 24;
// days in 1 year
export const D_IN_Y = 365;
csandman / compare-emails.sql
Last active March 4, 2021 22:32
Check if 2 emails in match in tsql
-- Written for TSQL
CREATE FUNCTION dbo.CompareEmails(
@Email1 AS NVARCHAR(MAX) = '',
@Email2 AS NVARCHAR(MAX) = ''
-- Basic check to see if each string is an email
IF LEN(@Email1) = 0