Skip to content

Instantly share code, notes, and snippets.

Avatar
🤦‍♂️
why...?

Marais Rossouw maraisr

🤦‍♂️
why...?
View GitHub Profile
@maraisr
maraisr / getDeepByPath.ts
Created Jun 23, 2020
gets an object by path array
View getDeepByPath.ts
const getDeepByPath = <T extends unknown>(obj: T, path: readonly string[]): any => {
let i = 0;
let temp = obj;
for (; i < path.length; ++i) {
temp = temp[path[i]];
}
return temp;
};
View isomorphic-relay-environment.js
import { Environment, Network, RecordSource, Store } from 'relay-runtime';
import RelayPublishQueue from 'relay-runtime/lib/store/RelayPublishQueue.js';
import RelayDefaultHandlerProvider from 'relay-runtime/lib/handlers/RelayDefaultHandlerProvider.js';
const network = Network.create(fetchQuery);
const createEnvironment = (records = {}) => {
const source = new RecordSource(records);
const store = new Store(source);
View batchedMatchMedia.tsx
/* eslint-disable react-hooks/rules-of-hooks */
import { useCallback, useLayoutEffect, useMemo, useState } from 'react';
import { unstable_batchedUpdates } from 'react-dom';
import { useOverdriveContext } from '../components/OverdriveProvider';
import { useTheme } from '../components/ThemeProvider';
export const useMedia = (
queries: ReadonlyArray<keyof Theme['breakpoints']>,
@maraisr
maraisr / useLocalQuery.tsx
Created Dec 16, 2019 — forked from sibelius/useLocalQuery.tsx
useLocalQuery hook to consume local data from a GraphQL Query
View useLocalQuery.tsx
const useLocalQuery = <TQuery extends {response: any; variables: any}>(
environment: Environment,
query: any,
inVariables: TQuery['variables'] = {}
): TQuery['response'] | null => {
const variables = useDeepEqual(inVariables)
const [dataRef, setData] = useRefState<SelectorData | null>(null)
const disposablesRef = useRef<Disposable[]>([])
useEffect(() => {
const {getRequest, createOperationDescriptor} = environment.unstable_internal
@maraisr
maraisr / environment.ts
Created Nov 26, 2019
Next.js + Relay + TypeScript
View environment.ts
import fetch from 'isomorphic-unfetch';
import React, { createContext, useContext, useMemo } from 'react';
import { ReactRelayContext } from 'react-relay';
import {
Environment,
FetchFunction,
Network,
RecordSource,
RequestParameters,
Store,
View useForkedRef.ts
import { MutableRefObject, useMemo } from 'react';
export const useForkedRef = <T = Element>(...refs: Array<MutableRefObject<T>>): (node: T) => void =>
useMemo(() => {
if (refs.every(ref => ref == null)) {
return null;
}
return node => {
refs.forEach(ref => {
View folder-structure.md

Folder Structure

Motivations

  • Clear feature ownership
  • Module usage predictibility (refactoring, maintainence, you know what's shared, what's not, prevents accidental regressions, avoids huge directories of not-actually-reusable modules, etc)
View generate.ts
/* tslint:disable */
import fs from 'fs';
import path from 'path';
import ts from 'typescript';
const aliasWhitelist = ['ResponsiveProp'];
const tsconfigPath = path.join(__dirname, '../tsconfig.json');
const componentsFile = path.join(__dirname, '../lib/components/index.ts');
@maraisr
maraisr / clean.sh
Last active Apr 23, 2019
Clean remote branches except master
View clean.sh
git branch -ar \
| grep origin/ \
| grep -v 'master$' \
| grep -v HEAD \
| cut -d/ -f 2- \
| xargs -I % -n 1 -P 8 sh -c "git push origin :%"
View commonCommands.sh
# 1. redo last command but as root
sudo !!
# 2. open an editor to run a command
ctrl+x+e
# 3. create a super fast ram disk
mkdir -p /mnt/ram
mount -t tmpfs tmpfs /mnt/ram -o size=8192M
You can’t perform that action at this time.