Skip to content

Instantly share code, notes, and snippets.

Avatar

Prana Adiwira intrnl

View GitHub Profile
@intrnl
intrnl / mui-rte.tsx
Created Jan 26, 2023
Material UI v4 Rich Text Editor component
View mui-rte.tsx
import React, {type ReactNode} from 'react'
import {type CreateEditorStateProps} from 'remirror'
import {
BoldExtension,
ItalicExtension,
UnderlineExtension,
BulletListExtension,
OrderedListExtension,
HardBreakExtension,
@intrnl
intrnl / lazy-swr.ts
Last active Jan 25, 2023
useLazySWR hook for lazily querying
View lazy-swr.ts
import React from 'react'
import useSWR, { type Fetcher, type Key, type SWRConfiguration, type SWRResponse } from 'swr'
type ArgumentsTuple = [any, ...unknown[]] | readonly [any, ...unknown[]]
type StrictTupleKey = ArgumentsTuple | null | undefined | false
type StrictKey = StrictTupleKey | (() => StrictTupleKey)
type SetKey<T> = React.Dispatch<React.SetStateAction<T>>
export function useLazySWR<
View gql-typegen-addon.ts
/* eslint-env node */
import ts from 'typescript'
import {TypeGenAddonFactory} from 'ts-graphql-plugin'
import {astf} from 'ts-graphql-plugin/lib/ts-ast-util'
const typedQueryDocumentAddon: TypeGenAddonFactory = ({source}) => ({
operationDefinition({tsResultNode, tsVariableNode}) {
const lhs = astf.createIdentifier(`${tsResultNode.name.text}Document`)
const rhs = astf.createTypeReferenceNode(
@intrnl
intrnl / use-search-param.tsx
Last active Jan 14, 2023
React hook for managing search params with serialization (using react-router v6 and serialize-query-params)
View use-search-param.tsx
import React, { type ReactNode } from 'react';
import { useSearchParams } from 'react-router-dom';
import { type QueryParamConfig } from 'serialize-query-params';
export * from 'serialize-query-params';
const SearchParamsContext = React.createContext<ReturnType<typeof useSearchParams> | null>(null);
interface SearchParamsProviderProps {
children?: ReactNode;
View void-mirror.sh
#!/usr/bin/env -S bash
## credits to https://paste.sh/1QS8Tgf6#aHTfRy1dOG4rcA5x_6kauwq3
#pkg=current/xbps-0.59.1_6.x86_64.xbps
pkg=current/dolphin-22.08.2_1.x86_64.xbps
otime=current/otime
file="${XDG_CACHE_HOME:-$HOME/.cache}/${0##*/}-results"
mirrormsg() { printf '%s\n' "getting mirrors from void-docs..." ; }
View attendance-count.js
// This attendance counting function assumes the following:
// - You can attend multiple times in one day (and still be counted as one)
// - Attendance time is not counted, you can attend for several minutes and it still counts as one day
/** @typedef {string | number | Date} DateLike */
/**
* @param {Array<{ start: DateLike, end: DateLike }>} attendances
* @param {Date} startDate
* @param {Date} endDate
View iterable-reader.js
/**
* @typedef {object} Reader
* @property {(p: Uint8Array) => Promise<number | null>} read
*/
/**
* @typedef {object} Seeker
* @property {(offset: number) => Promise<number>} seek
*/
View chunked-iterator.js
/**
* Create an async iterable for a readable stream
* @template T
* @param {ReadableStream<T>} stream
* @returns {AsyncIterableIterator<T>}
*/
function createStreamIterator (stream) {
// return if browser already supports async iterator in stream
if (stream[Symbol.asyncIterator]) {
return stream;
View bytereader.js
function assert (condition, message = 'Assertion failed') {
if (!condition) {
throw new Error(message);
}
}
export class ByteReader {
/**
* @param {ArrayBuffer} buffer
* @param {number} [offset]
View tagged-vanilla-extract.js
import { style } from '@vanilla-extract/css';
const NEW_RULE = /(?:([\u0080-\uFFFF\w-%@]+) *:? *([^{;]+?);|([^;}{]*?) *{)|(}\s*)/g;
const RULE_CLEAN = /\/\*[^]*?\*\/| +/g;
const RULE_NEWLINE = /\n+/g;
const EMPTY = ' ';
/**
* @param {string} val
* @returns {object}