Skip to content

Instantly share code, notes, and snippets.

View srph's full-sized avatar
🛠️
Building @Stride-Labs Frontend

Kier Borromeo srph

🛠️
Building @Stride-Labs Frontend
View GitHub Profile
@srph
srph / readme.md
Last active May 13, 2022 02:18
React: axios useInterceptor hook

Usage

import React from 'react'
import { useAuthUser } from '~/src/contexts/AuthUser'
import { useInterceptor } from './useInterceptor'

/**
 * Setup axios interceptors to work with auth
 */
@srph
srph / mergeRefs.ts
Created May 10, 2022 13:15
React: merge refs
export function mergeRefs<T>(...refs: Ref<T>[]) {
return (value: T) => {
refs.forEach((ref) => {
if (typeof ref === 'function') {
ref(value)
} else if (ref.current) {
ref.current = value
}
})
}
@srph
srph / Popover.tsx
Last active May 10, 2022 15:47
React: Popover - Fully wrapped Popper component
import React, { useMemo } from 'react'
import ReactDOM from 'react-dom'
import styled from 'styled-components'
import { usePopper } from 'react-popper'
import type { Placement, Offsets } from '@popperjs/core'
import { useStateRef, useUpdateEffect, useDocumentListener, useOutsideClick } from '~/src/hooks'
import { theme } from '~/src/theme'
interface Props {
@srph
srph / index.ts
Created January 10, 2022 00:56
JS: Merge sort (post-holidays exercise)
const merge = (left: number[], right: number[]): number[] => {
let result = []
let lindex = 0
let rindex = 0
while (lindex < left.length && rindex < right.length) {
if (left[lindex] < right[rindex]) {
result.push(left[lindex])
lindex++
} else {
@srph
srph / index.js
Created September 14, 2021 12:07
JS: Select and invite all friends
// You can manually select this by:
// Console -> Find Modal -> Right Click -> Store as Global Variable
const modal = document.querySelector('#mount_0_0_5Q > div > div:nth-child(1) > div > div:nth-child(7) > div > div > div.rq0escxv.l9j0dhe7.du4w35lb > div > div.iqfcb0g7.tojvnm2t.a6sixzi8.k5wvi7nf.q3lfd5jv.pk4s997a.bipmatt0.cebpdrjk.qowsmv63.owwhemhu.dp1hu0rb.dhp61c6y.l9j0dhe7.iyyx5f41.a8s20v7p > div > div > div > div.rdhft8ur.a31nu0t8.r8a9u66l.ni8dbmo4.stjgntxs.k4urcfbm > div.f71di6ws.cxgpxx05.rz4wbd8a.sj5x9vvc.a8nywdso > div > div.j83agx80.cbu4d94t.buofh1pr.l9j0dhe7')
modal.querySelectorAll('.hu5pjgll.m6k467ps').forEach(element => {
var evt = new Event('click', { bubbles: true })
element.dispatchEvent(evt)
})
@srph
srph / dupes.js
Last active April 23, 2021 11:45
JS: Get duplicate values in an array of objects
const dupes = (arr, key) => {
const map = {}
const result = []
arr.forEach((obj) => {
const value = obj[key]
if (map[value]) result.push(obj)
map[value] = true
})
@srph
srph / index.ts
Last active April 22, 2021 11:35
Shogun Frontend: Signals
import React, { memo, MouseEvent, RefObject, useEffect, useMemo } from 'react'
import { useRef } from 'react'
//#region utils
/**
* We need a notion of reactivity. React is not reactive.
*/
interface Signal<A> {
(handler: (a: A) => void): () => void
}
function newSignal<A>(): [signal: Signal<A>, next: (value: A) => void] {
@srph
srph / index.js
Last active December 14, 2020 09:05
JS: Generate a random string from a given characterset using only each character once
function generateUnique(length) {
const generated = []
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
for (let i = 0; i < length; i++) {
let character = ''
do {
character = characters[random(characters.length)]
} while (generated.includes(character))
@srph
srph / index.js
Created August 18, 2020 12:47
JS: Get only a number twice
var integers = []
// Generate 10 integers
for (var i = 0; i < 10; i++) {
var number
do {
// Keep generating a random number if it's already in the array twice.
number = random(5)
} while(getCountInList(number) >= 2)
@srph
srph / index.js
Created July 22, 2020 10:22
JS: Get the prior status of the list based on the order of the provided statuses.
/**
* Get the prior status of the list based on the order of the provided statuses.
*
* @example
* getPriorStatus([{ status: 'active' }, { status: 'pending' }, { status: 'approved' }], ['approved', 'active', 'declined', 'pending'])
* @returns
* approved
*/
const getPriorStatus = (data, statuses) => {
for (let i = 0; i < statuses.length; i++) {