Skip to content

Instantly share code, notes, and snippets.

View dontry's full-sized avatar
🎯
Focusing

Don dontry

🎯
Focusing
View GitHub Profile
@dontry
dontry / dark-mode.js
Created December 3, 2022 11:47
Dark mode query.
var darkQuery = window.matchMedia('(prefers-color-scheme: dark)')
if (!initialTheme) {
initialTheme = darkQuery.matches ? 'dark' : 'light'
}
setTheme(initialTheme)
darkQuery.addEventListener('change', function (e) {
if (!preferredTheme) {
setTheme(e.matches ? 'dark' : 'light')
}
})
@dontry
dontry / immer.ts
Created September 5, 2021 05:52
immer
const keyForState = Symbol('key-for-state');
type state = {
base: any,
copy: any,
}
const createProxy = (value: any) => {
const state: state = {
base: value,
copy: null,
@dontry
dontry / machine.js
Last active June 8, 2021 13:30
Generated by XState Viz: https://xstate.js.org/viz
const searchValid = (context, event) => {
return context.canSearch && event.query && event.query.length > 0;
}
const searchMachine = Machine(
{
id: 'search',
initial: 'idle',
context: {
canSearch: true
var winston = require('winston');
require('winston-daily-rotate-file');
var transport = new (winston.transports.DailyRotateFile)({
filename: 'application-%DATE%.log',
datePattern: 'YYYY-MM-DD-HH',
zippedArchive: true,
maxSize: '20m',
maxFiles: '14d'
});
@dontry
dontry / throttle_debounce.js
Created July 15, 2019 13:59
throttle_debounce
const debounce = (func, delay) => {
let inDebounce
return function() {
const context = this
const args = arguments
clearTimeout(inDebounce)
inDebounce = setTimeout(() => func.apply(context, args), delay)
}
}
import React, { Component } from "react";
import { EditorState, convertToRaw, convertFromRaw } from "draft-js";
import { Editor } from "react-draft-wysiwyg";
import { translate } from "react-translate";
import { connect } from "react-redux";
import { TEXT_CHANGED } from "../../actions";
class TextEditor extends Component {
constructor(props) {
super(props);
@dontry
dontry / file_upload.js
Created May 26, 2019 23:25
Uploading file to S3
import React from "react";
import { connect } from "react-redux";
import S3 from "aws-s3";
import {
DELETE_ATTACHMENT_REQUEST,
GET_ATTACHMENTS_REQUEST,
START_UPLOADING,
UPLOAD_ATTACHMENT_REQUEST
} from "../actions";
import {Icon} from "../../../../layout";
var BROWSER_TYPE = detectBrowserType();
//debounce
function debounce(func, wait, immediate) {
var timeout;
return function () {
var context = this,
args = arguments;
@dontry
dontry / authorizeDecorator
Created April 24, 2019 06:34
decorate factory function
function createAuthorizeDecorator(currentUser){
return function authorize(fn){
return function decorator(...args){
if(currentUser.isAuthenticated()){
return fn.apply(this, args);
} else {
throw "Not authorized to execute " + fn.name + "()";
}
}
}
@dontry
dontry / hook_pagination.js
Last active April 9, 2019 01:39
react_hook_pagination
const usePagination = (fetchApi) => {
const [query, setQuery] = useState({ page: 1, size: 15 }); // 初始页码为: 1
const [isError, setIsError] = useState(false); // 初始状态为false
const [list, setList] = useState([]); // 初始列表数据为空数组: []
useEffect(() => {
setIsError(false);
fetchApi(query)
.then(setList)
.catch(() => setIsError(true));