mirror of
https://github.com/dataelement/bisheng.git
synced 2026-09-01 15:32:50 +08:00
fix(chat): render an image from the link the client already has
A message the user just sent isn't in the database yet, so asking the backend to re-issue a link came back empty — workflow chat showed "no longer available", task mode sat in its skeleton — and both only came right after a refresh. The upload already handed the client a working link, so that renders immediately and the re-issued one takes over when it arrives, which is what an older conversation needs. Stored links carry the internal storage host, so they get the same rewrite the download card does before being handed to the browser.
This commit is contained in:
@@ -118,6 +118,7 @@ function UploadedFileList({ files, conversationId }: { files: any[]; conversatio
|
||||
conversationId={conversationId}
|
||||
fileId={file.file_id}
|
||||
altText={file.name || file.file_name}
|
||||
initialUrl={file.filepath || file.file_path || file.file_url}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
@@ -17,37 +17,51 @@ export function MessageImage({
|
||||
conversationId,
|
||||
fileId,
|
||||
altText,
|
||||
initialUrl,
|
||||
}: {
|
||||
conversationId?: string;
|
||||
fileId?: string;
|
||||
altText?: string;
|
||||
/** Link the client already holds from the upload, if any. */
|
||||
initialUrl?: string;
|
||||
}) {
|
||||
const [url, setUrl] = useState<string | null>(null);
|
||||
// Stored links carry the internal storage host, which the browser can't
|
||||
// reach — same swap the download card does.
|
||||
const toReachable = (u?: string | null) =>
|
||||
u ? u.replace(/https?:\/\/[^/]+/, __APP_ENV__.BASE_URL) : null;
|
||||
|
||||
const [url, setUrl] = useState<string | null>(toReachable(initialUrl));
|
||||
const [failed, setFailed] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
// A just-sent message reaches here before the backend has assigned the
|
||||
// conversation its id. That's "not yet", not "gone" — keep waiting, or the
|
||||
// placeholder flashes up on every image the user sends.
|
||||
// Nothing to ask for, and nothing to show either.
|
||||
if (!conversationId || !fileId) {
|
||||
if (!initialUrl) {
|
||||
return;
|
||||
}
|
||||
setUrl(toReachable(initialUrl));
|
||||
return;
|
||||
}
|
||||
setFailed(false);
|
||||
// The upload link renders straight away — a message the user just sent
|
||||
// isn't in the database yet, so asking the backend for a link would come
|
||||
// back empty and read as a dead image. The re-issued link replaces it when
|
||||
// it arrives, which is what makes an old conversation work.
|
||||
getAttachmentUrl(conversationId, fileId).then((fresh) => {
|
||||
if (cancelled) {
|
||||
return;
|
||||
}
|
||||
if (fresh) {
|
||||
setUrl(fresh);
|
||||
} else {
|
||||
setUrl(toReachable(fresh));
|
||||
} else if (!initialUrl) {
|
||||
setFailed(true);
|
||||
}
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [conversationId, fileId]);
|
||||
}, [conversationId, fileId, initialUrl]);
|
||||
|
||||
// Debug aid: the resolved link and the ids it was resolved from are on the
|
||||
// wrapper, so a broken picture can be traced from devtools without digging
|
||||
|
||||
@@ -100,6 +100,7 @@ export default function MessageUser({ useName, data, showButton, disabledSearch
|
||||
conversationId={data.chat_id || chatId}
|
||||
fileId={file.file_id}
|
||||
altText={file.file_name || file.name}
|
||||
initialUrl={file.file_url || file.filepath}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user