forked from cvat-ai/cvat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.ts
132 lines (110 loc) · 3.75 KB
/
hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (C) 2021-2022 Intel Corporation
// Copyright (C) 2023 CVAT.ai Corporation
//
// SPDX-License-Identifier: MIT
import _ from 'lodash';
import {
useRef, useEffect, useState, useCallback,
} from 'react';
import { useSelector } from 'react-redux';
import { useHistory } from 'react-router';
import { CombinedState, PluginComponent } from 'reducers';
import { authQuery } from './auth-query';
// eslint-disable-next-line import/prefer-default-export
export function usePrevious<T>(value: T): T | undefined {
const ref = useRef<T>();
useEffect(() => {
ref.current = value;
});
return ref.current;
}
export function useIsMounted(): () => boolean {
const ref = useRef(false);
useEffect(() => {
ref.current = true;
return () => {
ref.current = false;
};
}, []);
return useCallback(() => ref.current, []);
}
export type Plugin = {
component: CallableFunction;
weight: number;
};
export function usePlugins(
getState: (state: CombinedState) => PluginComponent[],
props: object = {}, state: object = {},
): Plugin[] {
const components = useSelector(getState);
const filteredComponents = components.filter((component) => component.data.shouldBeRendered(props, state));
const mappedComponents = filteredComponents
.map(({ component, data }): {
component: CallableFunction;
weight: number;
} => ({
component,
weight: data.weight,
}));
const ref = useRef<Plugin[]>(mappedComponents);
if (!_.isEqual(ref.current, mappedComponents)) {
ref.current = mappedComponents;
}
return ref.current;
}
export function useGoBack(): () => void {
const history = useHistory();
const goBack = useCallback(() => {
if (history.action !== 'POP') {
history.goBack();
} else {
history.push('/');
}
}, []);
return goBack;
}
export interface ICardHeightHOC {
numberOfRows: number;
minHeight: number;
paddings: number;
containerClassName: string;
siblingClassNames: string[];
}
export function useCardHeightHOC(params: ICardHeightHOC): () => string {
const {
numberOfRows, minHeight, paddings, containerClassName, siblingClassNames,
} = params;
return (): string => {
const [height, setHeight] = useState('auto');
useEffect(() => {
const resize = (): void => {
const container = window.document.getElementsByClassName(containerClassName)[0];
const siblings = siblingClassNames.map(
(classname: string): Element | undefined => window.document.getElementsByClassName(classname)[0],
);
if (container) {
const { clientHeight: containerHeight } = container;
const othersHeight = siblings.reduce<number>((acc: number, el: Element | undefined): number => {
if (el) {
return acc + el.clientHeight;
}
return acc;
}, 0);
const cardHeight = (containerHeight - (othersHeight + paddings)) / numberOfRows;
setHeight(`${Math.max(Math.round(cardHeight), minHeight)}px`);
}
};
resize();
window.addEventListener('resize', resize);
return () => {
window.removeEventListener('resize', resize);
};
}, []);
return height;
};
}
export function useAuthQuery(): Record<string, string> | null {
const history = useHistory();
const queryParams = new URLSearchParams(history.location.search);
return authQuery(queryParams);
}