mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-02 14:56:38 +08:00
5a8722ff07
接入无 URL 前缀的 zh-CN/en,顶栏与外观设置可切换语言;选择写入 cookie 后刷新生效。
52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
'use client';
|
|
|
|
import { useCallback, useState } from 'react';
|
|
import { toast } from 'sonner';
|
|
|
|
import { useTranslations } from 'next-intl';
|
|
|
|
import type {
|
|
ProxyRouteItem,
|
|
ProxyRouteMutationPayload,
|
|
} from '@/lib/services/openflare';
|
|
import { ProxyRouteService } from '@/lib/services/openflare';
|
|
|
|
import {
|
|
buildPayloadFromRoute,
|
|
getErrorMessage,
|
|
} from '../../components/helpers';
|
|
|
|
export function useRouteSectionSave(
|
|
route: ProxyRouteItem,
|
|
onRouteUpdate: (route: ProxyRouteItem) => void,
|
|
onSavingChange?: (saving: boolean) => void,
|
|
) {
|
|
const t = useTranslations('proxyRoutes');
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
const save = useCallback(
|
|
async (overrides: Partial<ProxyRouteMutationPayload>, message: string) => {
|
|
setSaving(true);
|
|
onSavingChange?.(true);
|
|
try {
|
|
const updated = await ProxyRouteService.update(
|
|
route.id,
|
|
buildPayloadFromRoute(route, overrides),
|
|
);
|
|
onRouteUpdate(updated);
|
|
toast.success(message);
|
|
} catch (error) {
|
|
toast.error(t('saveFailed'), {
|
|
description: getErrorMessage(error, t('requestFailed')),
|
|
});
|
|
} finally {
|
|
setSaving(false);
|
|
onSavingChange?.(false);
|
|
}
|
|
},
|
|
[onRouteUpdate, onSavingChange, route, t],
|
|
);
|
|
|
|
return { saving, save };
|
|
}
|