File size: 1,932 Bytes
9333689
 
97e41aa
 
38448fc
5411802
9333689
 
 
c3e8f3d
5411802
 
 
 
c3e8f3d
9333689
 
 
 
 
 
5411802
 
 
 
f80b091
 
5411802
9333689
 
 
 
 
 
 
 
5411802
9333689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f80b091
 
c3e8f3d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use client';

import ChatCard, { ChatCardLayout } from './ChatCard';
import { IconPlus } from '../ui/Icons';
import { auth } from '@/auth';
import { ChatEntity } from '@/lib/types';
import { VariableSizeList as List } from 'react-window';
import { cleanInputMessage } from '@/lib/messageUtils';
import AutoSizer from 'react-virtualized-auto-sizer';

export interface ChatSidebarListProps {
  chats: ChatEntity[];
  isAdminView?: boolean;
}

const getItemSize = (message: string, isAdminView?: boolean) => {
  if (message.length >= 45) return 116;
  else if (message.length >= 20) return 104;
  else return 88;
};

export default async function ChatSidebarList({
  chats,
  isAdminView,
}: ChatSidebarListProps) {
  return (
    <>
      {!isAdminView && (
        <div className="p-2">
          <ChatCardLayout link="/chat">
            <div className="overflow-hidden flex items-center size-full">
              <IconPlus className="w-1/4 font-bold" />
              <p className="text-sm w-3/4 ml-3 font-bold">New chat</p>
            </div>
          </ChatCardLayout>
        </div>
      )}
      <AutoSizer>
        {({ height, width }) => (
          <List
            itemData={chats}
            height={height}
            itemCount={chats.length}
            itemSize={index =>
              getItemSize(
                cleanInputMessage(chats[index].messages?.[0]?.content ?? ''),
                isAdminView,
              )
            }
            width={width}
          >
            {({ style, index, data }) => (
              <div
                style={style}
                className="px-2 flex items-center overflow-hidden"
              >
                <ChatCard
                  key={data[index].id}
                  chat={data[index]}
                  isAdminView={isAdminView}
                />
              </div>
            )}
          </List>
        )}
      </AutoSizer>
    </>
  );
}