Skip to content

Instantly share code, notes, and snippets.

View ypresto's full-sized avatar
🏠
Working from home

Yuya Tanaka ypresto

🏠
Working from home
View GitHub Profile
@ypresto
ypresto / usePooledVirtualItemsReact.ts
Last active April 12, 2024 11:30
Reuse rows rendered by @tanstack/virtual to optimize for complicated row component with slow mount (React / Vue)
// Not tested!
import type { VirtualItem, Virtualizer } from '@tanstack/virtual'
import { useEffect, useRef, useState } from 'react'
const DEBUG = false
export interface PooledVirtualItem extends Omit<VirtualItem, 'key'> {
key: number
suspended?: boolean
@ypresto
ypresto / wrap_text.py
Last active December 12, 2023 14:17
Python: BudouXで指定した幅を目安に改行する (改行後が2文字以内にならないよう調整)
import budoux
parser = budoux.load_default_japanese_parser()
def wrap_text(text: str, least_width: int):
phrases = parser.parse(text)
lines = ['']
for phrase in phrases:
if len(lines) > 0 and len(lines[-1]) >= least_width:
@ypresto
ypresto / useHistorySavedState.tsx
Last active February 29, 2024 12:21
React Hooks to save/restore arbitrary object bound to the history stack of Next.js, to restore view state incl. scroll position on go back or forward.
import { HistoryState } from 'next/dist/shared/lib/router/router'
import { useRouter } from 'next/router'
import React, { useContext, useRef } from 'react'
import { useIsomorphicLayoutEffect } from '...' // please place it here yourself or import it from your project.
interface ManagerState {
// matches to history.state
restoredKey?: string
bagForRestore: Record<string, unknown>
@ypresto
ypresto / mf-keyboard-shortcuts.js
Last active April 9, 2022 08:51
Keyboard shortcut snippet for MoneyForward. Paste .js to your dev console (quite insecure, check the code before do it).
function setup() {
const checkboxSelector = '.collective-operation-checkbox:not(#item_bulk_select)';
const globalCheckboxSelector = `.js-block-journalize-account ${checkboxSelector}`;
delegate(document, 'keydown', 'body', e => {
if (e.target.matches('input:not([type="checkbox"]), textarea')) {
return;
}
if (e.target.matches('.js-block-journalize-account') || findParent(e.target, '.js-block-journalize-account')) {
// keydown handler treats this
return;
@ypresto
ypresto / k8s-port-forward.sh
Last active February 2, 2022 06:53
Port forward to somewhere only accessible from Kubernetes cluster
#!/bin/sh -ex
# $1: host
# $2: port
# $3: local port (defaults to $2)
pod_name=bastion-$USER
# extend timeout for fargate env
( sleep 3 && kubectl wait --for=condition=ready --timeout=1m0s pod/$pod_name && echo Start port forwarding && kubectl port-forward pod/$pod_name ${3:-$2}:$2 ) &
@ypresto
ypresto / useFlipCount.ts
Last active August 2, 2023 13:35
Hooks to mitigate calling setState from useEffect/useLayoutEffect. Zero-rerender on hook value change. See useOverrideValue.ts first. https://zenn.dev/ypresto/articles/02f7adcb7c57b4
import { useRef } from 'react'
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
/**
* Hook that its return value increments when the flag is changed to true.
* You can use this with useOverrideValue() / useEffect() to dispatch only when specific condition is met,
* each time or at first (=== 1).
*/
export function useFlipCount(flag: boolean) {
const ref = useRef({ flag, count: 0 })
@ypresto
ypresto / typedStringUtils.ts
Created September 30, 2021 11:28
Typed uppercase/lowercase/capitalize/uncapitalize function for use with template literal types.
/**
* For Template Literal Types. Don't forget to add 'as const' after `...` .
*/
export function typedUppercase<S extends string>(str: S) {
return str.toUpperCase() as Uppercase<S>
}
/**
* For Template Literal Types. Don't forget to add 'as const' after `...` .
*/
@ypresto
ypresto / copy-uuid.sh
Last active May 31, 2021 09:01
[macOS] copy-uuid: Copy UUID to clipboard!
# Copy this to your .***shrc
alias copy-uuid='uuidgen | tr "[:upper:]" "[:lower:]" | tr -d "\n" | pbcopy'
@ypresto
ypresto / .zshrc
Created May 31, 2020 18:29
Fix "SSL certificate problem: certificate has expired" issue in homebrew
# Add this snippet to your .zshrc, .bashrc or etc.
# https://security.stackexchange.com/a/232446/235706
export CURL_SSL_BACKEND=secure-transport
# https://github.com/Homebrew/brew/issues/6274#issuecomment-507937736
export HOMEBREW_NO_ENV_FILTERING=1
@ypresto
ypresto / docker-compose-mutagen.yml
Last active March 25, 2021 18:49
Use mutagen with docker-sync like setup.
version: "3.7"
services:
service1:
volumes:
- your-app-mutagen-service1:/var/app:nocopy
service2:
volumes:
- your-app-mutagen-service2:/var/app:nocopy