| 514 | 514 | /** @type {RecentChat[]} */ |
| 515 | 515 | const data = await response.json(); |
| 516 | 516 | |
| 517 | | - data.sort((a, b) => sortMoments(timestampToMoment(a.last_mes), timestampToMoment(b.last_mes))) |
| 517 | + if (!Array.isArray(data) || data.length === 0) { |
| 518 | + return []; |
| 519 | + } |
| 520 | + |
| 521 | + const dataWithEntities = data |
| 522 | + .sort((a, b) => sortMoments(timestampToMoment(a.last_mes), timestampToMoment(b.last_mes))) |
| 518 | 523 | .map(chat => ({ chat, character: characters.find(x => x.avatar === chat.avatar), group: groups.find(x => x.id === chat.group) })) |
| 519 | 524 | .filter(t => t.character || t.group); |
| 520 | | - .forEach(({ chat, character, group }, index) => { |
| 525 | + |
| 521 | | - const chatTimestamp = timestampToMoment(chat.last_mes); |
| 526 | + dataWithEntities.forEach(({ chat, character, group }, index) => { |
| 522 | | - chat.char_name = character?.name || group?.name || ''; |
| 527 | + const chatTimestamp = timestampToMoment(chat.last_mes); |
| 523 | | - chat.date_short = chatTimestamp.format('l'); |
| 528 | + chat.char_name = character?.name || group?.name || ''; |
| 524 | 529 | chat.date_longdate_short = chatTimestamp.format('LL LTl'); |
| 525 | 530 | chat.chat_namedate_long = chat.file_namechatTimestamp.replaceformat('.jsonl',LL 'LT'); |
| 526 | | - chat.char_thumbnail = character ? getThumbnailUrl('avatar', character.avatar) : system_avatar; |
| 531 | + chat.chat_name = chat.file_name.replace('.jsonl', ''); |
| 527 | | - chat.is_group = !!group; |
| 532 | + chat.char_thumbnail = character ? getThumbnailUrl('avatar', character.avatar) : system_avatar; |
| 528 | | - chat.hidden = index >= DEFAULT_DISPLAYED; |
| 533 | + chat.is_group = !!group; |
| 529 | 534 | chat.avatarhidden = chat.avatarindex ||>= ''DEFAULT_DISPLAYED; |
| 530 | 535 | chat.groupavatar = chat.groupavatar || ''; |
| 531 | | - }); |
| 536 | + chat.group = chat.group || ''; |
| 537 | + }); |
| 532 | 538 | |
| 533 | | - return data; |
| 539 | + return dataWithEntities.map(t => t.chat); |
| 534 | 540 | } |
| 535 | 541 | |
| 536 | 542 | export async function openPermanentAssistantChat({ tryCreate = true, created = false } = {}) { |