Skip to content

Instantly share code, notes, and snippets.

function createLogger(namespace) {
return new Proxy(console, {
get(target, key) {
return target[key].bind(target, "\x1b[33m%s\x1b[0m", namespace, "-");
},
});
}
const logger = createLogger('my-namespace')
@HaNdTriX
HaNdTriX / absolutify.js
Created December 1, 2022 16:46
Convert relative paths to absolute paths inside html string
/**
* Convert relative paths to absolute paths
* @author HaNdTriX
* @param {string} html - HTML string
* @param {string} baseUrl - base url to prepend to relative paths
* @param {string[]} [attributes] - attributes to convert
* @returns {string}
*/
function absolutify(
html,
import { useEffect, useRef } from "react";
type ElementPickerProps = {
onPick: (event: MouseEvent, element: HTMLElement) => void;
};
export default function ElementPicker({ onPick }: ElementPickerProps) {
const lastSelectedRef = useRef<HTMLElement>();
const topRef = useRef<HTMLDivElement>(null);
const rightRef = useRef<HTMLDivElement>(null);
@HaNdTriX
HaNdTriX / sha256.js
Last active April 10, 2024 08:08
Convert a string to an sha256 hash in JavaScript
/**
* Convert a string to an sha256 hash
* @param str {string}
* @returns {string}
*/
async function sha256(str) {
const arrayBuffer = new TextEncoder("utf-8").encode(str)
const hashAsArrayBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer);
const uint8ViewOfHash = new Uint8Array(hashAsArrayBuffer);
return Array.from(uint8ViewOfHash).map((b) => b.toString(16).padStart(2, '0')).join('');
@HaNdTriX
HaNdTriX / useRoom.ts
Last active July 14, 2022 00:10
Simple shared conflict free peer to peer state via valtio and y.js.
import * as Y from "yjs";
import { bindProxyAndYMap } from "valtio-yjs";
import { WebrtcProvider } from "y-webrtc";
import { IndexeddbPersistence } from "y-indexeddb";
import { proxy } from "valtio";
import { useMemo, useEffect } from "react";
type WebrtcProviderOptions = ConstructorParameters<typeof WebrtcProvider>[2];
export default function useRoom<T extends Record<string, unknown>>(
// THIS IS JUST A POC. DO NOT USE THIS CODE!
module.exports = function babelPluginGenerateLanguageKatalog(api, options) {
const componentNames = ['FormattedMessage']
const functionNames = ['formatMessage']
const foundIds = new Set()
return {
inherits: require('babel-plugin-syntax-jsx'),
@HaNdTriX
HaNdTriX / StripeTextField.js
Last active November 23, 2021 15:59
Use Stripe and Material-UI
import React, { useImperativeHandle } from "react";
import PropTypes from "prop-types";
import TextField from "@material-ui/core/TextField";
import { fade, useTheme } from "@material-ui/core/styles";
function StripeInput(props) {
const {
component: Component,
inputRef,
/* eslint-disable no-unused-vars */
/**
* Allows to repeatedly call
* an async code block
*
* @callback callback
* @callback [filterError] Allows to differentiate beween different type of errors
* @param {number} [maxRetries=Infinity]
*/
function asyncRetry(
callback,
import React from 'react'
function Error({ statusCode }) {
return (
<>
<h1>{statusCode}</h1>
<h2>This is the Next.js _error.js page</h2>
<p>Choose the status code by changing the url param above</p>
<p>
This page supports the 7.x.x status code range{' '}
@HaNdTriX
HaNdTriX / apollo.logLink.js
Last active April 15, 2020 08:26
Simple logging middleware for Apollo Client.