Skip to content

Instantly share code, notes, and snippets.

View Dearest's full-sized avatar

Dearest Dearest

View GitHub Profile
import { useState, useEffect, useRef } from 'react';
import ResizeObserver from '@/modules/resize-observer';
export default function useElementSize(element) {
const targetRef = useRef();
const [state, setState] = useState({ width: undefined, height: undefined });
useEffect(() => {
if (typeof element === 'function') {
targetRef.current = element();
import { useState, useEffect, useCallback } from 'react';
import useThrottle from './use-throttle';
import { useGlobalEventListener } from './use-event-listener';
function getRect(element) {
if (element) return element.getBoundingClientRect();
return {
width: 0,
height: 0,
@Dearest
Dearest / association_loader.rb
Created June 17, 2021 08:28
AssociationLoader
class AssociationLoader < GraphQL::Batch::Loader
def self.validate(model, association_name)
new(model, association_name)
nil
end
def initialize(model, association_name, preload_scope = nil)
@model = model
@association_name = association_name
@reflection = model.reflect_on_association(association_name)
@Dearest
Dearest / skeleton_screen_loading.html
Last active August 8, 2020 03:56
简单的骨架屏 loading动画, 关键代码 background-size, background-position 参考https://juejin.im/post/6844904186832748557
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
display: flex;
justify-content: center;
@Dearest
Dearest / GitCommitEmoji.md
Created February 3, 2020 01:30 — forked from parmentf/GitCommitEmoji.md
Git Commit message Emoji
@Dearest
Dearest / retry.rb
Created January 4, 2019 06:28
ruby rescue retry的简洁写法
def self.set_flag( user_id, flag )
# Making sure we only retry 2 times
tries ||= 2
flag = UserResourceFlag.where( :user_id => user_id , :flag => flag).first_or_create!
rescue ActiveRecord::RecordNotUnique => e
Rollbar.error(e)
retry unless (tries -= 1).zero?
end
@Dearest
Dearest / filter_by_permission_query.rb
Created November 8, 2018 03:08
字符串数组里是否包含查询值
def apply(scope, permission)
scope.where('all_permissions = true OR permissions @> ARRAY[?]::varchar[]', permission)
end
@Dearest
Dearest / unicode_to_utf8
Created September 6, 2018 10:01
ruby unicode 转中文
require 'cgi'
def unicode_utf8(unicode_string)
unicode_string.gsub(/\\u\w{4}/) do |s|
str = s.sub(/\\u/, "").hex.to_s(2)
if str.length < 8
CGI.unescape(str.to_i(2).to_s(16).insert(0, "%"))
else
arr = str.reverse.scan(/\w{0,6}/).reverse.select{|a| a != ""}.map{|b| b.reverse}
hex = lambda do |s|
(arr.first == s ? "1" * arr.length + "0" * (8 - arr.length - s.length) + s : "10" + s).to_i(2).to_s(16).insert(0, "%")
@Dearest
Dearest / city.rb
Created August 7, 2018 06:48
memoization a method with parameters
class City < ActiveRecord::Base
def self.top_cities(order_by)
@top_cities ||= Hash.new do |h, key|
h[key] = where(top_city: true).order(key).to_a
end
@top_cities[order_by]
end
end
@Dearest
Dearest / routes.rb
Created May 18, 2018 02:25
rails 拆分路由
Rails.application.routes.draw do
root 'application#index'
def draw(routes_name)
instance_eval(File.read(Rails.root.join("config", "routes", "#{routes_name}.rb")))
end
# v1 接口
draw :v1