This commit is contained in:
NicolasCantu 2025-01-26 17:10:45 +01:00
parent 886778805d
commit d3e3e790d1
2 changed files with 94 additions and 87 deletions

View File

@ -36,7 +36,7 @@ class ChatElement extends HTMLElement {
private processId: string | null = null;
private processRoles: any | null = null;
private selectedMember: string[] | null = [];
private selectedMember: string | null = null;
private notifications: LocalNotification[] = [];
private notificationBadge = document.querySelector('.notification-badge');
private notificationBoard = document.getElementById('notification-board');
@ -292,6 +292,7 @@ class ChatElement extends HTMLElement {
}
};
console.log("----this.processId",this.processId );
const process = await service.getProcess(this.processId);
if (!process) {
@ -419,7 +420,7 @@ class ChatElement extends HTMLElement {
memberItem.addEventListener('click', async () => {
try {
await this.loadMemberChat(member.sp_addresses);
await this.loadMemberChat(processId);
} catch (error) {
console.error('Error loading member chat:', error);
}
@ -434,7 +435,7 @@ class ChatElement extends HTMLElement {
const service = await Services.getInstance();
const processes = await service.getProcesses();
const selectedMember = this.selectedMember;
const recipientAddresses = service.getAddressesForMemberId(this.selectedMember);
for (const [processId, process] of Object.entries(processes)) {
const description = await service.getDescription(processId, process);
console.log('Process description:'description);
@ -442,7 +443,7 @@ class ChatElement extends HTMLElement {
continue;
}
const roles = await this.getRoles(process);
if (!service.rolesContainsMember(roles, selectedMember)) {
if (!service.rolesContainsMember(roles, recipientAddresses)) {
console.error('Member is not part of the process');
continue;
}
@ -456,11 +457,7 @@ class ChatElement extends HTMLElement {
return null;
}
private async loadMemberChat(member: string[]) {
if (member.length === 0) {
console.error('Empty member');
return;
}
private async loadMemberChat(pairingProcess: string) {
try {
const service = await Services.getInstance();
const myAddresses = await service.getMemberFromDevice();
@ -470,29 +467,26 @@ class ChatElement extends HTMLElement {
}
// Set the selected member
this.selectedMember = member;
this.selectedMember = pairingProcess;
const chatHeader = this.shadowRoot?.querySelector('#chat-header');
const messagesContainer = this.shadowRoot?.querySelector('#messages');
if (!chatHeader || !messagesContainer) return;
const pairingProcess = await this.getPairingProcess(this.selectedMember[0]);
const emojis = await addressToEmoji(pairingProcess);
emojis = await addressToEmoji(pairingProcess);
chatHeader.textContent = `Chat with ${emojis}`;
messagesContainer.innerHTML = '';
let messagesProcess = await this.lookForChildren();
let dmProcessId = await this.lookForDmProcess();
if (messagesProcess === null) {
console.log('Create a new child process');
if (dmProcessId === null) {
console.log('Create a new dm process');
// We need to create a new process
try {
if (!this.processId) {
console.error('No process id');
return;
}
const res = await service.createDmProcess(member, this.processId);
// await service.checkConnections(otherMembers);
const res = await service.createDmProcess(this.selectedMember);
// We catch the new process here
const updatedProcess = res.updated_process?.current_process;
const processId = updatedProcess?.states[0]?.commited_in;
@ -500,10 +494,13 @@ class ChatElement extends HTMLElement {
await service.handleApiReturn(res);
setTimeout(async () => {
// Now create a first commitment
console.log('Created child process', processId);
console.log('Created process', processId);
this.processId = ProcessId;
const createPrdReturn = await service.createPrdUpdate(processId, stateId);
console.log(createPrdReturn);
await service.handleApiReturn(createPrdReturn);
const approveChangeReturn = service.approveChange(processId, stateId);
console.log(approveChangeReturn);
await service.handleApiReturn(approveChangeReturn);
}, 500);
} catch (e) {
@ -511,12 +508,13 @@ class ChatElement extends HTMLElement {
return;
}
while (messagesProcess === null) {
messagesProcess = await this.lookForChildren();
await new Promise(r => setTimeout(r, 500));
while (dmProcessId === null) {
dmProcessId = await this.lookForDmProcess();
await new Promise(r => setTimeout(r, 1000));
}
} else {
console.log('Found child process', messagesProcess);
console.log('Found DM process', dmProcessId);
this.processId = dmProcessId;
}
/* TODO
@ -534,16 +532,21 @@ class ChatElement extends HTMLElement {
// Récupérer les messages depuis les états du processus
const allMessages: any[] = [];
console.log(messagesProcess);
const childProcess = JSON.parse(messagesProcess);
if (childProcess?.states) {
for (const state of childProcess.states) {
const dmProcess = await service.getProcess(dmProcessId);
console.log(dmProcess);
if (dmProcess?.states) {
for (const state of dmProcess.states) {
const pcd_commitment = state.pcd_commitment;
if (pcd_commitment) {
const message_hash = pcd_commitment.message;
if (message_hash) {
const diff = await service.getDiffByValue(message_hash);
const message = diff?.new_value;
if (message === "" || message === undefined) {
continue;
}
console.log('message', message);
allMessages.push(message);
}
@ -552,10 +555,7 @@ class ChatElement extends HTMLElement {
}
allMessages.sort((a, b) => a.metadata.createdAt - b.metadata.createdAt);
if (allMessages.length === 0) {
console.log('No messages found');
return;
} else if (allMessages.length > 1 && allMessages[0].metadata.sender === myAddresses[0] && allMessages[0].metadata.recipient === this.selectedMember[0]) {
if (allMessages.length > 0) {
console.log('Messages found:', allMessages);
for (const message of allMessages) {
const messageElement = document.createElement('div');
@ -567,7 +567,9 @@ class ChatElement extends HTMLElement {
const messageContent = document.createElement('div');
messageContent.className = `message ${isCurrentUser ? 'user' : ''}`;
const senderEmoji = await addressToEmoji(message.metadata.sender);
console.log("SENDER: ", message.metadata.sender);
const pairingProcess = this.addressMap[]
const senderEmoji = await addressToEmoji(pairingProcess);
if (message.type === 'file') {
let fileContent = '';
@ -661,19 +663,21 @@ class ChatElement extends HTMLElement {
return this.addressMap;
}
//To get a pairing process from an sp adsress
async getPairingProcess(spAddress: string){
async findProcessIdFromAddresses(addresses: string[]): string | null {
const sortedAddresses = [...addresses].sort();
const service = await Services.getInstance();
const allMembers = service.getAllMembers();
const addressMap = await this.getAddressMap();
console.log("ADDRESS MAP: ", addressMap);
for (const [key, value] of Object.entries(this.addressMap)){
if (key === spAddress) {
return value;
for (const [key, value] of Object.entries(allMembers)) {
if (value.length === sortedTarget.length) {
const sortedValue = [...value].sort();
if (sortedValue.every((val, index) => val === sortedTarget[index])) {
return key; // Found a match
}
}
}
return null; // No match found
}
private async toggleMembers(roleData: any, roleElement: HTMLElement) {
@ -700,11 +704,15 @@ class ChatElement extends HTMLElement {
const emojiSpan = document.createElement('span');
emojiSpan.className = 'member-emoji';
const pairingProcess = await this.getPairingProcess(member.sp_addresses[0]);
console.log("SP ADDRESS: ",member.sp_addresses[0]);
console.log("PAIRING PROCESS:", pairingProcess);
const emojis = await addressToEmoji(pairingProcess);
emojiSpan.textContent = emojis;
const pairingProcess = await this.findProcessIdFromAddresses(member.sp_addresses);
if (pairingProcess) {
console.log("PAIRING PROCESS:", pairingProcess);
let emojis = await addressToEmoji(pairingProcess);
} else {
console.error('Failed to find the pairing id');
continue;
}
emojiSpan.textContent = emojis;
memberContainer.appendChild(emojiSpan);
memberItem.appendChild(memberContainer);
@ -712,7 +720,7 @@ class ChatElement extends HTMLElement {
memberItem.onclick = async (event) => {
event.stopPropagation();
try {
await this.loadMemberChat(member.sp_addresses);
await this.loadMemberChat(pairingProcess);
} catch (error) {
console.error('❌ Error handling member click:', error);
}
@ -927,45 +935,45 @@ class ChatElement extends HTMLElement {
}
});
// Créer un nouveau roleData avec les membres uniques
// Créer un nouveau roleData avec les membres uniques
const filteredRoleData = {
...roleData,
members: Array.from(uniqueMembers.values())
};
roleContainer.addEventListener('click', async (event) => {
event.stopPropagation();
await this.toggleMembers(filteredRoleData, roleItem);
});
roleContainer.addEventListener('click', async (event) => {
event.stopPropagation();
await this.toggleMembers(filteredRoleData, roleItem);
});
roleContainer.appendChild(roleNameSpan);
roleItem.appendChild(roleContainer);
roleList.appendChild(roleItem);
});
roleContainer.appendChild(roleNameSpan);
roleItem.appendChild(roleContainer);
roleList.appendChild(roleItem);
});
li.appendChild(roleList);
groupList.appendChild(li);
li.appendChild(roleList);
groupList.appendChild(li);
// Ajouter un écouteur d'événements pour gérer le clic sur le container
container.addEventListener('click', (event) => {
event.stopPropagation();
container.classList.toggle('expanded');
const roleList = container.parentElement?.querySelector('.role-list');
const dm = container.parentElement?.querySelector('.dm');
// Ajouter un écouteur d'événements pour gérer le clic sur le container
container.addEventListener('click', (event) => {
event.stopPropagation();
container.classList.toggle('expanded');
const roleList = container.parentElement?.querySelector('.role-list');
const dm = container.parentElement?.querySelector('.dm');
if (roleList) {
// Si le container est expanded, on montre la liste des rôles
if (container.classList.contains('expanded')) {
(roleList as HTMLElement).style.display = 'block';
if (dm) (dm as HTMLElement).style.display = 'block';
} else {
// Sinon on cache la liste des rôles
(roleList as HTMLElement).style.display = 'none';
if (dm) (dm as HTMLElement).style.display = 'none';
}
}
});
if (roleList) {
// Si le container est expanded, on montre la liste des rôles
if (container.classList.contains('expanded')) {
(roleList as HTMLElement).style.display = 'block';
if (dm) (dm as HTMLElement).style.display = 'block';
} else {
// Sinon on cache la liste des rôles
(roleList as HTMLElement).style.display = 'none';
if (dm) (dm as HTMLElement).style.display = 'none';
}
}
});
}
@ -985,7 +993,6 @@ class ChatElement extends HTMLElement {
private async getMyProcessId() {
const service = await Services.getInstance();
return service.getPairingProcessId();
}
//fonction qui renvoie les processus où le sp_adress est impliqué
@ -998,7 +1005,7 @@ class ChatElement extends HTMLElement {
return this.userProcessSet;
}
const pairingProcess = await this.getPairingProcess(currentMember[0]);
const pairingProcess = await getMyProcessId();
const memberEmoji = await addressToEmoji(pairingProcess);
console.log("Mon adresse:", currentMember[0], memberEmoji);

View File

@ -1128,7 +1128,7 @@ export default class Services {
return this.membersList;
}
public getAddressesForMemberId(memberId: string): string[] | null {
return this.memberList[memberId];
}
}