2024-04-02 15:44:09 +08:00
|
|
|
import { ExclamationCircleFilled } from '@ant-design/icons';
|
|
|
|
import { App } from 'antd';
|
2024-02-22 17:14:25 +08:00
|
|
|
import isEqual from 'lodash/isEqual';
|
2024-04-02 15:44:09 +08:00
|
|
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-02-22 17:14:25 +08:00
|
|
|
|
|
|
|
export const useSetModalState = () => {
|
|
|
|
const [visible, setVisible] = useState(false);
|
|
|
|
|
2024-08-14 17:26:47 +08:00
|
|
|
const showModal = useCallback(() => {
|
2024-02-22 17:14:25 +08:00
|
|
|
setVisible(true);
|
2024-08-14 17:26:47 +08:00
|
|
|
}, []);
|
|
|
|
const hideModal = useCallback(() => {
|
2024-02-22 17:14:25 +08:00
|
|
|
setVisible(false);
|
2024-08-14 17:26:47 +08:00
|
|
|
}, []);
|
2024-02-22 17:14:25 +08:00
|
|
|
|
2024-08-14 17:26:47 +08:00
|
|
|
const switchVisible = useCallback(() => {
|
2024-03-15 19:35:59 +08:00
|
|
|
setVisible(!visible);
|
2024-08-14 17:26:47 +08:00
|
|
|
}, [visible]);
|
2024-03-15 19:35:59 +08:00
|
|
|
|
|
|
|
return { visible, showModal, hideModal, switchVisible };
|
2024-02-22 17:14:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const useDeepCompareEffect = (
|
|
|
|
effect: React.EffectCallback,
|
|
|
|
deps: React.DependencyList,
|
|
|
|
) => {
|
|
|
|
const ref = useRef<React.DependencyList>();
|
|
|
|
let callback: ReturnType<React.EffectCallback> = () => {};
|
|
|
|
if (!isEqual(deps, ref.current)) {
|
|
|
|
callback = effect();
|
|
|
|
ref.current = deps;
|
|
|
|
}
|
|
|
|
useEffect(() => {
|
|
|
|
return () => {
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
};
|
2024-02-27 19:05:50 +08:00
|
|
|
|
|
|
|
export interface UseDynamicSVGImportOptions {
|
|
|
|
onCompleted?: (
|
|
|
|
name: string,
|
|
|
|
SvgIcon: React.FC<React.SVGProps<SVGSVGElement>> | undefined,
|
|
|
|
) => void;
|
|
|
|
onError?: (err: Error) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useDynamicSVGImport(
|
|
|
|
name: string,
|
|
|
|
options: UseDynamicSVGImportOptions = {},
|
|
|
|
) {
|
|
|
|
const ImportedIconRef = useRef<React.FC<React.SVGProps<SVGSVGElement>>>();
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [error, setError] = useState<Error>();
|
|
|
|
|
|
|
|
const { onCompleted, onError } = options;
|
|
|
|
useEffect(() => {
|
|
|
|
setLoading(true);
|
|
|
|
const importIcon = async (): Promise<void> => {
|
|
|
|
try {
|
|
|
|
ImportedIconRef.current = (await import(name)).ReactComponent;
|
|
|
|
onCompleted?.(name, ImportedIconRef.current);
|
|
|
|
} catch (err: any) {
|
|
|
|
onError?.(err);
|
|
|
|
setError(err);
|
|
|
|
} finally {
|
|
|
|
setLoading(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
importIcon();
|
|
|
|
}, [name, onCompleted, onError]);
|
|
|
|
|
|
|
|
return { error, loading, SvgIcon: ImportedIconRef.current };
|
|
|
|
}
|
2024-04-02 15:44:09 +08:00
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
onOk?: (...args: any[]) => any;
|
|
|
|
onCancel?: (...args: any[]) => any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useShowDeleteConfirm = () => {
|
|
|
|
const { modal } = App.useApp();
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
const showDeleteConfirm = useCallback(
|
|
|
|
({ onOk, onCancel }: IProps): Promise<number> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
modal.confirm({
|
|
|
|
title: t('common.deleteModalTitle'),
|
|
|
|
icon: <ExclamationCircleFilled />,
|
|
|
|
// content: 'Some descriptions',
|
2024-04-03 11:21:54 +08:00
|
|
|
okText: t('common.ok'),
|
2024-04-02 15:44:09 +08:00
|
|
|
okType: 'danger',
|
2024-04-03 11:21:54 +08:00
|
|
|
cancelText: t('common.cancel'),
|
2024-04-02 15:44:09 +08:00
|
|
|
async onOk() {
|
|
|
|
try {
|
|
|
|
const ret = await onOk?.();
|
|
|
|
resolve(ret);
|
|
|
|
console.info(ret);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onCancel() {
|
|
|
|
onCancel?.();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[t, modal],
|
|
|
|
);
|
|
|
|
|
|
|
|
return showDeleteConfirm;
|
|
|
|
};
|
2024-04-03 11:21:54 +08:00
|
|
|
|
|
|
|
export const useTranslate = (keyPrefix: string) => {
|
|
|
|
return useTranslation('translation', { keyPrefix });
|
|
|
|
};
|
2024-04-07 17:41:29 +08:00
|
|
|
|
|
|
|
export const useCommonTranslation = () => {
|
|
|
|
return useTranslation('translation', { keyPrefix: 'common' });
|
|
|
|
};
|