Skip to content

Instantly share code, notes, and snippets.

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

Ameet Madan ameetmadan

🏠
Working from home
View GitHub Profile
@ameetmadan
ameetmadan / useMediaQuery.ts
Created February 5, 2023 18:17
Use Media Query Hook without using a library (supports Breakpoints)
import { useState, useEffect } from "react";
type Breakpoint = "xs" | "sm" | "md" | "lg" | "xl";
function convertQueryToBreakpoint(breakpoint: Breakpoint): string {
switch (breakpoint) {
case "xs":
return "(min-width: 0px)";
case "sm":
return "(min-width: 600px)";
@ameetmadan
ameetmadan / fc.tsx
Last active April 2, 2023 08:52
Create a new React.FC using Live templates for IntelliJ WebStorm
import React, { FC } from "react";
// $ComponentName$ is a variable which can be set to fileNameWithoutExtension()
type $ComponentName$Props = {
};
export const $ComponentName$: FC<$ComponentName$Props> = () => {
return (
@ameetmadan
ameetmadan / swr.tsx
Last active April 2, 2023 08:52
Boilerplate code for SWR hook using IntelliJ WebStorm Live Templates
import axios from "axios";
// define the fetcher once, export it and use it every where you use useSWR
export const fetcher = (url: string) => axios.get(url).then(res => res.data)
const YOUR_API_ENDPOINT = '';
const { data, isLoading, error} = useSWR(`${YOUR_API_ENDPOINT}`, fetcher)
if (isLoading) return <div><p>loading</p></div>
if (error) return <div><p>error</p></div>
@ameetmadan
ameetmadan / Instagram.tsx
Last active April 7, 2023 12:05
Instagram-like grid on your portfolio page with SWR and API routes
import React, { FC } from "react";
import useSWR from "swr";
import Image from "next/image";
type Post = {
media_type: "CAROUSEL_ALBUM" | "IMAGE" | "VIDEO"
permalink: string;
media_url: string;
caption: string;
id: string;
@ameetmadan
ameetmadan / date-diff.ts
Created April 17, 2023 07:44
Clean display of dates in the past
import DateDiff from "date-diff";
export function getFormattedDateDifference(dateAdded: string) {
const diff = new DateDiff(new Date(), new Date(dateAdded))
if (diff.minutes() < 60) {
if (Math.round(diff.minutes()) === 1) {
return `${Math.round(diff.minutes())} minute ago`
}
return `${Math.round(diff.minutes())} minutes ago`
}
if (diff.hours() < 24) {
@ameetmadan
ameetmadan / countries.ts
Created October 3, 2023 21:24
TS country array
export const countries = [
"Afghanistan",
"Åland-Inseln",
"Albanien",
"Algerien",
"Amerikanisch-Samoa",
"Andorra",
"Angola",
"Anguilla",
"Antarktis",
@ameetmadan
ameetmadan / ch-cantons.ts
Created October 3, 2023 21:25
Swiss cantons TS array
export const cantons = [
{title: "Aargau", code: "AG"},
{title: "Appenzell Ausserrhoden", code: "AR"},
{title: "Appenzell Innerrhoden", code: "AI"},
{title: "Basel-Landschaft", code: "BL"},
{title: "Basel-Stadt", code: "BS"},
{title: "Bern", code: "BE"},
{title: "Fribourg", code: "FR"},
{title: "Genève", code: "GE"},
{title: "Glarus", code: "GL"},
@ameetmadan
ameetmadan / pagination.ts
Created January 21, 2024 12:01
Next.js generic paginated response for API routes
import { NextApiResponse } from "next";
export interface PaginationResult<T> {
success: boolean;
data: T[];
pagination: {
page: number;
limit: number;
totalPages: number;
totalCount: number;
import requests
import random
# GitHub Configuration
GITHUB_TOKEN = "YOUR_GITHUB"
REPO_OWNER = ""
REPO_NAME = ""
API_URL = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}"
HEADERS = {"Authorization": f"token {GITHUB_TOKEN}"}
import requests
import webbrowser # Import webbrowser to open links
import time
# GitHub username
USERNAME = ""
# Replace 'None' with your GitHub Personal Access Token
GITHUB_TOKEN= None