Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src-tauri/src/commands/asset_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,13 @@ pub async fn get_asset_detail(
let (_, asset_service) = match load_asset_service(&app, &session).await {
Ok(result) => result,
Err(error) => {
error!(
"get asset detail failed: asset_id={}, error={}",
asset_id, error
);
let _ = app.emit(
"get-asset-detail-failure",
json!({ "status": 401, "error": error }),
json!({ "status": 401, "error": error, "asset_id": asset_id }),
);
return Ok(());
}
Expand All @@ -134,9 +138,17 @@ pub async fn get_asset_detail(
let asset_detail = asset_service.get_asset_detail(&asset_id).await;

if !asset_detail.success {
error!(
"get asset detail failed: asset_id={}, status={}",
asset_id, asset_detail.status
);
let _ = app.emit(
"get-asset-detail-failure",
json!({ "status": asset_detail.status }),
json!({
"status": asset_detail.status,
"error": asset_detail.data,
"asset_id": asset_id
}),
);
return Ok(());
}
Expand Down
73 changes: 62 additions & 11 deletions ui/components/ConnectionEditor/connectionEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const props = defineProps<{
}>();

const { t, locale } = useI18n();
const toast = useToast();
const { getAssetDetail } = useAssetAction();
const ASSET_DETAIL_TIMEOUT_MS = 15000;

const open = ref(false);
const currentAsset = ref<AssetItem | null>(null);
Expand Down Expand Up @@ -163,10 +165,30 @@ async function ensureDetails(asset: AssetItem) {

if (!noAccounts && !noProtocols) return asset;

const detailsReady = new Promise<AssetItem>((resolve) => {
const unsubscribe = useEventBus().once(
const bus = useEventBus();

return await new Promise<AssetItem>((resolve, reject) => {
let settled = false;
let timer: ReturnType<typeof setTimeout> | undefined;
let unsubscribeUpdated: (() => void) | undefined;
let unsubscribeFailed: (() => void) | undefined;

const cleanup = () => {
if (timer) clearTimeout(timer);
unsubscribeUpdated?.();
unsubscribeFailed?.();
};

const finish = (next: () => void) => {
if (settled) return;
settled = true;
cleanup();
next();
};

unsubscribeUpdated = bus.on(
"assetDetailUpdated",
(payload: { assetId: string, permedAccounts: PermedAccount[], permedProtocols: PermedProtocol[] }) => {
(payload) => {
if (payload.assetId !== asset.id) return;

currentAsset.value = {
Expand All @@ -175,16 +197,26 @@ async function ensureDetails(asset: AssetItem) {
permedProtocols: payload.permedProtocols || []
} as AssetItem;

resolve(currentAsset.value!);
}
finish(() => resolve(currentAsset.value!));
},
false
);

void unsubscribe;
});
unsubscribeFailed = bus.on(
"assetDetailFailed",
(payload) => {
if (payload.assetId !== asset.id) return;
finish(() => reject(new Error("get asset detail failed")));
},
false
);

await getAssetDetail(asset.id);
const updated = await detailsReady;
return updated;
timer = setTimeout(() => {
finish(() => reject(new Error("get asset detail timeout")));
}, ASSET_DETAIL_TIMEOUT_MS);

getAssetDetail(asset.id);
});
}

/**
Expand All @@ -193,7 +225,26 @@ async function ensureDetails(asset: AssetItem) {
*/
async function openModal(asset: AssetItem, preferredProtocol?: string): Promise<any> {
currentAsset.value = asset;
await ensureDetails(asset);

try {
await ensureDetails(asset);
} catch (error) {
const timedOut = error instanceof Error && error.message.includes("timeout");

if (timedOut) {
toast.add({
title: t("Asset.GetAssetFailed"),
description: t("ConnectError.ConnectFailed"),
color: "error",
icon: "line-md:close-circle",
progress: true,
duration: 4000
});
}

throw error;
}

initDraft(currentAsset.value!, preferredProtocol);
open.value = true;

Expand Down
32 changes: 27 additions & 5 deletions ui/composables/useAssetAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,11 +578,33 @@ export const useAssetAction = () => {
}
});

// TODO 提示
unlistenGetAssetDetailFailed = await useTauriEventListen("get-asset-detail-failure", () => {
// interface eventPayload {
// status: string
// }
unlistenGetAssetDetailFailed = await useTauriEventListen("get-asset-detail-failure", (event) => {
interface eventPayload {
status?: number | string
error?: string
asset_id?: string
}

const payload = event.payload as eventPayload;
const status = Number(payload.status);

toast.add({
title: status === 401 ? t("Login.LoginAuthenticationExpired") : t("Asset.GetAssetFailed"),
description: status === 401
? t("Login.LoginAuthenticationExpiredDescription")
: t("ConnectError.ConnectFailed"),
color: "error",
icon: "line-md:close-circle",
progress: true,
duration: 4000
});

if (payload.asset_id) {
useEventBus().emit("assetDetailFailed", {
assetId: payload.asset_id,
status: Number.isFinite(status) ? status : undefined
});
}
});

unlistenRenameSuccess = await useTauriEventListen("rename-success", (event) => {
Expand Down
4 changes: 4 additions & 0 deletions ui/composables/useEventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ type BusEvents = {
permedAccounts: PermedAccount[]
permedProtocols: PermedProtocol[]
}
assetDetailFailed: {
assetId: string
status?: number
}
} & Record<EventType, unknown>;

const emitter: Emitter<BusEvents> = mitt<BusEvents>();
Expand Down
Loading