fix: 修复 ruleStorage 写入失败处理与 CopyButton 成功态样式
This commit is contained in:
@@ -2,6 +2,7 @@ import React, { useEffect, useRef, useState } from 'react';
|
|||||||
import { Check, Copy } from 'lucide-react';
|
import { Check, Copy } from 'lucide-react';
|
||||||
import { copyTextToClipboard } from '@/utils/clipboard';
|
import { copyTextToClipboard } from '@/utils/clipboard';
|
||||||
import { Button, type ButtonProps } from '@/components/ui/button';
|
import { Button, type ButtonProps } from '@/components/ui/button';
|
||||||
|
import { cn } from '@/lib/utils';
|
||||||
import { toast } from 'sonner';
|
import { toast } from 'sonner';
|
||||||
|
|
||||||
interface CopyButtonProps extends Omit<ButtonProps, 'children' | 'onClick'> {
|
interface CopyButtonProps extends Omit<ButtonProps, 'children' | 'onClick'> {
|
||||||
@@ -53,7 +54,7 @@ export const CopyButton: React.FC<CopyButtonProps> = ({
|
|||||||
aria-label={tooltip ?? '复制'}
|
aria-label={tooltip ?? '复制'}
|
||||||
variant={variant}
|
variant={variant}
|
||||||
size={size}
|
size={size}
|
||||||
className={className}
|
className={cn(className, copied && 'text-emerald-500')}
|
||||||
{...props}
|
{...props}
|
||||||
>
|
>
|
||||||
{copied ? (
|
{copied ? (
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ export function save(
|
|||||||
useCount: 0,
|
useCount: 0,
|
||||||
};
|
};
|
||||||
rules.unshift(newRule);
|
rules.unshift(newRule);
|
||||||
setAll(rules);
|
if (!setAll(rules)) return null;
|
||||||
return newRule;
|
return newRule;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +95,7 @@ export function save(
|
|||||||
updatedAt: now,
|
updatedAt: now,
|
||||||
};
|
};
|
||||||
rules[index] = updatedRule;
|
rules[index] = updatedRule;
|
||||||
setAll(rules);
|
if (!setAll(rules)) return null;
|
||||||
return updatedRule;
|
return updatedRule;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +117,7 @@ export function update(id: string, updates: Partial<DataRule>): DataRule | null
|
|||||||
updatedAt: Date.now(),
|
updatedAt: Date.now(),
|
||||||
};
|
};
|
||||||
rules[index] = updatedRule;
|
rules[index] = updatedRule;
|
||||||
setAll(rules);
|
if (!setAll(rules)) return null;
|
||||||
return updatedRule;
|
return updatedRule;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,7 +132,7 @@ export function deleteRule(id: string): boolean {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
rules.splice(index, 1);
|
rules.splice(index, 1);
|
||||||
setAll(rules);
|
if (!setAll(rules)) return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,11 +267,13 @@ export function clear(): void {
|
|||||||
/**
|
/**
|
||||||
* 保存所有规则到存储
|
* 保存所有规则到存储
|
||||||
*/
|
*/
|
||||||
function setAll(rules: DataRule[]): void {
|
function setAll(rules: DataRule[]): boolean {
|
||||||
try {
|
try {
|
||||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(rules));
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(rules));
|
||||||
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[ruleStorage] 保存规则失败:', error);
|
console.error('[ruleStorage] 保存规则失败:', error);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user