Skip to content

Instantly share code, notes, and snippets.

View alreadyExisted's full-sized avatar
🐱
Meow

Vitaliy alreadyExisted

🐱
Meow
  • Kharkov, Ukraine
View GitHub Profile
@alreadyExisted
alreadyExisted / selection-text-popover.tsx
Last active October 13, 2020 09:06
react component text selection popover
import { useRef, useCallback, useEffect, useState, CSSProperties } from 'react'
import { createPortal } from 'react-dom'
import styles from './styles.css'
interface Props {
anchorEl?: HTMLElement
onClick?: () => void
}
export function TextSelectionPopover({ anchorEl, onClick }: Props) {
@alreadyExisted
alreadyExisted / react-quill-max-legth.ts
Created August 7, 2020 14:18
React Quill maxLength
const prevText = usePrevious(editorRef.current?.getEditor().getText())
useLayoutEffect(() => {
if (!editorRef.current || !value || !prevText || !maxLength || value.length <= maxLength) return
const editor = editorRef.current.getEditor()
const selection = editor.getSelection() || { index: prevText.length, length: 0 }
editor.setText(prevText, 'user')
setTimeout(() => {
const index = selection.index === prevText.length ? selection.index : selection.index - 1
editor.setSelection(index, selection.length)
}, 16)
import { createSlice, configureStore, combineReducers } from '@reduxjs/toolkit'
enum LoadingType {
Pending = '/pending',
Fulfilled = '/fulfilled',
Rejected = '/rejected',
}
const loadingTypesRegExp = new RegExp(`(${LoadingType.Pending}|${LoadingType.Fulfilled}|${LoadingType.Rejected})$`)
import { configureStore, createAction, createSlice, combineReducers } from '@reduxjs/toolkit'
import { Middleware } from 'redux'
const set = createAction<{ type: string; loading: boolean }>('loadings/set')
const slice = createSlice({
name: 'loadings',
reducers: {},
initialState: {} as Record<string, boolean | undefined>,
extraReducers: builder =>
export function useCurrencyFieldHandleChange<V extends string | undefined, E extends { value: V }>(
fieldValue: V,
onChange: OnChangeFn<E>
) {
const { formatNumber } = useIntl()
const ref = useRef<HTMLInputElement>(null)
const [value, setValue] = useState<number>()
const [selection, setSelection] = useState<[number | null, number | null] | null>(null)
const getDisplayValue = useCallback((_value?: number) => (_value ? formatNumber(_value) : ''), [formatNumber])
const displayValue = useMemo(() => getDisplayValue(value), [getDisplayValue, value])
@alreadyExisted
alreadyExisted / safe-saga.ts
Last active September 10, 2020 03:18
Safe Redux Saga
import { PayloadAction } from '@reduxjs/toolkit'
import { put } from 'redux-saga/effects'
import { ApiError } from '@app/api/errors'
import { createEvent } from '@app/utils/events'
import { eventsActions } from '@app/state/modules/events'
export function safeSaga<T extends PayloadAction<any>>(saga: (action: T) => Generator) {
return function* (action: T) {
try {
yield put({ type: `${action.type}_START`, payload: action.payload })
(function() {
var INTERVAL = 10
// год, месяц (0-11), день
var END_DATE = new Date(2021, 11, 20, 12, 00)
var d, h, m, s, ms
var countdown = document.getElementById('countdown')
function getCountdown() {
var now = new Date().getTime()
var distance = END_DATE - now
@alreadyExisted
alreadyExisted / slugify.ts
Created August 16, 2019 11:40
Convert cyrillic, latin string to slug
const transliterate = (s: string) => {
const map = [
['а', 'a'],
['А', 'A'],
['б', 'b'],
['Б', 'B'],
['в', 'v'],
['В', 'V'],
['г', 'g'],
['Г', 'G'],
class EmailObfuscator {
encodeEmail(email: string) {
const key = this.getRandomCode(33, 330)
let encodedString = key.toString(16)
Array.from(email).forEach(item => encodedString += (item.charCodeAt(0) ^ key).toString(16))
return encodedString
}
import AOS from 'aos'
import { useRouter } from 'client/scenes/@hooks/router'
import { useCallback, useEffect } from 'react'
export function useAnimation() {
const { location: { pathname } } = useRouter()
const initAnimation = useCallback((cb: () => void) => {
const images = Array.from(document.images).filter(item => item.getAttribute('data-aos-img'))
const imgCount = images.length