@connectycube/react-ui-kit 0.0.16 → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/configs/dependencies.json +32 -3
- package/configs/imports.json +13 -2
- package/dist/index.cjs +4 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/types/components/alert-dialog.d.ts.map +1 -1
- package/dist/types/components/avatar.d.ts +2 -2
- package/dist/types/components/avatar.d.ts.map +1 -1
- package/dist/types/components/badge.d.ts +11 -0
- package/dist/types/components/badge.d.ts.map +1 -0
- package/dist/types/components/button.d.ts +3 -5
- package/dist/types/components/button.d.ts.map +1 -1
- package/dist/types/components/dialog-item.d.ts +46 -0
- package/dist/types/components/dialog-item.d.ts.map +1 -0
- package/dist/types/components/file-picker.d.ts +22 -0
- package/dist/types/components/file-picker.d.ts.map +1 -0
- package/dist/types/components/formatted-date.d.ts +8 -0
- package/dist/types/components/formatted-date.d.ts.map +1 -0
- package/dist/types/components/input.d.ts +5 -0
- package/dist/types/components/input.d.ts.map +1 -0
- package/dist/types/components/label.d.ts +5 -0
- package/dist/types/components/label.d.ts.map +1 -0
- package/dist/types/components/link-preview.d.ts +21 -0
- package/dist/types/components/link-preview.d.ts.map +1 -0
- package/dist/types/components/linkify-text.d.ts +9 -0
- package/dist/types/components/linkify-text.d.ts.map +1 -0
- package/dist/types/components/search.d.ts +15 -0
- package/dist/types/components/search.d.ts.map +1 -0
- package/dist/types/components/spinner.d.ts +12 -0
- package/dist/types/components/spinner.d.ts.map +1 -0
- package/dist/types/components/status-sent.d.ts +7 -0
- package/dist/types/components/status-sent.d.ts.map +1 -0
- package/dist/types/components/stream-view.d.ts.map +1 -1
- package/dist/types/components/utils.d.ts +0 -2
- package/dist/types/components/utils.d.ts.map +1 -1
- package/gen/components/alert-dialog.jsx +3 -1
- package/gen/components/avatar.jsx +13 -3
- package/gen/components/badge.jsx +45 -0
- package/gen/components/button.jsx +6 -9
- package/gen/components/dialog-item.jsx +149 -0
- package/gen/components/file-picker.jsx +200 -0
- package/gen/components/formatted-date.jsx +57 -0
- package/gen/components/input.jsx +23 -0
- package/gen/components/label.jsx +22 -0
- package/gen/components/link-preview.jsx +131 -0
- package/gen/components/linkify-text.jsx +31 -0
- package/gen/components/search.jsx +75 -0
- package/gen/components/spinner.jsx +36 -0
- package/gen/components/status-sent.jsx +21 -0
- package/gen/components/stream-view.jsx +5 -1
- package/gen/components/utils.js +0 -11
- package/package.json +5 -2
- package/src/components/alert-dialog.tsx +3 -1
- package/src/components/avatar.tsx +16 -6
- package/src/components/badge.tsx +42 -0
- package/src/components/button.tsx +12 -14
- package/src/components/connectycube-ui/avatar.jsx +54 -0
- package/src/components/connectycube-ui/avatar.tsx +77 -0
- package/src/components/connectycube-ui/badge.jsx +45 -0
- package/src/components/connectycube-ui/badge.tsx +42 -0
- package/src/components/connectycube-ui/dialog-item.jsx +149 -0
- package/src/components/connectycube-ui/dialog-item.tsx +188 -0
- package/src/components/connectycube-ui/file-picker.jsx +200 -0
- package/src/components/connectycube-ui/file-picker.tsx +231 -0
- package/src/components/connectycube-ui/formatted-date.jsx +57 -0
- package/src/components/connectycube-ui/formatted-date.tsx +57 -0
- package/src/components/connectycube-ui/label.jsx +22 -0
- package/src/components/connectycube-ui/label.tsx +23 -0
- package/src/components/connectycube-ui/linkify-text.tsx +40 -0
- package/src/components/connectycube-ui/presence.jsx +81 -0
- package/src/components/connectycube-ui/presence.tsx +96 -0
- package/src/components/connectycube-ui/status-sent.jsx +21 -0
- package/src/components/connectycube-ui/status-sent.tsx +25 -0
- package/src/components/connectycube-ui/utils.js +10 -0
- package/src/components/connectycube-ui/utils.ts +10 -0
- package/src/components/dialog-item.tsx +188 -0
- package/src/components/file-picker.tsx +231 -0
- package/src/components/formatted-date.tsx +57 -0
- package/src/components/input.tsx +26 -0
- package/src/components/label.tsx +23 -0
- package/src/components/link-preview.tsx +149 -0
- package/src/components/linkify-text.tsx +41 -0
- package/src/components/placeholder-text.tsx +1 -1
- package/src/components/search.tsx +86 -0
- package/src/components/spinner.tsx +42 -0
- package/src/components/status-sent.tsx +25 -0
- package/src/components/stream-view.tsx +9 -5
- package/src/components/utils.ts +0 -11
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/types/components/animated-loader.d.ts +0 -10
- package/dist/types/components/animated-loader.d.ts.map +0 -1
- package/gen/components/animated-loader.jsx +0 -12
- package/src/components/animated-loader.tsx +0 -16
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
import { forwardRef, memo } from 'react';
|
|
3
|
+
import { Users, type LucideProps } from 'lucide-react';
|
|
4
|
+
import { cn } from './utils';
|
|
5
|
+
import { Avatar, type AvatarProps } from './avatar';
|
|
6
|
+
import { type PresenceStatus } from './presence';
|
|
7
|
+
import { StatusSent, type StatusSentProps } from './status-sent';
|
|
8
|
+
import { FormattedDate, type FormattedDateProps } from './formatted-date';
|
|
9
|
+
import { Badge, type BadgeProps } from './badge';
|
|
10
|
+
|
|
11
|
+
interface DialogItemProps extends React.ComponentProps<'div'> {
|
|
12
|
+
index?: number;
|
|
13
|
+
isPrivateDialog?: boolean;
|
|
14
|
+
selected?: boolean;
|
|
15
|
+
onSelect?: () => void;
|
|
16
|
+
name: string;
|
|
17
|
+
photo?: string;
|
|
18
|
+
userOnline?: boolean;
|
|
19
|
+
userPresence?: PresenceStatus;
|
|
20
|
+
hasGroupIcon?: boolean;
|
|
21
|
+
lastSentStatus?: StatusSentProps['status'];
|
|
22
|
+
lastSentDate?: FormattedDateProps['date'];
|
|
23
|
+
lastSenderName?: string;
|
|
24
|
+
lastMessage?: string;
|
|
25
|
+
typingStatusText?: string;
|
|
26
|
+
draft?: string;
|
|
27
|
+
draftLabel?: string;
|
|
28
|
+
unreadCount?: number;
|
|
29
|
+
language?: string;
|
|
30
|
+
divider?: 'top' | 'bottom' | 'both' | 'none';
|
|
31
|
+
avatarProps?: AvatarProps;
|
|
32
|
+
contentProps?: React.ComponentProps<'div'>;
|
|
33
|
+
headerProps?: React.ComponentProps<'div'>;
|
|
34
|
+
headerLeftProps?: React.ComponentProps<'div'>;
|
|
35
|
+
groupIconProps?: LucideProps;
|
|
36
|
+
nameProps?: React.ComponentProps<'span'>;
|
|
37
|
+
headerRightProps?: React.ComponentProps<'div'>;
|
|
38
|
+
statusSentIconProps?: StatusSentProps;
|
|
39
|
+
formattedDateProps?: FormattedDateProps;
|
|
40
|
+
footerProps?: React.ComponentProps<'div'>;
|
|
41
|
+
lastMessageProps?: React.ComponentProps<'span'>;
|
|
42
|
+
draftLabelProps?: React.ComponentProps<'span'>;
|
|
43
|
+
lastSenderNameProps?: React.ComponentProps<'span'>;
|
|
44
|
+
unreadBadgeProps?: BadgeProps;
|
|
45
|
+
dividerProps?: React.ComponentProps<'div'>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function DialogItemBase(
|
|
49
|
+
{
|
|
50
|
+
index = -1,
|
|
51
|
+
isPrivateDialog,
|
|
52
|
+
selected,
|
|
53
|
+
onSelect = () => {},
|
|
54
|
+
name,
|
|
55
|
+
photo,
|
|
56
|
+
userOnline,
|
|
57
|
+
userPresence,
|
|
58
|
+
hasGroupIcon,
|
|
59
|
+
lastSentStatus,
|
|
60
|
+
lastSentDate,
|
|
61
|
+
lastSenderName,
|
|
62
|
+
lastMessage,
|
|
63
|
+
typingStatusText,
|
|
64
|
+
draft,
|
|
65
|
+
draftLabel = 'Draft',
|
|
66
|
+
unreadCount = 0,
|
|
67
|
+
language = 'en',
|
|
68
|
+
divider = 'bottom',
|
|
69
|
+
avatarProps,
|
|
70
|
+
contentProps,
|
|
71
|
+
headerProps,
|
|
72
|
+
headerLeftProps,
|
|
73
|
+
groupIconProps,
|
|
74
|
+
nameProps,
|
|
75
|
+
headerRightProps,
|
|
76
|
+
statusSentIconProps,
|
|
77
|
+
formattedDateProps,
|
|
78
|
+
footerProps,
|
|
79
|
+
lastMessageProps,
|
|
80
|
+
draftLabelProps,
|
|
81
|
+
lastSenderNameProps,
|
|
82
|
+
unreadBadgeProps,
|
|
83
|
+
dividerProps,
|
|
84
|
+
...props
|
|
85
|
+
}: DialogItemProps,
|
|
86
|
+
ref: React.ForwardedRef<HTMLDivElement>
|
|
87
|
+
) {
|
|
88
|
+
const avatarSource = photo || avatarProps?.src;
|
|
89
|
+
const avatarFallback = name || avatarProps?.name;
|
|
90
|
+
const avatarOnline = isPrivateDialog ? userOnline || avatarProps?.online : false;
|
|
91
|
+
const avatarPresence = isPrivateDialog ? userPresence || avatarProps?.presence : undefined;
|
|
92
|
+
const showTopDivider = divider === 'top' || (divider === 'both' && index === 0);
|
|
93
|
+
const showBottomDivider = divider === 'bottom' || divider === 'both';
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<div
|
|
97
|
+
ref={ref}
|
|
98
|
+
{...props}
|
|
99
|
+
onClick={onSelect}
|
|
100
|
+
className={cn(
|
|
101
|
+
'flex items-start gap-2 px-2 flex-1 cursor-pointer',
|
|
102
|
+
'transition-colors duration-200 ease-linear',
|
|
103
|
+
`${selected ? 'border-l-[0.25em] pl-1 border-l-ring bg-ring/20' : 'hover:bg-ring/5'}`,
|
|
104
|
+
props?.className
|
|
105
|
+
)}
|
|
106
|
+
>
|
|
107
|
+
<Avatar
|
|
108
|
+
src={avatarSource}
|
|
109
|
+
name={avatarFallback}
|
|
110
|
+
online={avatarOnline}
|
|
111
|
+
presence={avatarPresence}
|
|
112
|
+
{...avatarProps}
|
|
113
|
+
className={cn(`size-13 my-2 ${photo ? 'bg-ring/10' : 'bg-ring/30'}`, avatarProps?.className)}
|
|
114
|
+
/>
|
|
115
|
+
|
|
116
|
+
<div
|
|
117
|
+
{...contentProps}
|
|
118
|
+
className={cn('relative self-stretch flex-1 flex-col text-foreground', contentProps?.className)}
|
|
119
|
+
>
|
|
120
|
+
<div {...headerProps} className={cn('flex items-center justify-between gap-1', headerProps?.className)}>
|
|
121
|
+
<div {...headerLeftProps} className={cn('flex items-center gap-1', headerLeftProps?.className)}>
|
|
122
|
+
{!isPrivateDialog && hasGroupIcon && (
|
|
123
|
+
<Users
|
|
124
|
+
{...groupIconProps}
|
|
125
|
+
className={cn('text-muted-foreground size-4 min-w-4', groupIconProps?.className)}
|
|
126
|
+
/>
|
|
127
|
+
)}
|
|
128
|
+
<span {...nameProps} className={cn('font-medium text-left line-clamp-1', nameProps?.className)}>
|
|
129
|
+
{name}
|
|
130
|
+
</span>
|
|
131
|
+
</div>
|
|
132
|
+
<div {...headerRightProps} className={cn('flex items-center gap-1', headerRightProps?.className)}>
|
|
133
|
+
<StatusSent status={lastSentStatus} {...statusSentIconProps} />
|
|
134
|
+
<FormattedDate date={lastSentDate} language={language} {...formattedDateProps} />
|
|
135
|
+
</div>
|
|
136
|
+
</div>
|
|
137
|
+
<div {...footerProps} className={cn('flex items-start justify-between gap-1', footerProps?.className)}>
|
|
138
|
+
<span
|
|
139
|
+
{...lastMessageProps}
|
|
140
|
+
className={cn('text-sm text-left text-muted-foreground line-clamp-2', lastMessageProps?.className)}
|
|
141
|
+
>
|
|
142
|
+
{typingStatusText ||
|
|
143
|
+
(draft ? (
|
|
144
|
+
<span>
|
|
145
|
+
<span
|
|
146
|
+
{...draftLabelProps}
|
|
147
|
+
className={cn('text-red-500 font-medium', draftLabelProps?.className)}
|
|
148
|
+
>{`${draftLabel}: `}</span>
|
|
149
|
+
{draft}
|
|
150
|
+
</span>
|
|
151
|
+
) : (
|
|
152
|
+
<span>
|
|
153
|
+
{lastSenderName && (
|
|
154
|
+
<span
|
|
155
|
+
{...lastSenderNameProps}
|
|
156
|
+
className={cn('font-semibold', lastSenderNameProps?.className)}
|
|
157
|
+
>{`${lastSenderName}: `}</span>
|
|
158
|
+
)}
|
|
159
|
+
{lastMessage}
|
|
160
|
+
</span>
|
|
161
|
+
))}
|
|
162
|
+
</span>
|
|
163
|
+
{unreadCount > 0 && (
|
|
164
|
+
<Badge
|
|
165
|
+
{...unreadBadgeProps}
|
|
166
|
+
className={cn(
|
|
167
|
+
'bg-ring text-background text-xs rounded-full p-1 h-6 min-w-6 mt-0.5',
|
|
168
|
+
unreadBadgeProps?.className
|
|
169
|
+
)}
|
|
170
|
+
>
|
|
171
|
+
{unreadCount}
|
|
172
|
+
</Badge>
|
|
173
|
+
)}
|
|
174
|
+
</div>
|
|
175
|
+
{showTopDivider && (
|
|
176
|
+
<div {...dividerProps} className={cn('absolute -top-px left-0 right-0 border-t', dividerProps?.className)} />
|
|
177
|
+
)}
|
|
178
|
+
{showBottomDivider && <div className="absolute bottom-0 left-0 right-0 border-b" />}
|
|
179
|
+
</div>
|
|
180
|
+
</div>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const DialogItem = memo(forwardRef<HTMLDivElement, DialogItemProps>(DialogItemBase));
|
|
185
|
+
|
|
186
|
+
DialogItem.displayName = 'DialogItem';
|
|
187
|
+
|
|
188
|
+
export { DialogItem };
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { forwardRef, useState } from 'react';
|
|
2
|
+
import { FilePlusCorner, Paperclip } from 'lucide-react';
|
|
3
|
+
import { Label } from './label';
|
|
4
|
+
import { cn } from './utils';
|
|
5
|
+
|
|
6
|
+
const areValidFiles = (files, accept) => {
|
|
7
|
+
if (files.length === 0) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const acceptList =
|
|
12
|
+
typeof accept === 'string' ? accept.split(/\s*,\s*|\s+/).map((type) => type.toLowerCase().trim()) : null;
|
|
13
|
+
|
|
14
|
+
if (acceptList === null || acceptList.length === 0) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return files.every((file) => {
|
|
19
|
+
const { type, size } = file;
|
|
20
|
+
|
|
21
|
+
if (typeof type !== 'string' || typeof size !== 'number' || size <= 0 || type.length === 0) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const fileMimeType = type.toLowerCase();
|
|
26
|
+
const [fileType, fileSubtype] = fileMimeType.split('/');
|
|
27
|
+
|
|
28
|
+
return acceptList.some(
|
|
29
|
+
(acceptType) =>
|
|
30
|
+
acceptType.startsWith('*') ||
|
|
31
|
+
acceptType === fileMimeType ||
|
|
32
|
+
(fileType && acceptType.endsWith('/*') && acceptType.startsWith(fileType)) ||
|
|
33
|
+
(acceptType.startsWith('.') && acceptType.slice(1) === fileSubtype)
|
|
34
|
+
);
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
const handleFiles = (fileList, accept, multiple, onSelectFile, onInvalidFile) => {
|
|
38
|
+
const files = Array.from(fileList || []).filter(Boolean);
|
|
39
|
+
|
|
40
|
+
if (files.length === 0) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const validated = areValidFiles(files, accept);
|
|
45
|
+
|
|
46
|
+
if (validated) {
|
|
47
|
+
onSelectFile(multiple ? files : files[0] ? [files[0]] : []);
|
|
48
|
+
} else {
|
|
49
|
+
onInvalidFile();
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function FilePickerInputBase(
|
|
54
|
+
{
|
|
55
|
+
onSelectFile = () => {},
|
|
56
|
+
onInvalidFile = () => {},
|
|
57
|
+
multiple = false,
|
|
58
|
+
accept = '*/*',
|
|
59
|
+
iconElement,
|
|
60
|
+
labelProps,
|
|
61
|
+
iconProps,
|
|
62
|
+
...props
|
|
63
|
+
},
|
|
64
|
+
ref
|
|
65
|
+
) {
|
|
66
|
+
const handleChange = (event) => {
|
|
67
|
+
handleFiles(event.currentTarget.files, accept, multiple, onSelectFile, onInvalidFile);
|
|
68
|
+
event.currentTarget.value = '';
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<Label
|
|
73
|
+
{...labelProps}
|
|
74
|
+
htmlFor="file-uploader"
|
|
75
|
+
className={cn(
|
|
76
|
+
'group p-2 rounded-full hover:bg-ring/10 transition-all duration-200 ease-in-out cursor-pointer',
|
|
77
|
+
labelProps?.className
|
|
78
|
+
)}
|
|
79
|
+
>
|
|
80
|
+
{iconElement || (
|
|
81
|
+
<Paperclip
|
|
82
|
+
{...iconProps}
|
|
83
|
+
className={cn(
|
|
84
|
+
'text-foreground group-hover:scale-110 transition-all duration-200 ease-in-out',
|
|
85
|
+
iconProps?.className
|
|
86
|
+
)}
|
|
87
|
+
/>
|
|
88
|
+
)}
|
|
89
|
+
<input
|
|
90
|
+
ref={ref}
|
|
91
|
+
id="file-uploader"
|
|
92
|
+
type="file"
|
|
93
|
+
multiple={multiple}
|
|
94
|
+
accept={accept}
|
|
95
|
+
onChange={handleChange}
|
|
96
|
+
{...props}
|
|
97
|
+
className={cn('hidden', props?.className)}
|
|
98
|
+
/>
|
|
99
|
+
</Label>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const FilePickerInput = forwardRef(FilePickerInputBase);
|
|
104
|
+
|
|
105
|
+
FilePickerInput.displayName = 'FilePickerInput';
|
|
106
|
+
|
|
107
|
+
function FilePickerDropzoneBase(
|
|
108
|
+
{
|
|
109
|
+
onSelectFile = () => {},
|
|
110
|
+
onInvalidFile = () => {},
|
|
111
|
+
multiple = false,
|
|
112
|
+
accept = '*/*',
|
|
113
|
+
children,
|
|
114
|
+
iconElement,
|
|
115
|
+
placeholder,
|
|
116
|
+
dropZoneProps,
|
|
117
|
+
placeholderContainerProps,
|
|
118
|
+
iconProps,
|
|
119
|
+
placeholderProps,
|
|
120
|
+
...props
|
|
121
|
+
},
|
|
122
|
+
ref
|
|
123
|
+
) {
|
|
124
|
+
const [isDragging, setIsDragging] = useState(false);
|
|
125
|
+
const handleDragEvent = (event) => {
|
|
126
|
+
event.preventDefault();
|
|
127
|
+
event.stopPropagation();
|
|
128
|
+
};
|
|
129
|
+
const handleDrop = (event) => {
|
|
130
|
+
handleDragEvent(event);
|
|
131
|
+
setIsDragging(false);
|
|
132
|
+
handleFiles(event.dataTransfer.files, accept, multiple, onSelectFile, onInvalidFile);
|
|
133
|
+
event.dataTransfer.clearData();
|
|
134
|
+
};
|
|
135
|
+
const handleDragEnter = (event) => {
|
|
136
|
+
handleDragEvent(event);
|
|
137
|
+
|
|
138
|
+
if (event.dataTransfer.items && event.dataTransfer.items.length > 0) {
|
|
139
|
+
setIsDragging(true);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
const handleDragLeave = (event) => {
|
|
143
|
+
if (!event.currentTarget.contains(event.relatedTarget)) {
|
|
144
|
+
setIsDragging(false);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
return (
|
|
149
|
+
<div
|
|
150
|
+
ref={ref}
|
|
151
|
+
onDragEnter={handleDragEnter}
|
|
152
|
+
onDragLeave={handleDragLeave}
|
|
153
|
+
onDragOver={handleDragEvent}
|
|
154
|
+
{...props}
|
|
155
|
+
className={cn('size-full relative', props?.className)}
|
|
156
|
+
>
|
|
157
|
+
{children}
|
|
158
|
+
<div
|
|
159
|
+
onDrop={handleDrop}
|
|
160
|
+
{...dropZoneProps}
|
|
161
|
+
className={cn(
|
|
162
|
+
'group absolute top-0 left-0 size-full',
|
|
163
|
+
'flex items-center justify-center',
|
|
164
|
+
'transition-all duration-300 ease-out',
|
|
165
|
+
'border-2 border-dashed border-ring bg-ring/25 rounded-md',
|
|
166
|
+
isDragging
|
|
167
|
+
? 'opacity-100 pointer-events-auto visible scale-100'
|
|
168
|
+
: 'opacity-0 pointer-events-none invisible scale-98',
|
|
169
|
+
dropZoneProps?.className
|
|
170
|
+
)}
|
|
171
|
+
>
|
|
172
|
+
<div
|
|
173
|
+
{...placeholderContainerProps}
|
|
174
|
+
className={cn(
|
|
175
|
+
'bg-muted flex flex-row items-center gap-2 rounded-full py-2 px-4 transition-all',
|
|
176
|
+
isDragging ? 'scale-100' : 'scale-90',
|
|
177
|
+
placeholderContainerProps?.className
|
|
178
|
+
)}
|
|
179
|
+
>
|
|
180
|
+
{iconElement || (
|
|
181
|
+
<FilePlusCorner
|
|
182
|
+
absoluteStrokeWidth
|
|
183
|
+
{...iconProps}
|
|
184
|
+
className={cn('text-ring size-6', iconProps?.className)}
|
|
185
|
+
/>
|
|
186
|
+
)}
|
|
187
|
+
<span {...placeholderProps} className={cn('text-ring text-md font-bold', placeholderProps?.className)}>
|
|
188
|
+
{placeholder || 'Drop files here'}
|
|
189
|
+
</span>
|
|
190
|
+
</div>
|
|
191
|
+
</div>
|
|
192
|
+
</div>
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const FilePickerDropzone = forwardRef(FilePickerDropzoneBase);
|
|
197
|
+
|
|
198
|
+
FilePickerDropzone.displayName = 'FilePickerDropzone';
|
|
199
|
+
|
|
200
|
+
export { FilePickerInput, FilePickerDropzone, FilePickerInput as Input, FilePickerDropzone as Dropzone };
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
import { forwardRef, useState } from 'react';
|
|
3
|
+
import { FilePlusCorner, LucideProps, Paperclip } from 'lucide-react';
|
|
4
|
+
import { Label, type LabelProps } from './label';
|
|
5
|
+
import { cn } from './utils';
|
|
6
|
+
|
|
7
|
+
const areValidFiles = (files: File[], accept?: string): boolean => {
|
|
8
|
+
if (files.length === 0) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const acceptList =
|
|
13
|
+
typeof accept === 'string' ? accept.split(/\s*,\s*|\s+/).map((type) => type.toLowerCase().trim()) : null;
|
|
14
|
+
|
|
15
|
+
if (acceptList === null || acceptList.length === 0) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return files.every((file) => {
|
|
20
|
+
const { type, size } = file;
|
|
21
|
+
|
|
22
|
+
if (typeof type !== 'string' || typeof size !== 'number' || size <= 0 || type.length === 0) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const fileMimeType = type.toLowerCase();
|
|
27
|
+
const [fileType, fileSubtype] = fileMimeType.split('/');
|
|
28
|
+
|
|
29
|
+
return acceptList.some(
|
|
30
|
+
(acceptType) =>
|
|
31
|
+
acceptType.startsWith('*') ||
|
|
32
|
+
acceptType === fileMimeType ||
|
|
33
|
+
(fileType && acceptType.endsWith('/*') && acceptType.startsWith(fileType)) ||
|
|
34
|
+
(acceptType.startsWith('.') && acceptType.slice(1) === fileSubtype)
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
const handleFiles = (
|
|
39
|
+
fileList: FileList | null,
|
|
40
|
+
accept: string,
|
|
41
|
+
multiple: boolean,
|
|
42
|
+
onSelectFile: (files: File[]) => void,
|
|
43
|
+
onInvalidFile: () => void
|
|
44
|
+
) => {
|
|
45
|
+
const files = Array.from(fileList || []).filter(Boolean);
|
|
46
|
+
|
|
47
|
+
if (files.length === 0) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const validated = areValidFiles(files, accept);
|
|
52
|
+
|
|
53
|
+
if (validated) {
|
|
54
|
+
onSelectFile(multiple ? files : files[0] ? [files[0]] : []);
|
|
55
|
+
} else {
|
|
56
|
+
onInvalidFile();
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
interface FilePickerInputProps extends React.ComponentProps<'input'> {
|
|
61
|
+
onSelectFile: (files: File[]) => void;
|
|
62
|
+
onInvalidFile?: () => void;
|
|
63
|
+
iconElement?: React.ReactNode;
|
|
64
|
+
labelProps?: LabelProps;
|
|
65
|
+
iconProps?: LucideProps;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
interface FilePickerDropzoneProps extends FilePickerInputProps {
|
|
69
|
+
children?: React.ReactNode;
|
|
70
|
+
placeholder?: string;
|
|
71
|
+
dropZoneProps?: React.ComponentProps<'div'>;
|
|
72
|
+
placeholderContainerProps?: React.ComponentProps<'div'>;
|
|
73
|
+
iconProps?: LucideProps;
|
|
74
|
+
placeholderProps?: React.ComponentProps<'span'>;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function FilePickerInputBase(
|
|
78
|
+
{
|
|
79
|
+
onSelectFile = () => {},
|
|
80
|
+
onInvalidFile = () => {},
|
|
81
|
+
multiple = false,
|
|
82
|
+
accept = '*/*',
|
|
83
|
+
iconElement,
|
|
84
|
+
labelProps,
|
|
85
|
+
iconProps,
|
|
86
|
+
...props
|
|
87
|
+
}: FilePickerInputProps,
|
|
88
|
+
ref?: React.ForwardedRef<HTMLInputElement>
|
|
89
|
+
) {
|
|
90
|
+
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
91
|
+
handleFiles(event.currentTarget.files, accept, multiple, onSelectFile, onInvalidFile);
|
|
92
|
+
event.currentTarget.value = '';
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<Label
|
|
97
|
+
{...labelProps}
|
|
98
|
+
htmlFor="file-uploader"
|
|
99
|
+
className={cn(
|
|
100
|
+
'group p-2 rounded-full hover:bg-ring/10 transition-all duration-200 ease-in-out cursor-pointer',
|
|
101
|
+
labelProps?.className
|
|
102
|
+
)}
|
|
103
|
+
>
|
|
104
|
+
{iconElement || (
|
|
105
|
+
<Paperclip
|
|
106
|
+
{...iconProps}
|
|
107
|
+
className={cn(
|
|
108
|
+
'text-foreground group-hover:scale-110 transition-all duration-200 ease-in-out',
|
|
109
|
+
iconProps?.className
|
|
110
|
+
)}
|
|
111
|
+
/>
|
|
112
|
+
)}
|
|
113
|
+
<input
|
|
114
|
+
ref={ref}
|
|
115
|
+
id="file-uploader"
|
|
116
|
+
type="file"
|
|
117
|
+
multiple={multiple}
|
|
118
|
+
accept={accept}
|
|
119
|
+
onChange={handleChange}
|
|
120
|
+
{...props}
|
|
121
|
+
className={cn('hidden', props?.className)}
|
|
122
|
+
/>
|
|
123
|
+
</Label>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const FilePickerInput = forwardRef<HTMLInputElement, FilePickerInputProps>(FilePickerInputBase);
|
|
128
|
+
|
|
129
|
+
FilePickerInput.displayName = 'FilePickerInput';
|
|
130
|
+
|
|
131
|
+
function FilePickerDropzoneBase(
|
|
132
|
+
{
|
|
133
|
+
onSelectFile = () => {},
|
|
134
|
+
onInvalidFile = () => {},
|
|
135
|
+
multiple = false,
|
|
136
|
+
accept = '*/*',
|
|
137
|
+
children,
|
|
138
|
+
iconElement,
|
|
139
|
+
placeholder,
|
|
140
|
+
dropZoneProps,
|
|
141
|
+
placeholderContainerProps,
|
|
142
|
+
iconProps,
|
|
143
|
+
placeholderProps,
|
|
144
|
+
...props
|
|
145
|
+
}: FilePickerDropzoneProps,
|
|
146
|
+
ref: React.ForwardedRef<HTMLDivElement>
|
|
147
|
+
) {
|
|
148
|
+
const [isDragging, setIsDragging] = useState(false);
|
|
149
|
+
const handleDragEvent = (event: React.DragEvent<HTMLDivElement>) => {
|
|
150
|
+
event.preventDefault();
|
|
151
|
+
event.stopPropagation();
|
|
152
|
+
};
|
|
153
|
+
const handleDrop = (event: React.DragEvent<HTMLDivElement>) => {
|
|
154
|
+
handleDragEvent(event);
|
|
155
|
+
setIsDragging(false);
|
|
156
|
+
handleFiles(event.dataTransfer.files, accept, multiple, onSelectFile, onInvalidFile);
|
|
157
|
+
event.dataTransfer.clearData();
|
|
158
|
+
};
|
|
159
|
+
const handleDragEnter = (event: React.DragEvent<HTMLDivElement>) => {
|
|
160
|
+
handleDragEvent(event);
|
|
161
|
+
|
|
162
|
+
if (event.dataTransfer.items && event.dataTransfer.items.length > 0) {
|
|
163
|
+
setIsDragging(true);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
const handleDragLeave = (event: React.DragEvent<HTMLDivElement>) => {
|
|
167
|
+
if (!event.currentTarget.contains(event.relatedTarget as Node)) {
|
|
168
|
+
setIsDragging(false);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
return (
|
|
173
|
+
<div
|
|
174
|
+
ref={ref}
|
|
175
|
+
onDragEnter={handleDragEnter}
|
|
176
|
+
onDragLeave={handleDragLeave}
|
|
177
|
+
onDragOver={handleDragEvent}
|
|
178
|
+
{...props}
|
|
179
|
+
className={cn('size-full relative', props?.className)}
|
|
180
|
+
>
|
|
181
|
+
{children}
|
|
182
|
+
<div
|
|
183
|
+
onDrop={handleDrop}
|
|
184
|
+
{...dropZoneProps}
|
|
185
|
+
className={cn(
|
|
186
|
+
'group absolute top-0 left-0 size-full',
|
|
187
|
+
'flex items-center justify-center',
|
|
188
|
+
'transition-all duration-300 ease-out',
|
|
189
|
+
'border-2 border-dashed border-ring bg-ring/25 rounded-md',
|
|
190
|
+
isDragging
|
|
191
|
+
? 'opacity-100 pointer-events-auto visible scale-100'
|
|
192
|
+
: 'opacity-0 pointer-events-none invisible scale-98',
|
|
193
|
+
dropZoneProps?.className
|
|
194
|
+
)}
|
|
195
|
+
>
|
|
196
|
+
<div
|
|
197
|
+
{...placeholderContainerProps}
|
|
198
|
+
className={cn(
|
|
199
|
+
'bg-muted flex flex-row items-center gap-2 rounded-full py-2 px-4 transition-all',
|
|
200
|
+
isDragging ? 'scale-100' : 'scale-90',
|
|
201
|
+
placeholderContainerProps?.className
|
|
202
|
+
)}
|
|
203
|
+
>
|
|
204
|
+
{iconElement || (
|
|
205
|
+
<FilePlusCorner
|
|
206
|
+
absoluteStrokeWidth
|
|
207
|
+
{...iconProps}
|
|
208
|
+
className={cn('text-ring size-6', iconProps?.className)}
|
|
209
|
+
/>
|
|
210
|
+
)}
|
|
211
|
+
<span {...placeholderProps} className={cn('text-ring text-md font-bold', placeholderProps?.className)}>
|
|
212
|
+
{placeholder || 'Drop files here'}
|
|
213
|
+
</span>
|
|
214
|
+
</div>
|
|
215
|
+
</div>
|
|
216
|
+
</div>
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const FilePickerDropzone = forwardRef<HTMLDivElement, FilePickerDropzoneProps>(FilePickerDropzoneBase);
|
|
221
|
+
|
|
222
|
+
FilePickerDropzone.displayName = 'FilePickerDropzone';
|
|
223
|
+
|
|
224
|
+
export {
|
|
225
|
+
FilePickerInput,
|
|
226
|
+
FilePickerDropzone,
|
|
227
|
+
FilePickerInput as Input,
|
|
228
|
+
FilePickerDropzone as Dropzone,
|
|
229
|
+
type FilePickerInputProps,
|
|
230
|
+
type FilePickerDropzoneProps,
|
|
231
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { forwardRef, memo } from 'react';
|
|
2
|
+
import { differenceInCalendarDays, format, formatDistanceToNow, isToday } from 'date-fns';
|
|
3
|
+
import { el, enUS, uk } from 'date-fns/locale';
|
|
4
|
+
import { cn } from './utils';
|
|
5
|
+
|
|
6
|
+
const locales = {
|
|
7
|
+
en: enUS,
|
|
8
|
+
el: el,
|
|
9
|
+
ua: uk,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
function formatDate(date, language = 'en', distanceToNow = false) {
|
|
13
|
+
const locale = locales[language] ?? enUS;
|
|
14
|
+
|
|
15
|
+
if (distanceToNow) {
|
|
16
|
+
return formatDistanceToNow(date, {
|
|
17
|
+
locale,
|
|
18
|
+
addSuffix: true,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const isWithinLast7Days = (date) => {
|
|
23
|
+
const diff = differenceInCalendarDays(new Date(), date);
|
|
24
|
+
|
|
25
|
+
return diff >= 0 && diff <= 6;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
return isToday(date)
|
|
29
|
+
? format(date, 'p', {
|
|
30
|
+
locale,
|
|
31
|
+
})
|
|
32
|
+
: isWithinLast7Days(date)
|
|
33
|
+
? format(date, 'eeee', {
|
|
34
|
+
locale,
|
|
35
|
+
})
|
|
36
|
+
: format(date, 'P', {
|
|
37
|
+
locale,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function FormattedDateBase({ date, language, distanceToNow, ...props }, ref) {
|
|
42
|
+
if (!date) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<span ref={ref} {...props} className={cn('text-xs text-muted-foreground', props?.className)}>
|
|
48
|
+
{formatDate(date, language, distanceToNow)}
|
|
49
|
+
</span>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const FormattedDate = memo(forwardRef(FormattedDateBase));
|
|
54
|
+
|
|
55
|
+
FormattedDate.displayName = 'FormattedDate';
|
|
56
|
+
|
|
57
|
+
export { FormattedDate };
|