Skip to content

Instantly share code, notes, and snippets.

View danieljpgo's full-sized avatar
🚀
raising the bar

Daniel Jorge danieljpgo

🚀
raising the bar
View GitHub Profile
export const useDebounce = (value, delay) => {
const [debouncedValue, setDebouncedValue] = React.useState(value);
React.useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
return () => {
clearTimeout(handler);
@danieljpgo
danieljpgo / breakpoints.ts
Last active August 5, 2021 15:16
Theme file based on design tokens
export const breakpoints = { // adicionar outros breakpoints se estiver espeficificado no design
'2xs': '360px',
xs: '480px',
sm: '640px',
md: '768px',
lg: '1024px',
xl: '1280px',
'2xl': '1536px',
} as const;
import { useLocation } from 'react-router-dom';
export default function useRouterQuery() {
const { search } = useLocation();
return new URLSearchParams(search);
}
import * as React from 'react';
import PropTypes from 'prop-types';
function MainPanel({ children }) {
return <section className="flex-grow">{children}</section>;
}
function SidePanel({ children }) {
return <aside className="flex-grow max-w-none md:max-w-75">{children}</aside>;
}
@danieljpgo
danieljpgo / localStorage.ts
Last active February 22, 2022 21:13
/lib
/**
* Returns the current object from `localStorage` associated with the given `key`.
*/
export function getLocalStorageData<T = unknown>(key: string): T | undefined {
const data = window.localStorage.getItem(key);
try {
return data ? JSON.parse(data) : undefined;
} catch {
return undefined;
}
import React from 'react';
import PropTypes from 'prop-types';
import { useLocation, Route } from 'react-router';
export default function DynamicRoute({ children, path, element }) {
const location = useLocation();
return (
<Route path={path} element={element(location)}>
{children}
import * as React from 'react';
import PropTypes from 'prop-types';
export default function NavigateOutside({ url }) {
React.useLayoutEffect(() => {
window.location.replace(url);
});
return null;
}
import axios from 'axios';
import { env } from '../lib/env';
import { request, response } from './interceptor';
const instance = axios.create({
baseURL: env.apiBaseUrl,
});
async function get<Data>(endpoint: string): Promise<Data> {
const { data } = await instance.get(endpoint);
/**
* Returns the current object from local storage associated with the given key.
* @template T
* @param {String} key The key to set in localStorage for this value
* @returns {T|undefined}
*/
export function getLocalStorageData(key) {
const data = window.localStorage.getItem(key);
try {
return data ? JSON.parse(data) : undefined;
import * as React from 'react';
type ErrorBoundaryState = {
error?: Error;
};
type ErrorBoundaryProps = {
children: React.ReactNode;
fallback: React.ReactNode | ((state: ErrorBoundaryState) => void);
};