Skip to content

Instantly share code, notes, and snippets.

@ikibalnyi
ikibalnyi / NonceContext.tsx
Last active December 5, 2023 02:54
Nonce for CSP to overcome RemixJs limitation with getLoadContext not being available in `entry.server.tsx`
import * as React from 'react'
const NonceContext = React.createContext<string | undefined>(undefined)
export const NonceProvider = NonceContext.Provider
export const useNonce = () => React.useContext(NonceContext)
@ikibalnyi
ikibalnyi / karabiner.json
Last active May 2, 2024 18:55
Karabiner config
{
"global": {
"ask_for_confirmation_before_quitting": true,
"check_for_updates_on_startup": true,
"show_in_menu_bar": true,
"show_profile_name_in_menu_bar": false,
"unsafe_ui": false
},
"profiles": [
{
@ikibalnyi
ikibalnyi / useRefProxy.ts
Created July 10, 2020 18:29
Hook to keep persistent reference to a variable
export const useProxyRef = <T extends object>(value: T) => {
const ref = useRef(value);
ref.current = value;
return useMemo(
() =>
new Proxy(
ref.current,
new Proxy(Reflect, {
get(target, p, receiver) {
@ikibalnyi
ikibalnyi / .bashrc
Last active September 4, 2019 22:46
Git and bash aliases
alias gut="git"
alias gti="git"
dockerclean() {
docker rmi --force $(docker images -f 'dangling=true' -q)
}
import React, { Reducer, useEffect, useReducer } from 'react';
import { createAsyncAction } from 'typesafe-actions';
import { ResponseError } from 'utils/api/request';
const initialState = {
isLoading: false,
data: null,
error: null,
};
@ikibalnyi
ikibalnyi / promiseThrottle.js
Last active August 5, 2019 01:33
Simple promise throttler function
const promiseThrottle = (maxParallelCalls = 10) => {
const queued = [];
let parallelCalls = 0;
const abortController = new AbortController();
const execute = () => {
if (!queued.length || parallelCalls >= maxParallelCalls) return;
const { promiseFn, resolve, reject } = queued.shift();
parallelCalls++;
@ikibalnyi
ikibalnyi / prepopulate.js
Last active August 28, 2019 14:05
React HoC for populating component props
import React, { useCallback, useEffect, useState } from 'react';
/**
* HoC for prefetching component data from remote server.
*
* @param fetchData(props) - async function generator for fetching data
* @param getInitialState(props)? - function to get initialState
* @param depsFn(props)? - function to set deps for useEffect
*/
function prepopulate(fetchData, getInitialState, depsFn) {