import React from "react";
function useLocalStorage<T>(key: string) {
const [data, setData] = React.useState<T | null>(() => {
const backup = localStorage.getItem(key);
return typeof backup === "string" ? (JSON.parse(backup) as T) : backup;
});
function updateState() {
This file contains hidden or 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
// App.test.tsx | |
import { describe, it } from "vitest"; | |
import { render, screen } from "@testing-library/react"; | |
import { App } from "./App"; | |
// Comente ou descomente essa linha para ver a diferença entre mockado e não mockado. | |
// vi.mock(import("./useFetch")); | |
This file contains hidden or 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
const squareRoot = (n, g = 1, lg) => g === lg ? g : squareRoot(n, (g + (n / g)) / 2, g); |
This file contains hidden or 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
name: Reusable install deps | |
description: This workflow installs dependencies using PNPM and caches the store directory. It is meant to be used as a reusable workflow. | |
inputs: | |
NODE_VERSION: | |
description: 'The version of Node.js to use' | |
required: true | |
default: '18.x' | |
PNPM_VERSION: | |
description: 'The version of PNPM to use' | |
required: true |
This file contains hidden or 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
// Intersect string with {}: | |
export type StateWords = | 'first' | 'second' | 'third' | string & {} | |
// Record<never, never> can also work: | |
export type StateWords = | 'first' | 'second' | 'third' | string & Record<never, never> | |
// These intersections trick TypeScript into thinking that it is a "different" string type that literal string types are not assignable to (and thus the union won't get reduced to only string). |
This file contains hidden or 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 getCurrentTime = () => { | |
return 1000 * Math.floor(Date.now() / 1000 + 0.1) | |
} | |
export const getNextHourTimestamp = () => { | |
const date = new Date(); | |
const currentHours = date.getHours(); | |
date.setHours(currentHours + 1) | |
date.setMinutes(0) |
This file contains hidden or 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
const memoryCache = new Map<string, any>() | |
/** | |
* `resetCachedFetch` is a function that clears the cachedFetch cache. | |
* It does not take any parameters and does not return any value. | |
*/ | |
export const resetCachedFetch = () => memoryCache.clear() | |
/** | |
* `cachedFetch` is a generic function that performs a fetch operation and caches the result. |
This file contains hidden or 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, useRef, useState } from 'react' | |
const countElementLines = (target: HTMLElement) => { | |
const style = window.getComputedStyle(target, null) | |
const fontSize = parseInt(style.getPropertyValue('font-size')) | |
let height = parseInt(style.getPropertyValue('height')) | |
const boxSizing = style.getPropertyValue('box-sizing') | |
if (boxSizing === 'border-box') { |
export const useDragToScroll = (ref: Ref<HTMLElement | null>) => {
const position = {
left: 0,
x: 0
}
const mouseDown = (event: MouseEvent) => {
if (ref.value === null) return
position.x = event.clientX