113 lines
2.6 KiB
TypeScript
113 lines
2.6 KiB
TypeScript
import {
|
|
BufferState,
|
|
BufferActionPayload,
|
|
MessageBuffer,
|
|
BufferAction,
|
|
BufferActionKind,
|
|
} from './client-view.types';
|
|
|
|
export const getBuffer = (
|
|
state: BufferState,
|
|
payload: BufferActionPayload,
|
|
): MessageBuffer | undefined => {
|
|
return state.buffers.find(
|
|
(item) =>
|
|
(payload.connection
|
|
? payload.connection.name === item.connection.name
|
|
: true) &&
|
|
(payload.toBuffer
|
|
? (payload.toBuffer.type
|
|
? payload.toBuffer.type === item.type
|
|
: true) &&
|
|
(payload.toBuffer.name ? payload.toBuffer.name === item.name : true)
|
|
: true),
|
|
);
|
|
};
|
|
|
|
export const ClientViewReducer = (
|
|
state: BufferState,
|
|
action: BufferAction,
|
|
): BufferState => {
|
|
const { type, payload } = action;
|
|
if (!payload) return state;
|
|
if (payload.connection) {
|
|
if (type === BufferActionKind.AddBuffer) {
|
|
const buff = getBuffer(state, payload);
|
|
if (!buff) {
|
|
return {
|
|
activeIndex: state.buffers.length,
|
|
buffers: [
|
|
...state.buffers,
|
|
{
|
|
type: payload.toBuffer?.type!,
|
|
name: payload.toBuffer?.name!,
|
|
connection: payload.connection,
|
|
lines: [],
|
|
},
|
|
],
|
|
};
|
|
}
|
|
}
|
|
|
|
if (type === BufferActionKind.AddBufferLine && payload.message) {
|
|
const buff = getBuffer(state, payload);
|
|
|
|
if (buff) {
|
|
if (
|
|
!buff.lines.find(
|
|
(i) =>
|
|
payload.message?.time === i.time &&
|
|
payload.message.sender === i.sender,
|
|
)
|
|
) {
|
|
buff.lines = [...buff.lines, payload.message];
|
|
}
|
|
}
|
|
|
|
return { ...state };
|
|
}
|
|
|
|
if (type === BufferActionKind.UpdateNicklist) {
|
|
const channels = state.buffers.filter((buf) =>
|
|
buf.type === 'channel' && payload.channels?.length
|
|
? payload.channels.includes(buf.name)
|
|
: true,
|
|
);
|
|
|
|
channels.forEach((chanbuf) => {
|
|
const nicklist = payload.connection?.nicklist.getChannelByName(
|
|
chanbuf.name,
|
|
);
|
|
|
|
if (nicklist) {
|
|
chanbuf.nicks = nicklist.nicks;
|
|
}
|
|
});
|
|
|
|
return { ...state };
|
|
}
|
|
|
|
if (type === BufferActionKind.SetTopic && payload.topic) {
|
|
const buff = getBuffer(state, payload);
|
|
|
|
if (buff) {
|
|
buff.topic = payload.topic;
|
|
}
|
|
|
|
return { ...state };
|
|
}
|
|
}
|
|
|
|
if (
|
|
type === BufferActionKind.SetActiveBuffer &&
|
|
payload.activeIndex != null
|
|
) {
|
|
return {
|
|
...state,
|
|
activeIndex: payload.activeIndex,
|
|
};
|
|
}
|
|
|
|
return state;
|
|
};
|