Skip to content

Instantly share code, notes, and snippets.

@ibelick
ibelick / useIpfs.ts
Created March 17, 2022 13:00
A simple React hook to upload a file or data into IPFS
import { create } from "ipfs-http-client";
import { ImportCandidate } from "ipfs-core-types/src/utils";
const client = create({ url: "https://ipfs.infura.io:5001/api/v0" });
const useIpfs = () => {
const upload = async (file: ImportCandidate) => {
try {
const added = await client.add(file);
const url = `https://ipfs.infura.io/ipfs/${added.path}`;
@ibelick
ibelick / dynamicTimeLeft.ts
Created March 24, 2022 08:36
Dynamic JavaScript time counter
export const dynamicTimeLeft = (date1: Date, date2: Date) => {
const distance = date2.getTime() - date1.getTime();
const hours = Math.floor(
(distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((distance % (1000 * 60)) / 1000);
const addZero = (t: number) => (t > 9 ? t : `0${t}`);
@ibelick
ibelick / Timer.jsx
Created March 24, 2022 08:43
Basic Timer in React
import React, { useEffect, useState } from "react";
import { dynamicTimeLeft } from "utils/date";
interface TimerProps {
dateEnd: Date;
}
const Timer: React.FC<TimerProps> = ({ dateEnd }) => {
const [time, setTime] = useState<null | string>(null);
@ibelick
ibelick / compressImage.ts
Created April 7, 2022 09:38
Tiny function in JavaScript/TypeScript to compress image file. Generally use this to precompress a image on the client side before uploading it.
import Compressor from "compressorjs";
export const compressImage = async (
file: File,
quality: number,
maxHeight: number,
maxWidth: number,
convertSize?: number
): Promise<File | Blob> => {
return await new Promise((resolve, reject) => {