Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add file type filtering dropdown to improve file navigation #718

Merged
merged 4 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions packages/api/src/EmbeddedChatApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,21 +732,22 @@ export default class EmbeddedChatApi {
}
}

async getAllFiles(isChannelPrivate = false) {
async getAllFiles(isChannelPrivate = false, typeGroup: string) {
SinghaAnirban005 marked this conversation as resolved.
Show resolved Hide resolved
const roomType = isChannelPrivate ? "groups" : "channels";
try {
const { userId, authToken } = (await this.auth.getCurrentUser()) || {};
const response = await fetch(
`${this.host}/api/v1/${roomType}.files?roomId=${this.rid}`,
{
headers: {
"Content-Type": "application/json",
"X-Auth-Token": authToken,
"X-User-Id": userId,
},
method: "GET",
}
);
const url =
typeGroup === ""
? `${this.host}/api/v1/${roomType}.files?roomId=${this.rid}`
: `${this.host}/api/v1/${roomType}.files?roomId=${this.rid}&typeGroup=${typeGroup}`;
const response = await fetch(url, {
headers: {
"Content-Type": "application/json",
"X-Auth-Token": authToken,
"X-User-Id": userId,
},
method: "GET",
});
return await response.json();
} catch (err) {
console.error(err);
Expand Down
34 changes: 33 additions & 1 deletion packages/react/src/views/MessageAggregators/FileGallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ const FileGallery = () => {
const [text, setText] = useState('');
const [isFetching, setIsFetching] = useState(true);
const [files, setFiles] = useState([]);
const [selectedFilter, setSelectedFilter] = useState('all');

const options = [
{ value: 'all', label: 'All' },
{ value: 'application', label: 'Files' },
{ value: 'video', label: 'Videos' },
{ value: 'image', label: 'Images' },
{ value: 'audio', label: 'Audios' },
{ value: 'text', label: 'Texts' },
];

const handleInputChange = (e) => {
setText(e.target.value);
Expand All @@ -30,7 +40,7 @@ const FileGallery = () => {

useEffect(() => {
const fetchAllFiles = async () => {
const res = await RCInstance.getAllFiles(isChannelPrivate);
const res = await RCInstance.getAllFiles(isChannelPrivate, '');
if (res?.files) {
const sortedFiles = res.files.sort(
(a, b) => new Date(b.uploadedAt) - new Date(a.uploadedAt)
Expand All @@ -42,11 +52,33 @@ const FileGallery = () => {
fetchAllFiles();
}, [RCInstance, isChannelPrivate, messages]);

const handleFilterSelect = async (val) => {
setIsFetching(true);
setSelectedFilter(val);
let res;
val === 'all'
? (res = await RCInstance.getAllFiles(isChannelPrivate, ''))
: (res = await RCInstance.getAllFiles(isChannelPrivate, val));
if (res?.files) {
const sortedFiles = res.files.sort(
(a, b) => new Date(b.uploadedAt) - new Date(a.uploadedAt)
);
setFiles(sortedFiles);
setIsFetching(false);
}
};

return (
<MessageAggregator
title="Files"
iconName="attachment"
noMessageInfo="No Files Found"
filterProps={{
isFile: true,
options,
value: selectedFilter,
handleFilterSelect,
}}
searchProps={{
isSearch: true,
handleInputChange,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const MessageAggregator = ({
noMessageInfo,
shouldRender,
fetchedMessageList,
filterProps,
searchProps,
searchFiltered,
fetching,
Expand Down Expand Up @@ -112,6 +113,7 @@ export const MessageAggregator = ({
<ViewComponent
title={title}
iconName={iconName}
filterProps={filterProps}
searchProps={searchProps}
onClose={() => setExclusiveState(null)}
style={{
Expand Down
5 changes: 4 additions & 1 deletion packages/ui-elements/src/components/Sidebar/Sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Sidebar = ({
iconName,
onClose,
children,
filterProps = {},
searchProps = {},
footer,
style = {},
Expand All @@ -26,7 +27,9 @@ const Sidebar = ({
style={{ ...style, ...styleOverrides }}
>
<SidebarHeader title={title} iconName={iconName} onClose={onClose} />
<SidebarContent searchProps={searchProps}>{children}</SidebarContent>
<SidebarContent searchProps={searchProps} filterProps={filterProps}>
{children}
</SidebarContent>
{footer && <SidebarFooter>{footer}</SidebarFooter>}
</Box>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ export const getSidebarContentStyles = (theme) => {
padding: 0 0.5rem;
border-radius: ${theme.radius};
position: relative;
margin: 0 1rem 1rem;
&.focused {
outline: 1px solid ${theme.colors.ring};
}
`,
filesHeader: css`
display: flex;
align-items: center;
justify-content: space-between;
margin: 1px 1rem 0;
`,

textInput: css`
border: none;
Expand Down
64 changes: 44 additions & 20 deletions packages/ui-elements/src/components/Sidebar/SidebarContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ import { Icon } from '../Icon';
import { Input } from '../Input';
import { getSidebarContentStyles } from './Sidebar.styles';
import { useTheme } from '../../hooks';
import { StaticSelect } from '../StaticSelect';

const SidebarContent = ({ children, searchProps = {}, style }) => {
const SidebarContent = ({
children,
searchProps = {},
style,
filterProps = {},
}) => {
const {
isSearch = false,
handleInputChange,
placeholder,
} = searchProps || {};
const { isFile, options, value, handleFilterSelect } = filterProps || {};
const searchContainerRef = useRef(null);
const { theme } = useTheme();
const styles = getSidebarContentStyles(theme);
Expand All @@ -29,25 +36,42 @@ const SidebarContent = ({ children, searchProps = {}, style }) => {

return (
<Box css={styles.content} style={style}>
{isSearch && (
<Box
css={styles.searchContainer}
style={{
position: 'relative',
margin: '0.5rem',
}}
ref={searchContainerRef}
>
<Input
placeholder={placeholder}
onChange={handleInputChange}
css={styles.textInput}
onFocus={handleFocus}
onBlur={handleBlur}
/>
<Icon name="magnifier" size="1.25rem" css={styles.noInfoIcon} />
</Box>
)}
<Box css={styles.filesHeader}>
{isSearch && (
<Box
css={styles.searchContainer}
style={{
position: 'relative',
marginBottom: '0.5rem',
width: isFile ? '60%' : '100%',
}}
ref={searchContainerRef}
>
<Input
placeholder={placeholder}
onChange={handleInputChange}
css={styles.textInput}
onFocus={handleFocus}
onBlur={handleBlur}
/>
<Icon name="magnifier" size="1.25rem" css={styles.noInfoIcon} />
</Box>
)}
{isFile && (
<Box>
<StaticSelect
style={{
position: 'relative',
marginBottom: '0.5rem',
}}
isFile={isFile}
options={options}
value={value}
onSelect={handleFilterSelect}
/>
</Box>
)}
</Box>
{children}
</Box>
);
Expand Down
35 changes: 26 additions & 9 deletions packages/ui-elements/src/components/StaticSelect/StaticSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const StaticSelect = ({
style = {},
options = [],
placeholder = '',
isFile,
value,
onSelect,
disabled = false,
Expand All @@ -22,27 +23,34 @@ const StaticSelect = ({
const styles = getStaticSelectStyles(theme);

const [isOpen, setIsOpen] = useState(false);
const [internalValue, setInternalValue] = useState('');
const [internalValue, setInternalValue] = useState(value || '');
const [selectedOption, setSelectedOption] = useState(null);
const staticSelectRef = useRef(null);

useEffect(() => {
setInternalValue(value || '');
const option = options.find((opt) => opt.value === value);
if (option) {
setSelectedOption(option);
}
}, [value, options]);

const toggleDropdown = () => {
if (!disabled) {
setIsOpen(!isOpen);
}
};

const handleSelect = (optionValue) => {
const selectedOpt = options.find((opt) => opt.value === optionValue);
setInternalValue(optionValue);
setSelectedOption(selectedOpt);
setIsOpen(false);
if (onSelect) {
onSelect(optionValue);
}
};

useEffect(() => {
setInternalValue(value || '');
}, [value]);

useEffect(() => {
const handleClickOutside = (event) => {
if (
Expand All @@ -61,6 +69,8 @@ const StaticSelect = ({
};
}, [isOpen]);

const displayValue = selectedOption?.label || placeholder;

return (
<Box
className={`ec-static-select ${className} ${classNames}`}
Expand All @@ -78,20 +88,27 @@ const StaticSelect = ({
{...props}
>
<Box is="span" className="selected-option">
{!isOpen && internalValue
? options.find((option) => option.value === internalValue)?.label
: placeholder}
{displayValue}
</Box>
<Icon name="chevron-down" />
</Box>

{isOpen && (
{isOpen && !isFile && (
<ListBox
options={options}
onSelect={handleSelect}
value={internalValue}
/>
)}
{isOpen && isFile && (
<Box css={styles.fileTypeSelect}>
<ListBox
options={options}
onSelect={handleSelect}
value={internalValue}
/>
</Box>
)}
</Box>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ const getStaticSelectStyles = (theme) => {
cursor: not-allowed !important;
color: ${theme.colors.mutedForeground};
`,

fileTypeSelect: css`
position: absolute;
z-index: 10;
top: 100%;
left: 0;
width: 100%;
background-color: white;
`,
};

return styles;
Expand Down
Loading