This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"compilerOptions": { | |
/* Visit https://aka.ms/tsconfig.json to read more about this file */ | |
/* Projects */ | |
"target": "ES2022", | |
"lib": ["ES2022", "DOM", "DOM.Iterable"], | |
"moduleDetection": "force", | |
/* Modules */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { User } from "oidc-client-ts"; | |
const { VITE_OIDC_AUTHORITY, VITE_OIDC_CLIENT_ID } = import.meta.env; | |
const AUTH_KEY = `oidc.user:${VITE_OIDC_AUTHORITY}:${VITE_OIDC_CLIENT_ID}`; | |
// Get the user from local storage (used for API requests where hooks are not available) | |
export const getUser = () => { | |
const storageString = localStorage.getItem(AUTH_KEY); | |
if (!storageString) return null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type RGB = { | |
r: number; | |
g: number; | |
b: number; | |
}; | |
export const hexToRgb = (hex: string): RGB => { | |
const bigint = parseInt(hex.slice(1), 16); | |
return { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function debounce<T extends (...args: Parameters<T>) => ReturnType<T>>( | |
callback: T, | |
wait: number, | |
immediate?: boolean | |
): (...args: Parameters<T>) => void { | |
let timeout: NodeJS.Timeout | undefined; | |
return function (this: ThisParameterType<T>, ...args: Parameters<T>): void { | |
const later = () => { | |
timeout = undefined; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const parseFormData = (formData: FormData) => | |
Array.from(formData.entries()).reduce( | |
(acc, [k, v]) => { | |
if (!acc[k]) { | |
const values = formData.getAll(k); | |
acc[k] = values.length > 1 ? values : v; | |
} | |
return acc; | |
}, | |
{} as Record<string, FormDataEntryValue | FormDataEntryValue[]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const formatBytes = (bytes: number, options?: Intl.NumberFormatOptions) => { | |
/** | |
* Per {@link https://tc39.es/ecma402/#table-sanctioned-single-unit-identifiers}, | |
* these are the valid units for the "unit" style. Since each unit multiple is | |
* considered a separate unit, we have to manually determine the appropriate unit | |
* and corresponding value, otherwise we get formatting such as "1 BB" instead of | |
* "1 GB". | |
*/ | |
const units = ["byte", "kilobyte", "megabyte", "gigabyte", "terabyte", "petabyte"]; | |
if (!bytes || bytes <= 0) return `0 ${units[0]}s`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// when you create ky instance, set onDownloadProgress: progressStore.setSnapshot, | |
import { useSyncExternalStore } from "react"; | |
import type { DownloadProgress } from "ky"; | |
export const shallowCompare = <T extends Record<string, unknown>>(obj1: T, obj2: T) => { | |
if (Object.is(obj1, obj2)) return true; | |
if (!(obj1 instanceof Object) || !(obj2 instanceof Object)) return false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useCallback, useEffect, useRef } from "react"; | |
import { useForm } from "react-hook-form"; | |
import useScript from "../hooks/useScript"; | |
type LocationFormData = { | |
location: string; | |
}; | |
const AutocompleteInput = () => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useCallback, useEffect, useRef } from "react"; | |
import useScript from "../hooks/useScript"; | |
const AutocompleteInput = () => { | |
// Load the Google Maps API script asynchronously | |
const status = useScript( | |
`https://maps.googleapis.com/maps/api/js?key=${ | |
import.meta.env.VITE_GOOGLE_MAPS_API_KEY | |
}&libraries=places` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useState } from "react"; | |
type ScriptStatus = "idle" | "loading" | "ready" | "error"; | |
type ScriptType = HTMLScriptElement | null; | |
function useScript(src: string): ScriptStatus { | |
const [status, setStatus] = useState<ScriptStatus>(src ? "loading" : "idle"); | |
useEffect(() => { | |
// Fetch existing script element by src |
NewerOlder