Skip to content

Instantly share code, notes, and snippets.

View DonHuskini's full-sized avatar
🏠
Working from home

Don Huskini DonHuskini

🏠
Working from home
View GitHub Profile
@DonHuskini
DonHuskini / mock.js
Last active July 19, 2022 15:39
Mock data pattern javascript
class Mock {
users = []
posts = []
constructor() {
const ref = this
this.users = [
{
id: 1,
@DonHuskini
DonHuskini / test.ts
Created July 19, 2022 14:52
my file/folder structure
import { useEffect, useState } from "react";
import axios from "axios";
// |
// |
// |--services (all API fetching goes here)
// |----people.js
// |--hooks (all custom hooks goes here)
// |----useEndpoint.js
// |--pages
@DonHuskini
DonHuskini / snippet.js
Created July 19, 2022 02:48
How to implement debounce
// #1. With a controlled input
import { useState, useEffect } from 'react';
const debounce = require("lodash.debounce");
function SearchBar({ onSearch, wait = 500 }) {
const [value, setValue] = useState('');
const [searchTerm, setSearchTerm] = useState('');
useEffect(() => {
onSearch && onSearch(searchTerm);
@DonHuskini
DonHuskini / gist:5295795289b3ca3972fe524b319dda29
Created June 28, 2022 01:53
Download file nodejs and save with fs + fetch
async function download(url, path) {
const res = await fetch(url);
const fileStream = fs.createWriteStream(path);
await new Promise((resolve, reject) => {
res.body.pipe(fileStream);
res.body.on("error", reject);
fileStream.on("finish", resolve);
});
}
@DonHuskini
DonHuskini / file.tsx
Created June 13, 2022 18:28
Responsive embla carousel with slidesPerView prop without CLS (Comulative Layout Shift)
//---------------
// implementation:
import styled from "styled-components";
import { useCarousel } from "./useCarousel";
type CarouselProps = {
slides: React.ReactNode[];
/** [mobile, tablet, desktop] */
@DonHuskini
DonHuskini / ex.tsx
Created June 9, 2022 15:35
getStaticPaths + getStaticProps with TS
import { ParsedUrlQuery } from 'querystring'
import { GetStaticPaths, GetStaticProps } from 'next'
// getPost(slug: string): Post | undefined
// getAllPostSlugs(): string[]
interface Params extends ParsedUrlQuery {
slug: string
}
@DonHuskini
DonHuskini / hooks.ts
Last active March 24, 2025 12:22
useMediaQuery & useBreakpoints hooks for handling conditional rendering on multiple breakpoints (Adapted for Next.js)
// -------------------------
// useMediaQuery
import { useState, useEffect, useLayoutEffect } from "react";
export function useMediaQuery(query: string) {
const [matches, setMatches] = useState(false);
useEffect(() => {
const media = window.matchMedia(query);
# Command to check git default line ending:
# if true, it will auto-convert line endings. Windows => crlf, Linux/MAC => lf
`git config --global --get core.autocrlf`
# Command to update git default line ending
# will always be the same as in the repo. VSCode is smart enough to understand crlf and lf line endings.
`git config --global core.autocrlf false`
# Add ssh key/passphrase to ssh-agent so I can log into SSH servers without having to type passphrase every time
ssh-add ~/.ssh/id_rsa
@DonHuskini
DonHuskini / BlurredUpImage.jsx
Last active November 17, 2021 15:16
Image Incremental Loading
import styled from '@emotion/styled'
import Image from 'next/image'
import { useProgressiveImage } from 'src/hooks/useProgressiveImage'
export function BlurredUpImage({ tiny, large, width, height, alt }) {
const [src, { blur }] = useProgressiveImage(tiny, large)
return <StyledImage src={src} width={width} height={height} alt={alt} blur={blur} />
}
const StyledImage = styled(Image)`
@DonHuskini
DonHuskini / workaround aspect-ratio safari
Created September 9, 2021 04:07
workaround aspect-ratio safari
import { useRef, useEffect, useState } from "react";
export default function Test() {
const [height, setHeight] = useState(0);
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
if (ref.current) {
setHeight((ref.current.clientWidth * 16) / 9);
}