feat: 组件重构
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import { useState } from 'react';
|
||||
|
||||
export function useStarToggle() {
|
||||
const [updatingStars, setUpdatingStars] = useState<Set<string>>(new Set());
|
||||
|
||||
const toggleStar = async (recordId: string) => {
|
||||
if (updatingStars.has(recordId)) return null;
|
||||
|
||||
try {
|
||||
setUpdatingStars(prev => new Set(prev).add(recordId));
|
||||
const response = await fetch(`/api/records/${recordId}/star`, {
|
||||
method: 'PATCH'
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error('切换星标状态失败');
|
||||
const data = await response.json();
|
||||
return data.isStarred as boolean;
|
||||
} catch (error) {
|
||||
console.error('切换星标状态失败:', error);
|
||||
return null;
|
||||
} finally {
|
||||
setUpdatingStars(prev => {
|
||||
const newSet = new Set(prev);
|
||||
newSet.delete(recordId);
|
||||
return newSet;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return { updatingStars, toggleStar };
|
||||
}
|
||||
Reference in New Issue
Block a user