refactor: 拆分小程序发布与后台发布页面职责
This commit is contained in:
1 parent
8973338c64
commit
7c0f22ad7a
19 files changed
+797
-538
No files matched your search
@@ -4,7 +4,7 @@ import type { TemplateAdminSection } from "@personal-brand/api-client";
|
||||
import { TemplateListField } from "./TemplateListField.js";
|
||||
import { TemplateFieldRenderer } from "./TemplateFieldRenderer.js";
|
||||
import { MutationError } from "./QueryState.js";
|
||||
import { presentationFieldName } from "../features/brand/presentation-bindings.js";
|
||||
import { presentationFieldName } from "../lib/presentation/presentation-bindings.js";
|
||||
import { useRecordContentEditorState } from "./record-content/useRecordContentEditorState.js";
|
||||
import type { RecordContentEditorProps, RecordContentSource } from "./record-content/types.js";
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { Offering, TemplateAdminSection, TrustContent } from "@personal-brand/api-client";
|
||||
import type { PresentationValues } from "../../features/brand/presentation-bindings.js";
|
||||
import type { PresentationValues } from "../../lib/presentation/presentation-bindings.js";
|
||||
|
||||
export type RecordContentSource = "offering" | "trust";
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
presentationFieldName,
|
||||
readPresentationValue,
|
||||
type PresentationValues,
|
||||
} from "../../features/brand/presentation-bindings.js";
|
||||
} from "../../lib/presentation/presentation-bindings.js";
|
||||
import type { RecordContentSource } from "./types.js";
|
||||
|
||||
function createInitialValues(
|
||||
|
||||
@@ -39,7 +39,7 @@ import {
|
||||
filterSectionsForPreviewTarget,
|
||||
presentationFieldName,
|
||||
type PresentationValues,
|
||||
} from "./presentation-bindings.js";
|
||||
} from "../../lib/presentation/presentation-bindings.js";
|
||||
import "./PresentationEditor.css";
|
||||
import { useEditorDraft } from "./use-editor-draft.js";
|
||||
import {
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { useMemo } from "react";
|
||||
import type { Offering, TrustContent } from "@personal-brand/api-client";
|
||||
import type { TemplateAdminSection } from "@personal-brand/api-client";
|
||||
import { applyPresentationValues, applyStandardEntityValues } from "./presentation-bindings.js";
|
||||
import type { PresentationValues } from "./presentation-bindings.js";
|
||||
import {
|
||||
applyPresentationValues,
|
||||
applyStandardEntityValues,
|
||||
} from "../../lib/presentation/presentation-bindings.js";
|
||||
import type { PresentationValues } from "../../lib/presentation/presentation-bindings.js";
|
||||
|
||||
export interface RecordDraft {
|
||||
source: "offering" | "trust";
|
||||
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
presentationFieldName,
|
||||
readPresentationValue,
|
||||
type PresentationValues,
|
||||
} from "./presentation-bindings.js";
|
||||
} from "../../lib/presentation/presentation-bindings.js";
|
||||
|
||||
const standardBrandBindings = [
|
||||
"brand.name",
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
import { Alert, Button, Form, Input, Space } from "antd";
|
||||
import type { FormInstance } from "antd";
|
||||
import type { MiniAppReleaseSettings } from "@personal-brand/api-client";
|
||||
import { AdminSection } from "../../components/AdminSection.js";
|
||||
import { MutationError } from "../../components/QueryState.js";
|
||||
import { StatusTag } from "../../components/StatusTag.js";
|
||||
|
||||
export type MiniAppSettingsValues = {
|
||||
appId: string;
|
||||
apiBaseUrl: string;
|
||||
privateKey?: string;
|
||||
};
|
||||
|
||||
interface MiniAppSettingsTabProps {
|
||||
form: FormInstance<MiniAppSettingsValues>;
|
||||
settings: MiniAppReleaseSettings | undefined;
|
||||
saving: boolean;
|
||||
error?: unknown;
|
||||
onSave: (values: MiniAppSettingsValues) => void;
|
||||
}
|
||||
|
||||
export function MiniAppSettingsTab({
|
||||
form,
|
||||
settings,
|
||||
saving,
|
||||
error,
|
||||
onSave,
|
||||
}: MiniAppSettingsTabProps) {
|
||||
return (
|
||||
<div className="admin-stack admin-stack-lg">
|
||||
<AdminSection
|
||||
title="小程序上传配置"
|
||||
description="AppID 用于服务端调用微信官方上传工具;上传私钥保存前会使用 AES-256-GCM 加密。"
|
||||
>
|
||||
{settings?.buildError ? (
|
||||
<Alert
|
||||
type="warning"
|
||||
showIcon
|
||||
className="admin-offset-bottom"
|
||||
message="小程序构建参数未就绪"
|
||||
description={settings.buildError}
|
||||
/>
|
||||
) : (
|
||||
<Alert
|
||||
type="info"
|
||||
showIcon
|
||||
className="admin-offset-bottom"
|
||||
message={`请求域名 ${settings?.requestDomain ?? "未确认"}`}
|
||||
description="该域名在构建时写入小程序包,必须与微信后台“开发设置 → 服务器域名 → request 合法域名”一致;不一致会导致小程序打开即加载失败。"
|
||||
/>
|
||||
)}
|
||||
<Form form={form} layout="vertical" onFinish={onSave}>
|
||||
<Form.Item
|
||||
name="appId"
|
||||
label="微信 AppID"
|
||||
rules={[{ required: true, message: "请输入微信 AppID" }]}
|
||||
>
|
||||
<Input maxLength={100} />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
name="apiBaseUrl"
|
||||
label="小程序请求地址"
|
||||
rules={[
|
||||
{ required: true, message: "请输入小程序请求地址" },
|
||||
{
|
||||
validator: (_rule, value: string) =>
|
||||
!value || /^https:\/\/[^/]+/.test(value)
|
||||
? Promise.resolve()
|
||||
: Promise.reject(
|
||||
new Error(
|
||||
"公网地址必须以 https:// 开头,例如 https://wechat.app.que01.top/api",
|
||||
),
|
||||
),
|
||||
},
|
||||
]}
|
||||
extra="构建时写入小程序包,必须与微信后台“开发设置 → 服务器域名 → request 合法域名”一致。修改后需重新构建并上传代码才生效。"
|
||||
>
|
||||
<Input maxLength={200} placeholder="https://wechat.app.que01.top/api" />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
name="privateKey"
|
||||
label="上传私钥"
|
||||
help={
|
||||
settings?.hasStoredCredentials
|
||||
? "已加密保存。留空保持现有私钥,输入新值可替换。"
|
||||
: "粘贴微信开发者工具导出的 private.<appid>.key 全文,必须保留换行。"
|
||||
}
|
||||
>
|
||||
<Input.TextArea
|
||||
rows={5}
|
||||
autoComplete="off"
|
||||
spellCheck={false}
|
||||
placeholder={"-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----"}
|
||||
className="admin-code-input"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Space>
|
||||
<Button type="primary" loading={saving} htmlType="submit">
|
||||
保存上传配置
|
||||
</Button>
|
||||
{settings?.hasStoredCredentials ? (
|
||||
<StatusTag tone="success" label="已加密保存上传凭据" />
|
||||
) : (
|
||||
<StatusTag tone="warning" label="未配置上传私钥" />
|
||||
)}
|
||||
</Space>
|
||||
<MutationError error={error} />
|
||||
</Form>
|
||||
</AdminSection>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import {
|
||||
Alert,
|
||||
Button,
|
||||
@@ -23,6 +23,9 @@ import { EmptyState } from "../../components/EmptyState.js";
|
||||
import { ErrorNote, MutationError, QueryState } from "../../components/QueryState.js";
|
||||
import { StatusTag, statusLabel } from "../../components/StatusTag.js";
|
||||
import { MiniAppReleaseCard } from "./MiniAppReleaseCard.js";
|
||||
import { MiniAppSettingsTab, type MiniAppSettingsValues } from "./MiniAppSettingsTab.js";
|
||||
import { usePublicationQueries } from "./use-publication-queries.js";
|
||||
import { usePublicationMutations } from "./use-publication-mutations.js";
|
||||
|
||||
const client = createApiClient();
|
||||
|
||||
@@ -44,66 +47,22 @@ export function PublicationPage({ canManageTemplate }: { canManageTemplate: bool
|
||||
const [rollbackVersion, setRollbackVersion] = useState<number | null>(null);
|
||||
const [note, setNote] = useState("");
|
||||
const [activeTab, setActiveTab] = useState("content");
|
||||
const [miniAppSettingsForm] = Form.useForm<{
|
||||
appId: string;
|
||||
apiBaseUrl: string;
|
||||
privateKey?: string;
|
||||
}>();
|
||||
const brand = useQuery({
|
||||
queryKey: ["publication-brand"],
|
||||
queryFn: () => client.getAdminBrand(),
|
||||
});
|
||||
const workspace = useQuery({
|
||||
queryKey: ["publication-workspace"],
|
||||
queryFn: () => client.getAdminWorkspace(),
|
||||
});
|
||||
const catalog = useQuery({
|
||||
queryKey: ["publication-catalog"],
|
||||
queryFn: () => client.listPublicTemplates(),
|
||||
});
|
||||
const sample = useQuery({
|
||||
queryKey: ["publication-sample"],
|
||||
queryFn: () => client.previewAdminSampleContent(),
|
||||
enabled: Boolean(workspace.data),
|
||||
});
|
||||
const readiness = useQuery({
|
||||
queryKey: ["publication-readiness"],
|
||||
queryFn: () => client.getAdminReleaseReadiness(),
|
||||
});
|
||||
const releases = useQuery({
|
||||
queryKey: ["publication-releases"],
|
||||
queryFn: () => client.listAdminReleases(),
|
||||
});
|
||||
const miniAppReleases = useQuery({
|
||||
queryKey: ["publication-miniapp-releases"],
|
||||
queryFn: () => client.listAdminMiniAppReleases(),
|
||||
});
|
||||
const miniAppSettings = useQuery({
|
||||
queryKey: ["publication-miniapp-settings"],
|
||||
queryFn: () => client.getAdminMiniAppSettings(),
|
||||
});
|
||||
const templatePreview = useQuery({
|
||||
queryKey: ["publication-template-preview"],
|
||||
queryFn: () => client.getAdminTemplateUpgradePreview(),
|
||||
});
|
||||
useEffect(() => {
|
||||
if (!miniAppSettings.data) return;
|
||||
miniAppSettingsForm.setFieldsValue({
|
||||
appId: miniAppSettings.data.appId,
|
||||
apiBaseUrl: miniAppSettings.data.apiBaseUrl,
|
||||
privateKey: "",
|
||||
});
|
||||
}, [miniAppSettings.data, miniAppSettingsForm]);
|
||||
useEffect(() => {
|
||||
const building = miniAppReleases.data?.find(
|
||||
(task) =>
|
||||
task.buildStatus === "building" &&
|
||||
Date.now() - new Date(task.updatedAt).getTime() < 15 * 60 * 1000,
|
||||
);
|
||||
if (!building) return;
|
||||
const timer = setInterval(() => void miniAppReleases.refetch(), 1500);
|
||||
return () => clearInterval(timer);
|
||||
}, [miniAppReleases.data, miniAppReleases]);
|
||||
const [miniAppSettingsForm] = Form.useForm<MiniAppSettingsValues>();
|
||||
const queries = usePublicationQueries(miniAppSettingsForm);
|
||||
const {
|
||||
brand,
|
||||
workspace,
|
||||
catalog,
|
||||
sample,
|
||||
readiness,
|
||||
releases,
|
||||
miniAppReleases,
|
||||
miniAppSettings,
|
||||
templatePreview,
|
||||
loading,
|
||||
error,
|
||||
retry,
|
||||
} = queries;
|
||||
const refresh = () => {
|
||||
for (const key of [
|
||||
"publication-brand",
|
||||
@@ -120,144 +79,28 @@ export function PublicationPage({ canManageTemplate }: { canManageTemplate: bool
|
||||
void queryClient.invalidateQueries({ queryKey: [key] });
|
||||
}
|
||||
};
|
||||
const install = useMutation({
|
||||
mutationFn: (key: BrandTemplateKey) => client.installAdminTemplate(key),
|
||||
onSuccess: () => {
|
||||
setIndustryTarget(null);
|
||||
message.success("行业模板已安装");
|
||||
refresh();
|
||||
},
|
||||
const mutations = usePublicationMutations({
|
||||
refresh,
|
||||
brandConfig: brand.data?.config,
|
||||
onInstallClose: () => setIndustryTarget(null),
|
||||
onPublishClose: () => setConfirmAction(null),
|
||||
onNoteClear: () => setNote(""),
|
||||
onRollbackClose: () => setRollbackVersion(null),
|
||||
});
|
||||
const upgradeTemplate = useMutation({
|
||||
mutationFn: () => client.upgradeAdminTemplate(),
|
||||
onSuccess: () => {
|
||||
message.success("行业模板已升级,现有内容未被覆盖");
|
||||
refresh();
|
||||
},
|
||||
});
|
||||
const rollbackTemplate = useMutation({
|
||||
mutationFn: () => client.rollbackAdminTemplate(),
|
||||
onSuccess: () => {
|
||||
message.success("行业模板配置基线已回滚");
|
||||
refresh();
|
||||
},
|
||||
});
|
||||
const selectVisual = useMutation({
|
||||
mutationFn: (key: string) =>
|
||||
client.updateAdminBrand({
|
||||
config: { ...(brand.data?.config ?? {}), visualTemplateKey: key },
|
||||
}),
|
||||
onSuccess: () => {
|
||||
message.success("视觉模板已保存到草稿");
|
||||
refresh();
|
||||
},
|
||||
});
|
||||
const importSample = useMutation({
|
||||
mutationFn: () => client.importAdminSampleContent(),
|
||||
onSuccess: ({ counts }) => {
|
||||
message.success(
|
||||
counts.inquiryForms
|
||||
? "示例内容和客户咨询表单已导入草稿"
|
||||
: "示例内容已导入草稿,现有客户咨询表单已保留",
|
||||
);
|
||||
refresh();
|
||||
},
|
||||
});
|
||||
const publish = useMutation({
|
||||
mutationFn: () => client.publishAdminRelease(note.trim() || undefined),
|
||||
onSuccess: () => {
|
||||
setConfirmAction(null);
|
||||
setNote("");
|
||||
message.success("品牌已发布");
|
||||
refresh();
|
||||
},
|
||||
});
|
||||
const revoke = useMutation({
|
||||
mutationFn: () => client.revokeAdminRelease(),
|
||||
onSuccess: () => {
|
||||
setConfirmAction(null);
|
||||
message.success("品牌已撤回,小程序将显示未发布状态");
|
||||
refresh();
|
||||
},
|
||||
});
|
||||
const rollback = useMutation({
|
||||
mutationFn: (version: number) => client.rollbackAdminRelease(version),
|
||||
onSuccess: () => {
|
||||
setRollbackVersion(null);
|
||||
message.success("历史版本已重新发布");
|
||||
refresh();
|
||||
},
|
||||
});
|
||||
const uploadLatestMiniAppRelease = useMutation({
|
||||
mutationFn: async () => {
|
||||
const latest = await client.getLatestAdminMiniAppRelease();
|
||||
const task = latest ?? (await client.createAdminMiniAppRelease());
|
||||
return client.uploadAdminMiniAppRelease(task.id);
|
||||
},
|
||||
onSuccess: () => {
|
||||
message.success("小程序代码已上传");
|
||||
void queryClient.invalidateQueries({ queryKey: ["publication-miniapp-releases"] });
|
||||
},
|
||||
});
|
||||
const saveMiniAppSettings = useMutation({
|
||||
mutationFn: (values: { appId: string; apiBaseUrl: string; privateKey?: string }) =>
|
||||
client.updateAdminMiniAppSettings({
|
||||
appId: values.appId,
|
||||
apiBaseUrl: values.apiBaseUrl,
|
||||
...(values.privateKey ? { privateKey: values.privateKey } : {}),
|
||||
}),
|
||||
onSuccess: () => {
|
||||
message.success("小程序上传配置已保存");
|
||||
void queryClient.invalidateQueries({ queryKey: ["publication-miniapp-settings"] });
|
||||
},
|
||||
});
|
||||
const uploadMiniAppRelease = useMutation({
|
||||
mutationFn: (id: string) => client.uploadAdminMiniAppRelease(id),
|
||||
onSuccess: () => {
|
||||
message.success("小程序代码已上传");
|
||||
void queryClient.invalidateQueries({ queryKey: ["publication-miniapp-releases"] });
|
||||
},
|
||||
});
|
||||
const updateMiniAppStatus = useMutation({
|
||||
mutationFn: ({
|
||||
id,
|
||||
status,
|
||||
}: {
|
||||
id: string;
|
||||
status: Parameters<typeof client.updateAdminMiniAppReleaseStatus>[1]["status"];
|
||||
}) => client.updateAdminMiniAppReleaseStatus(id, { status }),
|
||||
onSuccess: () => {
|
||||
message.success("小程序发布状态已更新");
|
||||
void queryClient.invalidateQueries({ queryKey: ["publication-miniapp-releases"] });
|
||||
},
|
||||
});
|
||||
const loading = [
|
||||
brand,
|
||||
workspace,
|
||||
catalog,
|
||||
readiness,
|
||||
releases,
|
||||
miniAppReleases,
|
||||
miniAppSettings,
|
||||
].some((query) => query.isLoading);
|
||||
const error = [
|
||||
brand,
|
||||
workspace,
|
||||
catalog,
|
||||
readiness,
|
||||
releases,
|
||||
miniAppReleases,
|
||||
miniAppSettings,
|
||||
].find((query) => query.isError)?.error;
|
||||
const retry = () => {
|
||||
void brand.refetch();
|
||||
void workspace.refetch();
|
||||
void catalog.refetch();
|
||||
void readiness.refetch();
|
||||
void releases.refetch();
|
||||
void miniAppReleases.refetch();
|
||||
void miniAppSettings.refetch();
|
||||
};
|
||||
const {
|
||||
install,
|
||||
upgradeTemplate,
|
||||
rollbackTemplate,
|
||||
selectVisual,
|
||||
importSample,
|
||||
publish,
|
||||
revoke,
|
||||
rollback,
|
||||
uploadLatestMiniAppRelease,
|
||||
saveMiniAppSettings,
|
||||
uploadMiniAppRelease,
|
||||
updateMiniAppStatus,
|
||||
} = mutations;
|
||||
const industryKey =
|
||||
workspace.data?.installations[0]?.templateKey ??
|
||||
workspace.data?.brand?.templateKey ??
|
||||
@@ -391,7 +234,16 @@ export function PublicationPage({ canManageTemplate }: { canManageTemplate: bool
|
||||
{
|
||||
key: "settings",
|
||||
label: "小程序配置",
|
||||
children: activeTab === "settings" ? miniAppSettingsTab : null,
|
||||
children:
|
||||
activeTab === "settings" ? (
|
||||
<MiniAppSettingsTab
|
||||
form={miniAppSettingsForm}
|
||||
settings={miniAppSettings.data}
|
||||
saving={saveMiniAppSettings.isPending}
|
||||
error={saveMiniAppSettings.error}
|
||||
onSave={(values) => saveMiniAppSettings.mutate(values)}
|
||||
/>
|
||||
) : null,
|
||||
},
|
||||
]}
|
||||
/>
|
||||
@@ -695,7 +547,7 @@ export function PublicationPage({ canManageTemplate }: { canManageTemplate: bool
|
||||
title={confirmAction === "revoke" ? "确认撤回品牌" : "确认发布品牌"}
|
||||
open={confirmAction !== null}
|
||||
onCancel={() => setConfirmAction(null)}
|
||||
onOk={() => (confirmAction === "revoke" ? revoke.mutate() : publish.mutate())}
|
||||
onOk={() => (confirmAction === "revoke" ? revoke.mutate() : publish.mutate(note.trim()))}
|
||||
confirmLoading={publish.isPending || revoke.isPending}
|
||||
okText={confirmAction === "revoke" ? "确认撤回" : "确认发布"}
|
||||
okButtonProps={{ danger: confirmAction === "revoke" }}
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { message } from "antd";
|
||||
import type { BrandTemplateKey } from "@personal-brand/api-client";
|
||||
import { createApiClient } from "../../lib/api.js";
|
||||
|
||||
const client = createApiClient();
|
||||
|
||||
interface MutationOptions {
|
||||
refresh: () => void;
|
||||
brandConfig: Record<string, unknown> | null | undefined;
|
||||
onInstallClose: () => void;
|
||||
onPublishClose: () => void;
|
||||
onNoteClear: () => void;
|
||||
onRollbackClose: () => void;
|
||||
}
|
||||
|
||||
export function usePublicationMutations({
|
||||
refresh,
|
||||
onInstallClose,
|
||||
onPublishClose,
|
||||
onNoteClear,
|
||||
onRollbackClose,
|
||||
brandConfig,
|
||||
}: MutationOptions) {
|
||||
const queryClient = useQueryClient();
|
||||
const install = useMutation({
|
||||
mutationFn: (key: BrandTemplateKey) => client.installAdminTemplate(key),
|
||||
onSuccess: () => {
|
||||
onInstallClose();
|
||||
message.success("行业模板已安装");
|
||||
refresh();
|
||||
},
|
||||
});
|
||||
const upgradeTemplate = useMutation({
|
||||
mutationFn: () => client.upgradeAdminTemplate(),
|
||||
onSuccess: () => {
|
||||
message.success("行业模板已升级,现有内容未被覆盖");
|
||||
refresh();
|
||||
},
|
||||
});
|
||||
const rollbackTemplate = useMutation({
|
||||
mutationFn: () => client.rollbackAdminTemplate(),
|
||||
onSuccess: () => {
|
||||
message.success("行业模板配置基线已回滚");
|
||||
refresh();
|
||||
},
|
||||
});
|
||||
const selectVisual = useMutation({
|
||||
mutationFn: (key: string) =>
|
||||
client.updateAdminBrand({
|
||||
config: { ...(brandConfig ?? {}), visualTemplateKey: key },
|
||||
}),
|
||||
onSuccess: () => {
|
||||
message.success("视觉模板已保存到草稿");
|
||||
refresh();
|
||||
},
|
||||
});
|
||||
const importSample = useMutation<Awaited<ReturnType<typeof client.importAdminSampleContent>>>({
|
||||
mutationFn: () => client.importAdminSampleContent(),
|
||||
onSuccess: ({ counts }) => {
|
||||
message.success(
|
||||
counts.inquiryForms
|
||||
? "示例内容和客户咨询表单已导入草稿"
|
||||
: "示例内容已导入草稿,现有客户咨询表单已保留",
|
||||
);
|
||||
refresh();
|
||||
},
|
||||
});
|
||||
const publish = useMutation({
|
||||
mutationFn: (note: string) => client.publishAdminRelease(note || undefined),
|
||||
onSuccess: () => {
|
||||
onPublishClose();
|
||||
onNoteClear();
|
||||
message.success("品牌已发布");
|
||||
refresh();
|
||||
},
|
||||
});
|
||||
const revoke = useMutation({
|
||||
mutationFn: () => client.revokeAdminRelease(),
|
||||
onSuccess: () => {
|
||||
onPublishClose();
|
||||
message.success("品牌已撤回,小程序将显示未发布状态");
|
||||
refresh();
|
||||
},
|
||||
});
|
||||
const rollback = useMutation({
|
||||
mutationFn: (version: number) => client.rollbackAdminRelease(version),
|
||||
onSuccess: () => {
|
||||
onRollbackClose();
|
||||
message.success("历史版本已重新发布");
|
||||
refresh();
|
||||
},
|
||||
});
|
||||
const uploadLatestMiniAppRelease = useMutation({
|
||||
mutationFn: async () => {
|
||||
const latest = await client.getLatestAdminMiniAppRelease();
|
||||
const task = latest ?? (await client.createAdminMiniAppRelease());
|
||||
return client.uploadAdminMiniAppRelease(task.id);
|
||||
},
|
||||
onSuccess: () => {
|
||||
message.success("小程序代码已上传");
|
||||
void queryClient.invalidateQueries({ queryKey: ["publication-miniapp-releases"] });
|
||||
},
|
||||
});
|
||||
const saveMiniAppSettings = useMutation({
|
||||
mutationFn: (values: { appId: string; apiBaseUrl: string; privateKey?: string }) =>
|
||||
client.updateAdminMiniAppSettings({
|
||||
appId: values.appId,
|
||||
apiBaseUrl: values.apiBaseUrl,
|
||||
...(values.privateKey ? { privateKey: values.privateKey } : {}),
|
||||
}),
|
||||
onSuccess: () => {
|
||||
message.success("小程序上传配置已保存");
|
||||
void queryClient.invalidateQueries({ queryKey: ["publication-miniapp-settings"] });
|
||||
},
|
||||
});
|
||||
const uploadMiniAppRelease = useMutation({
|
||||
mutationFn: (id: string) => client.uploadAdminMiniAppRelease(id),
|
||||
onSuccess: () => {
|
||||
message.success("小程序代码已上传");
|
||||
void queryClient.invalidateQueries({ queryKey: ["publication-miniapp-releases"] });
|
||||
},
|
||||
});
|
||||
const updateMiniAppStatus = useMutation({
|
||||
mutationFn: ({
|
||||
id,
|
||||
status,
|
||||
}: {
|
||||
id: string;
|
||||
status: Parameters<typeof client.updateAdminMiniAppReleaseStatus>[1]["status"];
|
||||
}) => client.updateAdminMiniAppReleaseStatus(id, { status }),
|
||||
onSuccess: () => {
|
||||
message.success("小程序发布状态已更新");
|
||||
void queryClient.invalidateQueries({ queryKey: ["publication-miniapp-releases"] });
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
install,
|
||||
upgradeTemplate,
|
||||
rollbackTemplate,
|
||||
selectVisual,
|
||||
importSample,
|
||||
publish,
|
||||
revoke,
|
||||
rollback,
|
||||
uploadLatestMiniAppRelease,
|
||||
saveMiniAppSettings,
|
||||
uploadMiniAppRelease,
|
||||
updateMiniAppStatus,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import { useEffect } from "react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import type { FormInstance } from "antd";
|
||||
import { createApiClient } from "../../lib/api.js";
|
||||
|
||||
const client = createApiClient();
|
||||
|
||||
export function usePublicationQueries(
|
||||
miniAppSettingsForm: FormInstance<{
|
||||
appId: string;
|
||||
apiBaseUrl: string;
|
||||
privateKey?: string;
|
||||
}>,
|
||||
) {
|
||||
const brand = useQuery<Awaited<ReturnType<typeof client.getAdminBrand>>>({
|
||||
queryKey: ["publication-brand"],
|
||||
queryFn: () => client.getAdminBrand(),
|
||||
});
|
||||
const workspace = useQuery<Awaited<ReturnType<typeof client.getAdminWorkspace>>>({
|
||||
queryKey: ["publication-workspace"],
|
||||
queryFn: () => client.getAdminWorkspace(),
|
||||
});
|
||||
const catalog = useQuery<Awaited<ReturnType<typeof client.listPublicTemplates>>>({
|
||||
queryKey: ["publication-catalog"],
|
||||
queryFn: () => client.listPublicTemplates(),
|
||||
});
|
||||
const sample = useQuery<Awaited<ReturnType<typeof client.previewAdminSampleContent>>>({
|
||||
queryKey: ["publication-sample"],
|
||||
queryFn: () => client.previewAdminSampleContent(),
|
||||
enabled: Boolean(workspace.data),
|
||||
});
|
||||
const readiness = useQuery<Awaited<ReturnType<typeof client.getAdminReleaseReadiness>>>({
|
||||
queryKey: ["publication-readiness"],
|
||||
queryFn: () => client.getAdminReleaseReadiness(),
|
||||
});
|
||||
const releases = useQuery<Awaited<ReturnType<typeof client.listAdminReleases>>>({
|
||||
queryKey: ["publication-releases"],
|
||||
queryFn: () => client.listAdminReleases(),
|
||||
});
|
||||
const miniAppReleases = useQuery<Awaited<ReturnType<typeof client.listAdminMiniAppReleases>>>({
|
||||
queryKey: ["publication-miniapp-releases"],
|
||||
queryFn: () => client.listAdminMiniAppReleases(),
|
||||
});
|
||||
const miniAppSettings = useQuery<Awaited<ReturnType<typeof client.getAdminMiniAppSettings>>>({
|
||||
queryKey: ["publication-miniapp-settings"],
|
||||
queryFn: () => client.getAdminMiniAppSettings(),
|
||||
});
|
||||
const templatePreview = useQuery<
|
||||
Awaited<ReturnType<typeof client.getAdminTemplateUpgradePreview>>
|
||||
>({
|
||||
queryKey: ["publication-template-preview"],
|
||||
queryFn: () => client.getAdminTemplateUpgradePreview(),
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (!miniAppSettings.data) return;
|
||||
miniAppSettingsForm.setFieldsValue({
|
||||
appId: miniAppSettings.data.appId,
|
||||
apiBaseUrl: miniAppSettings.data.apiBaseUrl,
|
||||
privateKey: "",
|
||||
});
|
||||
}, [miniAppSettings.data, miniAppSettingsForm]);
|
||||
|
||||
useEffect(() => {
|
||||
const building = miniAppReleases.data?.find(
|
||||
(task) =>
|
||||
task.buildStatus === "building" &&
|
||||
Date.now() - new Date(task.updatedAt).getTime() < 15 * 60 * 1000,
|
||||
);
|
||||
if (!building) return;
|
||||
const timer = setInterval(() => void miniAppReleases.refetch(), 1500);
|
||||
return () => clearInterval(timer);
|
||||
}, [miniAppReleases]);
|
||||
|
||||
const loading = [
|
||||
brand,
|
||||
workspace,
|
||||
catalog,
|
||||
readiness,
|
||||
releases,
|
||||
miniAppReleases,
|
||||
miniAppSettings,
|
||||
].some((query) => query.isLoading);
|
||||
const error = [
|
||||
brand,
|
||||
workspace,
|
||||
catalog,
|
||||
readiness,
|
||||
releases,
|
||||
miniAppReleases,
|
||||
miniAppSettings,
|
||||
].find((query) => query.isError)?.error;
|
||||
const retry = () => {
|
||||
void brand.refetch();
|
||||
void workspace.refetch();
|
||||
void catalog.refetch();
|
||||
void readiness.refetch();
|
||||
void releases.refetch();
|
||||
void miniAppReleases.refetch();
|
||||
void miniAppSettings.refetch();
|
||||
};
|
||||
|
||||
return {
|
||||
brand,
|
||||
workspace,
|
||||
catalog,
|
||||
sample,
|
||||
readiness,
|
||||
releases,
|
||||
miniAppReleases,
|
||||
miniAppSettings,
|
||||
templatePreview,
|
||||
loading,
|
||||
error,
|
||||
retry,
|
||||
};
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
applyPresentationValues,
|
||||
presentationFieldName,
|
||||
readPresentationValue,
|
||||
} from "../brand/presentation-bindings.js";
|
||||
} from "../../lib/presentation/presentation-bindings.js";
|
||||
|
||||
export type OfferingFormValues = Record<string, unknown>;
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import type {
|
||||
TemplateAdminField,
|
||||
} from "@personal-brand/api-client";
|
||||
import type { PreviewTarget } from "../brand/preview-selection.js";
|
||||
import { applyPresentationValues } from "../brand/presentation-bindings.js";
|
||||
import { applyPresentationValues } from "../../lib/presentation/presentation-bindings.js";
|
||||
|
||||
export function previewTargetsByPage(
|
||||
sections: AdminTemplateContent["admin"]["sections"],
|
||||
|
||||
File renamed without changes.
@@ -6,7 +6,7 @@ import {
|
||||
filterSectionsForPreviewTarget,
|
||||
presentationFieldName,
|
||||
readPresentationValue,
|
||||
} from "../src/features/brand/presentation-bindings.ts";
|
||||
} from "../src/lib/presentation/presentation-bindings.ts";
|
||||
import {
|
||||
resolvePreviewSelection,
|
||||
previewTrustRecordId,
|
||||
|
||||
Reference in new issue
Block a user