Skip to content

Instantly share code, notes, and snippets.

@scwood
scwood / useControlledFluentSelection.ts
Last active June 7, 2023 02:22
Using fluent selection in a "controlled" manner
import { IObjectWithKey, Selection, SelectionMode } from '@fluentui/react';
import { useEffect, useRef } from 'react';
/** Options interface for the `useControlledFluentSelection` hook. */
export interface ControlledSelectionOptions<T> {
/** The currently selected items. */
selectedItems: T[];
/** Optional selection mode. */
selectionMode?: SelectionMode;
/** Event handler called when the selected items change. */
@scwood
scwood / tabsExample.tsx
Created January 17, 2023 22:50
Generic tabs component example
import { useState, Children, ReactElement, ReactNode } from "react";
export function App() {
return (
<Tabs>
<Tab name="Division">Division content!</Tab>
<Tab name="Wild card">Wild card content!</Tab>
<Tab name="Conference">Conference content!</Tab>
<Tab name="League">League content!</Tab>
</Tabs>
@scwood
scwood / FloatingRadioBanner.tsx
Created September 29, 2022 06:40
FloatingRadioBanner.tsx
function FloatingRadioBanner() {
const radioMediaType = useState(MediaType.None);
const isLoading = useState(true) // Idk if None really matters
useEffect(() => {
// Setup mixhalo
//
// Set radioMediaType inside ths listeners
//
// Set isLoading to false once you've determined the radioMediaType
@scwood
scwood / contextExample.tsx
Created July 12, 2022 19:25
React context example for sharing audio player controls
// AudioControlsProvider.tsx
import { createContext, useState, ReactNode, useContext } from 'react';
// This is the interface of our context object, i.e. this is the interface of
// stuff that normal components will have access to
export interface AudioControls {
isAudioPlaying: boolean;
pauseAudio: () => void;
}
@scwood
scwood / react-native-track-player-example.tsx
Last active June 22, 2022 18:56
React native track player pseudo code
// useInitializeTrackPlayer.ts
function useInitializeTrackPlayer() {
const [isInitialized, setIsInitialized] = useState(false);
useEffect(() => {
async function initializeTrackPlayer() {
await TrackPlayer.setupPlayer();
TrackPlayer.setOptions(...); // maybe await this? Not sure if it's async
setIsInitialized(true);
@scwood
scwood / Promisfy.ts
Created May 2, 2022 20:26
Strongly typed promisify utility function
type ErrorFirstCallback<D> = (error: any, data: D) => void;
type FunctionThatAcceptsCallback<A extends any[], D> = (
...args: [...A, ErrorFirstCallback<D>]
) => void;
type FunctionThatReturnsPromise<A extends any[], D> = (...args: A) => Promise<D>
function promisify<A extends any[], D>(
originalFunction: FunctionThatAcceptsCallback<A, D>
@scwood
scwood / useVisibilityTracker.ts
Last active January 30, 2022 20:29
React hook to track the visibility of an element
import { Dispatch, SetStateAction, useEffect, useState } from "react";
interface VisibilityTrackerResult {
/** Ref callback, give this to the element to be observed */
ref: Dispatch<SetStateAction<HTMLElement | null>>;
/** Whether or not the observed element is currently visible */
isVisible: boolean;
}
export interface TemperatureUnitToggleProps {
selectedUnit: TemperatureUnit;
onChange: (temperatureUnit: TemperatureUnit) => void;
}
/**
* A toggle component to swap between Celsius and Fahrenheit
*/
export const TemperatureUnitToggle: FunctionComponent<TemperatureUnitToggleProps> = ({ selectedUnit, onChange }) => {
const isCelsiusSelected = selectedUnit === TemperatureUnit.Celsius;
@scwood
scwood / gpob.bash
Created August 24, 2020 18:57
Push to current branch name in the origin
alias gpob='git push -u origin $(getCurrentGitBranch)'
getCurrentGitBranch() {
local branch=$(git rev-parse --abbrev-ref HEAD)
echo $branch;
}
@scwood
scwood / retry.py
Last active September 24, 2019 19:38
Retry decorator in Python
import random
import time
# This is the decorator - it's essentially a function that takes in a function
# as a parameter and returns a modified version of it
def retry(func, max_attempts=4, initial_delay=1):
def wrapper(*args, **kwargs):
delay = initial_delay
attempt = 1
while True: