重构目前所有的复制颜色代码功能使用统一的Hook。
This commit is contained in:
		@@ -1,8 +1,7 @@
 | 
			
		||||
import cx from 'clsx';
 | 
			
		||||
import { constant, flatten, isEqual, isNil, take, times } from 'lodash-es';
 | 
			
		||||
import { useEffect, useMemo } from 'react';
 | 
			
		||||
import { useCopyToClipboard } from 'react-use';
 | 
			
		||||
import { NotificationType, useNotification } from '../../components/Notifications';
 | 
			
		||||
import { constant, flatten, isEqual, take, times } from 'lodash-es';
 | 
			
		||||
import { useMemo } from 'react';
 | 
			
		||||
import { useCopyColor } from '../../hooks/useCopyColor';
 | 
			
		||||
import { HarmonyColor } from '../../models';
 | 
			
		||||
import styles from './HarmonyPreview.module.css';
 | 
			
		||||
 | 
			
		||||
@@ -11,8 +10,7 @@ type HarmonyPreviewProps = {
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export function HarmonyPreview({ colors = [] }: HarmonyPreviewProps) {
 | 
			
		||||
  const [cpState, copyToClipboard] = useCopyToClipboard();
 | 
			
		||||
  const { showToast } = useNotification();
 | 
			
		||||
  const copyColor = useCopyColor();
 | 
			
		||||
  const extendedColors = useMemo(() => {
 | 
			
		||||
    const sortedColors = colors.sort((a, b) => -(a.ratio - b.ratio));
 | 
			
		||||
    if (sortedColors.length >= 4) {
 | 
			
		||||
@@ -24,19 +22,6 @@ export function HarmonyPreview({ colors = [] }: HarmonyPreviewProps) {
 | 
			
		||||
    ]);
 | 
			
		||||
  }, [colors]);
 | 
			
		||||
 | 
			
		||||
  useEffect(() => {
 | 
			
		||||
    if (!isNil(cpState.error)) {
 | 
			
		||||
      showToast(NotificationType.ERROR, 'Failed to copy to clipboard', 'tabler:alert-circle', 3000);
 | 
			
		||||
    } else if (!isNil(cpState.value)) {
 | 
			
		||||
      showToast(
 | 
			
		||||
        NotificationType.SUCCESS,
 | 
			
		||||
        `${cpState.value} has been copied to clipboard.`,
 | 
			
		||||
        'tabler:circle-check',
 | 
			
		||||
        3000,
 | 
			
		||||
      );
 | 
			
		||||
    }
 | 
			
		||||
  }, [cpState]);
 | 
			
		||||
 | 
			
		||||
  return (
 | 
			
		||||
    <div className={styles.preview}>
 | 
			
		||||
      <h5>Harmony Preview</h5>
 | 
			
		||||
@@ -49,7 +34,7 @@ export function HarmonyPreview({ colors = [] }: HarmonyPreviewProps) {
 | 
			
		||||
            <div className={styles.color_ratio}>{ratio > 0 && `Ratio: ${ratio}`}</div>
 | 
			
		||||
            <div className={styles.color_square} style={{ backgroundColor: `#${color}` }}></div>
 | 
			
		||||
            <div className={styles.color_code}>
 | 
			
		||||
              {ratio > 0 && <span onClick={() => copyToClipboard(`#${color}`)}>#{color}</span>}
 | 
			
		||||
              {ratio > 0 && <span onClick={() => copyColor(color)}>#{color}</span>}
 | 
			
		||||
            </div>
 | 
			
		||||
          </div>
 | 
			
		||||
        ))}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user