Skip to content

Instantly share code, notes, and snippets.

// @ts-expect-error - Quagga is not typed
import Quagga from "quagga";
import { createRef, useCallback, useEffect, useState } from "react";
import { toError } from "@/components/error/errorUtil";
import { Button } from "@/components/style/Button";
export interface BarCodeReaderProps {
onRead: (barcode: string) => void;
}
@ginpei
ginpei / getCpuTemperature.win.js
Last active June 5, 2024 23:48
Get CPU temperature for Windows by Node.js
console.log(Math.round((require('child_process').execSync('wmic /namespace:\\\\root\\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature').toString().split('\r\n')[1].trim()) / 10 - 273)+ '℃')
@ginpei
ginpei / example.tsx
Created August 7, 2023 00:46
History system using React Redux
import { PayloadAction, configureStore, createSlice } from "@reduxjs/toolkit";
import type { NextPage } from "next";
import { Provider, useDispatch, useSelector } from "react-redux";
import undoable, { StateWithHistory, UndoableOptions } from "redux-undo";
// partial state for a section (undo target)
interface NumberState {
id: string;
title: string;
value: number;
import type { NextPage } from "next";
import { useState } from "react";
import { HStack } from "../../src/lib/layout/HStack";
import { VStack } from "../../src/lib/layout/VStack";
import { Button } from "../../src/lib/style/Button";
import { H1 } from "../../src/lib/style/H1";
import { H2 } from "../../src/lib/style/H2";
interface Task {
done: boolean;
@ginpei
ginpei / filter.js
Created December 29, 2022 11:40
Dim off items without keywords in Amazon search result
var words = twotabsearchtextbox.value.split(" ");
[...document.querySelectorAll(".s-result-item")]
.filter((v) => !words.every((w) => v.textContent.toLowerCase().includes(w)))
.forEach((v) => v.style.opacity = "0.3")
@ginpei
ginpei / .js
Last active October 6, 2022 21:18
Measure Express.js middleware runtime performance
const app = express();
const _use = app.use;
let numUseCalls = 0;
app.use = function(...args) {
const fn = args[args.length - 1];
// except `express.static()`
if (fn && fn.name === 'serveStatic') {
_use.apply(this, args);
import { useFocusRing } from "@react-aria/focus";
import { useListBox, useOption } from "@react-aria/listbox";
import { mergeProps } from "@react-aria/utils";
import { ListProps, ListState, useListState } from "@react-stately/list";
import { Node } from "@react-types/shared";
import { useRef } from "react";
interface ListBoxProps<T> extends ListProps<T> {
label?: string;
}
interface FruitMap {
'Apple': AppleFruit;
'Banana': BananaFruit;
}
type FruitName = keyof FruitMap;
type Fruit = FruitMap[FruitName]
interface FruitBase<Type extends FruitName> {
type FruitTemplate<Type extends string, Props> ={ type: Type } & Props
type Fruit =
| AppleFruit
| BananaFruit
type AppleFruit = FruitTemplate<'Apple', {
sliced: boolean;
peeled: boolean;
}>