Skip to content

Instantly share code, notes, and snippets.

View NikaBuligini's full-sized avatar

Nikoloz Buligini NikaBuligini

View GitHub Profile
@NikaBuligini
NikaBuligini / decode.ts
Last active November 21, 2024 14:51
Encryption
// objectKey, encrypted
const decryptKey = await crypto.subtle.importKey(
"jwk",
{
k: objectKey,
alg: "A128GCM",
ext: true,
key_ops: ["decrypt"],
kty: "oct",
import React from 'react';
export function useInterval(callback, delay) {
const savedCallback = React.useRef();
React.useEffect(() => {
savedCallback.current = callback;
}, [callback]);
React.useEffect(() => {
@NikaBuligini
NikaBuligini / useClickOutside.js
Last active January 17, 2020 10:21
useClickOutside hook for react
import React from 'react';
export function useEventListener(eventName, handler, element = document) {
const savedCallback = React.useRef();
React.useEffect(() => {
savedCallback.current = handler;
}, [handler]);
React.useEffect(() => {
@NikaBuligini
NikaBuligini / useDocumentVisibility.js
Last active February 14, 2023 12:30
useDocumentVisibility (react hook)
import React from 'react';
import useEventListener from './useEventListener';
function getVisibilityPropertyNames() {
// Opera 12.10 and Firefox 18 and later support
if (typeof document.hidden !== 'undefined') {
return ['hidden', 'visibilitychange'];
}
export const setCookie = (key, value, exdays) => {
const d = new Date();
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
const expires = `expires=${d.toUTCString()}`;
document.cookie = `${key}=${value};${expires};secure;path=/`;
};
export const getCookie = key => {
const name = `${key}=`;
const decodedCookie = decodeURIComponent(document.cookie);
@NikaBuligini
NikaBuligini / isCrushed.js
Created December 14, 2017 08:31
This piece of code checks if function name has been altered by minification and env is not production
import warning from 'warning';
/*
* This is a dummy function to check if the function name has been altered by minification.
* If the function has been minified and NODE_ENV !== 'production', warn the user.
*/
function isCrushed() {}
if (
process.env.NODE_ENV !== 'production' &&
@NikaBuligini
NikaBuligini / localStorage.js
Last active January 11, 2017 09:04
Simple API for saving and retrieving data from local storage of browser
// returns object which is persisted in local storage
const getState = () => {
try {
const serializedState = localStorage.getItem('state');
if (serializedState === null) {
return undefined;
}
return JSON.parse(serializedState);
} catch (err) {
return undefined;