Skip to content

Instantly share code, notes, and snippets.

View ricardocanelas's full-sized avatar
🦈

Ricardo Canelas ricardocanelas

🦈
View GitHub Profile
@sibelius
sibelius / useStateCallback.tsx
Last active July 29, 2020 13:42
useState that properly handles a function value
export const useStateCallback = (callback: Function) => {
const [value, setValue] = useState(() => callback);
const setCallback = useCallback((fn: Function) => {
setValue(() => fn);
}, [setValue]
return [value, setCallback];
}
@sibelius
sibelius / FeatureFlag.tsx
Created May 6, 2020 12:33
Basic Feature Flag implementation using React.Context
import React, { useContext, useCallback } from 'react';
export const FeatureFlagContext = React.createContext<string[]>([]);
export const useFeatureFlag = () => {
const features = useContext<string[]>(FeatureFlagContext);
const hasFeature = useCallback(
(feature: string) => {
return features.includes(feature);
@sibelius
sibelius / webpack.config.js
Last active July 14, 2020 14:45
run node files using webpack
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const cwd = process.cwd();
export const outputPath = path.join(cwd, '.webpack');
export const outputFilename = 'bundle.js';
export default {
@sibelius
sibelius / useAuth.tsx
Created February 26, 2020 14:46
useAuth hook to handle authentication, use @inline to consume Relay/GraphQL data outside components
import { useEffect } from 'react';
import { graphql, readInlineData } from 'react-relay';
import { useHistory } from '../routing/useHistory';
import { useAuth_user } from './__generated__/useAuth_user.graphql';
const useAuthFragment = graphql`
fragment useAuth_user on User @inline {
id
@sibelius
sibelius / batchCursor.ts
Created February 11, 2020 14:24
Mongo cursor processing - let you select a strategy of how to process elements of a Cursor so you can iterate through all items
/*
* Usage
* let cursor = Test.find().sort({ name: 1 }).cursor();
const get = makeGen(cursor, 100);
let first100 = await get.next();
console.log(first100.value.length);
https://gist.github.com/lineus/3f7d826a21796129db968d6590c93faa
*/
export async function* batchCursor(c, n) {
const cursor = c;
@swyxio
swyxio / cloudos.md
Last active May 3, 2023 12:23
Cloud Operating Systems and Reconstituting the Monolith. tweet responses: https://twitter.com/swyx/status/1226257539886669825?s=20
@diego3g
diego3g / en-validation.json
Created September 30, 2019 18:10
Validation
{
"above": "The {{field}} should be above {{argument.0}}.",
"accepted": "The {{field}} should have been accepted",
"after": "The {{field}} should be a date after {{argument.0}}",
"after_offset_of": "The {{field}} should be after {{argument.0}} {{argument.1}} from today’s date",
"alpha": "The {{field}} should contain letters only",
"alpha_numeric": "The {{field}} should contain letters and numbers only",
"array": "The {{field}} should be an ARRAY.",
"before": "The {{field}} should be a date before {{argument.0}}.",
"before_offset_of": "The {{field}} should be before {{argument.0}} {{argument.1}} from today’s date",
@renanmav
renanmav / environment.js
Created September 20, 2019 20:09
Setup RelayNetworkLogger with relay-runtime v6
import { Network } from 'relay-runtime'
import createRelayNetworkLogger from 'relay-runtime/lib/network/createRelayNetworkLogger'
import RelayNetworkLoggerTransaction from 'relay-runtime/lib/network/RelayNetworkLoggerTransaction'
import cacheHandler from './cacheHandler'
const RelayNetworkLogger = createRelayNetworkLogger(RelayNetworkLoggerTransaction)
const network = Network.create(
@brianleroux
brianleroux / BeforeExitListener.js
Created September 12, 2019 22:04
Lambda NodeJS 10.x Default Runtime JavaScript
/** Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
"use strict";
/**
* The runtime has a single beforeExit function which is stored in the global
* object with a symbol key.
* The symbol is not exported.
* The process.beforeExit listener is setup in index.js along with all other
* top-level process event listeners.
*/
@sibelius
sibelius / useRouter.tsx
Created September 10, 2019 18:03
useRouter hook for react-router-dom
import { useContext } from 'react';
import { __RouterContext, RouteComponentProps } from 'react-router-dom';
export const useRouter = <TParams = {}>() => {
return useContext(__RouterContext) as RouteComponentProps<TParams>;
};