Skip to content

Instantly share code, notes, and snippets.

View weeksie's full-sized avatar

Scotty Weeks weeksie

View GitHub Profile
@weeksie
weeksie / 01-slice.ts
Last active April 5, 2024 19:22
Better Zustand slice functionality with added bonus of better tracing in redux devtools
import { type StoreApi } from 'zustand';
import { type NamedSet } from 'zustand/middleware';
type FirstSetStateArg<T> = Parameters<StoreApi<T>['setState']>[0];
export type SetState<T> = (partial: FirstSetStateArg<T>, name?: string) => void;
export type GetState<T> = StoreApi<T>['getState'];
type StateObject = Record<string, any>;
type SliceKey<S extends StateObject> = {
@weeksie
weeksie / background.ts
Created July 31, 2023 19:15
Example background script with hand-rolled redux for chrome extensions
import reloadOnUpdate from "virtual:reload-on-update-in-background-script";
reloadOnUpdate("pages/background");
import {
type Store,
type StoreApi,
type Dispatch,
type Listener,
} from '@lib/store';
@weeksie
weeksie / s3uploader.ts
Created November 7, 2022 20:09
A simple
import stream from 'stream';
import {
S3Client,
CreateMultipartUploadCommand,
UploadPartCommand,
CreateMultipartUploadCommandOutput,
CompleteMultipartUploadCommand,
CompletedPart,
DeleteObjectCommand,
import {
Id,
UUID,
Expand,
Require,
} from './types';
import {
isUUID
} from './uuids';
@weeksie
weeksie / Editor.tsx
Last active October 31, 2021 00:31
Using a provider to render content into a sibling or parent
const Editor = () => {
// ...
useTopBarContent(
<ValidationBar />
);
return (
// ...
);
};
@weeksie
weeksie / actions.ts
Last active February 10, 2022 18:22
Some example redux helpers for typescript projects. For illustrative purposes only, but really—you don't need another effing npm package, just write this stuff yourself.
declare global {
/**
extend this where needed
*/
export interface Meta {
session?: string;
receipt?: UUID;
}
}
@weeksie
weeksie / core.effects.js
Last active August 27, 2020 21:36
Redux with custom middleware
import { types, actions } from './actions';
import apolloClient from '../lib/apolloClient';
const client = apolloClient();
const apiQuery = store => next => action => {
next(action);
if (action.type.includes(types.API_QUERY)) {
client.query(action.payload)
@weeksie
weeksie / createActions.js
Created April 27, 2020 16:10
Utility function for creating Redux actions. Used like so:
import { camelCase } from 'camel-case';
export function createActions(actionTypes) {
const types = {};
const actions = {};
actionTypes.forEach(property => {
types[property.replace('/', '_')] = property;
actions[camelCase(property)] = x => ({ type: property, payload: x });
});
@weeksie
weeksie / object-transformers.js
Created March 13, 2019 16:19
Transforming object keys
import { isPlainObject, isArray, camelCase, snakeCase } from 'lodash';
export function snakeKeys(object) {
return deepTransformKeys(object, snakeCase);
}
export function camelKeys(object) {
return deepTransformKeys(object, camelCase);
}
'use strict';
var React = require('react'),
I18N = require('react-intl'),
I18NStore = require('stores/i18n_store'),
_ = require('lodash'),
FormattedMessage = I18N.FormattedMessage;
export default _.assign({
getDefaultProps: function() {
var path = this.displayName.split('.').map(_.snakeCase).join('.');