Skip to content

Instantly share code, notes, and snippets.

View Leechael's full-sized avatar
🐹
Working from home

Leechael Leechael

🐹
Working from home
View GitHub Profile
@Leechael
Leechael / content.md
Created March 4, 2023 16:41
How to Combine Jotai Atom, Next.js Router, and Zod

As a frontend developer, I often work with React and Next.js, and one of the challenges I face is managing query parameters. Next.js provides a powerful router system, but handling query parameters can become cumbersome and error-prone. Fortunately, with the help of Jotai and zod, I've found a solution that allows me to manage query parameters in a reusable and type-safe way.

Jotai is a lightweight state management library that provides a simple and flexible way to manage state in React applications. It represents state as atoms, which are observable and subscribable values. When an atom is updated, any React components that are associated with it will automatically re-render.

Next.js is a React-based lightweight framework that provides a powerful router system, allowing you to handle client-side and server-side routing easily. Using the useRouter hook, you can access the router information, including the URL path and query parameters.

zod is a TypeScript-first data validation library that provides a sim

@Leechael
Leechael / countVisibleObjects.ts
Created April 27, 2022 15:19
Count Visible Objects for Three.js
function countVisibleObjects(scene: THREE.Scene | THREE.Group | THREE.Object3D): number {
return scene.children.reduce(
(acc, obj) => {
if (obj.visible) {
return (obj.children.length) ? acc + countVisibleObjects(obj) + 1 : acc + 1
}
return acc
},
0
)
@Leechael
Leechael / example.ts
Last active February 16, 2021 13:01
one-time restore initial from async func.
import { atom } from "jotai";
import AsyncStorage from "@react-native-community/async-storage";
const jwtTokenAtom = atom<string, string>("", (_, set, value) => {
set(jwtTokenAtom, value);
AsyncStorage.setItem("jwt_token", value);
});
jwtTokenAtom.onMount = (set) => {
@Leechael
Leechael / run_cmd.py
Created October 5, 2019 10:02
Extract from whooey
#!/usr/bin/env python
import os
import sys
import shlex
import subprocess
from threading import Thread
try:
from Queue import Empty, Queue
except ImportError:
@Leechael
Leechael / fs_ossfs.py
Created October 5, 2019 09:13
fs-s3fs for Aliyun OSS
#!/usr/bin/env python
from fs_s3fs import S3FS
import boto3
from botocore.client import Config
class OSSFS(S3FS):
@property
@Leechael
Leechael / parse_lc_logs.py
Last active May 4, 2019 15:50
Parsing exported JSON log from LeanCloud
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" Export logs from leancloud:
`lean logs --format=json --from=2019-03-12 --to=2019-03-13 > 20190312.log`
Then use this script parse the log file.
"""
import { Component } from '@tarojs/taro'
interface RecycleContext {
append(list: any[], callback: () => any)
splice(begin: number, count: number, list: any[], callback: () => any)
update(begin: number, list: any[], callback: () => any)
destroy()
forceUpdate(callback: () => any, reinitSlot: boolean)
getBoundingClientRect(index:number)
@Leechael
Leechael / _.js
Created August 29, 2018 08:03
Resolving Promises Sequentially: Kefir.js version
// https://hackernoon.com/functional-javascript-resolving-promises-sequentially-7aac18c4431e
// Kefir.js version.
// Just a demo.
var ids = [1, 2, 3, 4];
var thenables = ids.map(i => {
let thenable = fetch(`https://httpbin.org/anything?id=${i}`)
.then(r => r.json())
.then(obj => obj.args.id);
return Kefir.fromPromise(thenable);
})
@Leechael
Leechael / set-alicdn-ssl-cert.py
Last active July 10, 2022 10:24
通过脚本设置阿里云 CDN 的证书
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
通过脚本设置阿里云 CDN 的证书
https://help.aliyun.com/document_detail/45014.html
$ pip install aliyun-python-sdk-cdn click
@Leechael
Leechael / Setup Let's Encrypt with Dehydrated.md
Last active March 15, 2018 13:35
Setup Let's Encrypt with Dehydrated

Letsencrypt SSL cert 设置笔记

Step 1: 下载 dehydrated

wget https://raw.githubusercontent.com/lukas2511/dehydrated/116386486b3749e4c5e1b4da35904f30f8b2749b/dehydrated

UPDATE: 下载最新版本:

wget https://raw.githubusercontent.com/lukas2511/dehydrated/master/dehydrated