Move more non-shared components to shlink-web-component

This commit is contained in:
Alejandro Celaya
2023-07-31 20:24:04 +02:00
parent bc11e568b9
commit c73a592fd1
11 changed files with 28 additions and 43 deletions

View File

@@ -1,5 +1,4 @@
import type { DependencyList, EffectCallback } from 'react';
import { useEffect, useRef, useState } from 'react';
import { useRef, useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { v4 as uuid } from 'uuid';
import { parseQuery } from '../../../shlink-web-component/utils/helpers/query';
@@ -27,22 +26,6 @@ export const useTimeoutToggle = (
return [flag, callback];
};
type ToggleResult = [boolean, () => void, () => void, () => void];
export const useToggle = (initialValue = false): ToggleResult => {
const [flag, setFlag] = useState<boolean>(initialValue);
return [flag, () => setFlag(!flag), () => setFlag(true), () => setFlag(false)];
};
export const useEffectExceptFirstTime = (callback: EffectCallback, deps: DependencyList): void => {
const isFirstLoad = useRef(true);
useEffect(() => {
!isFirstLoad.current && callback();
isFirstLoad.current = false;
}, deps);
};
export const useGoBack = () => {
const navigate = useNavigate();
return () => navigate(-1);
@@ -53,6 +36,13 @@ export const useParsedQuery = <T>(): T => {
return parseQuery<T>(search);
};
type ToggleResult = [boolean, () => void, () => void, () => void];
export const useToggle = (initialValue = false): ToggleResult => {
const [flag, setFlag] = useState<boolean>(initialValue);
return [flag, () => setFlag(!flag), () => setFlag(true), () => setFlag(false)];
};
export const useDomId = (): string => {
const { current: id } = useRef(`dom-${uuid()}`);
return id;

View File

@@ -22,7 +22,7 @@ export const determineOrderDir = <T extends string = string>(
return currentOrderDir ? newOrderMap[currentOrderDir] : 'ASC';
};
export const sortList = <List>(list: List[], { field, dir }: Order<Partial<keyof List>>) => (
export const sortList = <List>(list: List[], { field, dir }: Order<keyof List>) => (
!field || !dir ? list : list.sort((a, b) => {
const greaterThan = dir === 'ASC' ? 1 : -1;
const smallerThan = dir === 'ASC' ? -1 : 1;