diff --git a/src/components/ui/TagManageWidget.tsx b/src/components/ui/TagManageWidget.tsx index c3b52a3..15d6d65 100644 --- a/src/components/ui/TagManageWidget.tsx +++ b/src/components/ui/TagManageWidget.tsx @@ -20,6 +20,9 @@ import { endpoints } from '@/config/api'; import { toast } from 'react-toastify'; import LoadingSpinner from './LoadingSpinner'; import sleep from '@/utils/sleep'; +import { MAX_CHART_TAGS, saveChartTags } from '@/utils/chartTags'; +import { ApiError } from '@/utils/apiClient'; +import { CODE_TOO_MANY_ELEMENTS } from '@/config/apiRetCode'; import { motion } from 'framer-motion'; import type { TagManageWidgetProps, @@ -132,7 +135,7 @@ function tagsReducer(state: string[], action: TagsAction): string[] { case 'INIT_TAGS': return action.payload; case 'ADD_TAG': - return [...state, action.payload]; + return state.includes(action.payload) ? state : [...state, action.payload]; case 'REMOVE_TAG': { const newTags = [...state]; newTags.splice(action.payload, 1); @@ -159,6 +162,8 @@ const TagManageWindow = forwardRef( const [tags, dispatch] = useReducer(tagsReducer, []); const isInPrivatePage = window.location.pathname === '/user/charts'; const initializedRef = useRef(false); + /** Tags as last read from or saved to the server: the base of the public-tag PATCH diff. */ + const serverTagsRef = useRef([]); useEffect(() => { const handleClickOutside = (e: MouseEvent) => { @@ -220,7 +225,7 @@ const TagManageWindow = forwardRef( const fetcher = (url: string) => fetch(url, { mode: 'cors', credentials: 'include' }).then((res) => res.json()); - const { data, error, isLoading } = useSWR( + const { data, error, isLoading, mutate } = useSWR( endpoints.maichart.summary(songid), fetcher ); @@ -229,6 +234,7 @@ const TagManageWindow = forwardRef( if (data && !initializedRef.current) { if (isInPrivatePage) { if (data.tags !== undefined) { + serverTagsRef.current = data.tags; dispatch({ type: 'INIT_TAGS', payload: data.tags }); initializedRef.current = true; } else { @@ -236,6 +242,7 @@ const TagManageWindow = forwardRef( } } else { if (data.publicTags !== undefined) { + serverTagsRef.current = data.publicTags; dispatch({ type: 'INIT_TAGS', payload: data.publicTags }); initializedRef.current = true; } else { @@ -249,31 +256,43 @@ const TagManageWindow = forwardRef( const uploading = toast.loading(t("shared/TagManageWidget.Uploading"), { hideProgressBar: true, }); - const response = await fetch( - isInPrivatePage ? endpoints.maichart.tags(songid) : endpoints.maichart.publictags(songid), - { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(tags), - credentials: 'include', + let saved: string[]; + try { + saved = await saveChartTags(songid, isInPrivatePage ? 'private' : 'public', serverTagsRef.current, tags); + } catch (error) { + if (error instanceof ApiError && error.code === CODE_TOO_MANY_ELEMENTS) { + toast.error(t("shared/TagManageWidget.TooManyTags", { max: MAX_CHART_TAGS })); + } else if (error instanceof ApiError && error.status === 409) { + toast.error(t("shared/TagManageWidget.TagsChanged")); + } else { + toast.error('好像上传失败惹……'); } - ); - toast.done(uploading); + return; + } finally { + toast.done(uploading); + } - if (!response.ok) { - toast.error('好像上传失败惹……'); - } else { - //TODO: 刷新页面,重载数据,应该可以热重载,之后研究 - toast.success('上传成功了喵'); - if (window.location.pathname !== '/user/charts') { - await sleep(1000); - window.location.reload(); - } + // The server normalizes tags and PATCH merges other players' edits, so show what it now holds. + serverTagsRef.current = saved; + dispatch({ type: 'SET_TAGS', payload: saved }); + await mutate((summary) => summary && { ...summary, [isInPrivatePage ? 'tags' : 'publicTags']: saved }, { revalidate: false }); + toast.success('上传成功了喵'); + //TODO: 刷新页面,重载数据,应该可以热重载,之后研究 + if (window.location.pathname !== '/user/charts') { + await sleep(1000); + window.location.reload(); } + }; - return response; + /** Adds a tag unless it is already there; refuses one past the backend's per-kind limit. */ + const addTag = (tag: string) => { + if (tags.includes(tag)) return true; + if (tags.length >= MAX_CHART_TAGS) { + toast.error(t("shared/TagManageWidget.TooManyTags", { max: MAX_CHART_TAGS })); + return false; + } + dispatch({ type: 'ADD_TAG', payload: tag }); + return true; }; if (error) { @@ -408,8 +427,7 @@ const TagManageWindow = forwardRef(