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

add mimetype #309

Merged
merged 1 commit into from
Sep 11, 2023
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
Binary file removed ui/public/mime/applause.mp3
Binary file not shown.
400 changes: 400 additions & 0 deletions ui/public/mime/data.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ui/src/components/General/CodeHighlightWithCopy/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const CodeHighlightWithCopy: React.FC<CodeHighlightWithCopyPropsType> = ({
wrapLines
language={language}
style={isDarkMode ? obsidian : stackoverflowLight}
customStyle={{ padding: "20px 0" }}
customStyle={{ padding: "20px" }}
>
{codeString}
</SyntaxHighlighter>
Expand Down
42 changes: 42 additions & 0 deletions ui/src/pages/info/Mimetype/components/MimeSearchResult.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Card, Table } from "antd";
import React from "react";
import MimeSearch from "./SearchBar";
import { MIME_COLUMNS } from "../utils/constants";
import { MimeSearchResultPropsType } from "../utils/types";
import { useSearchParams } from "react-router-dom";
import { filteredMimeType } from "../utils/helper";

const MimeSearchResult: React.FC<MimeSearchResultPropsType> = ({
data,
isError,
isLoading,
}) => {
const [searchParams] = useSearchParams();

const searchQuery = searchParams.get("type") || "";

if (isError) {
return <div>Something went wrong!</div>;
}

const list = data
? searchQuery
? filteredMimeType(data, searchQuery)
: data
: [];
return (
<Card>
<MimeSearch />
<Table
columns={MIME_COLUMNS}
dataSource={list}
pagination={false}
bordered
scroll={{ x: "calc(50%)" }}
loading={isLoading}
/>
</Card>
);
};

export default MimeSearchResult;
36 changes: 36 additions & 0 deletions ui/src/pages/info/Mimetype/components/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useSearchParams } from "react-router-dom";
import { useState, useEffect, ChangeEvent } from "react";
import { ResponsiveInputWithLabel } from "components/General/FormComponents";
import { Form } from "antd";

const MimeSearch: React.FC = () => {
const [searchParams, setSearchParams] = useSearchParams();
const [queryParams, setQueryParams] = useState({
q: searchParams.get("type") || "",
});

const { q: searchQuery } = queryParams;

useEffect(() => {
setSearchParams(`?type=${searchQuery}`);
}, [searchQuery, setSearchParams]);

const handleSearchChange = (e: ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;
setQueryParams((prevParams) => ({ ...prevParams, q: value }));
};

return (
<Form layout="vertical">
<ResponsiveInputWithLabel
label="Search bar"
type="text"
placeholder="Search by Mimetype..."
value={searchQuery}
onChange={handleSearchChange}
/>
</Form>
);
};

export default MimeSearch;
21 changes: 9 additions & 12 deletions ui/src/pages/info/Mimetype/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { Table } from "antd";
import React from "react";
import { MIME_COLUMNS, MIME_DATA } from "./utils/constants";

import useFetchList from "lib/utils/hooks/useFetchList";
import MimeSearchResult from "./components/MimeSearchResult";

const Mimetype: React.FC = () => {
const { data, isError, isLoading } = useFetchList(
"mime",
"/mime/data.json"
);

return (
<div>
<Table
columns={MIME_COLUMNS}
dataSource={MIME_DATA}
pagination={false}
title={() => "Features"}
bordered
scroll={{ x: "calc(50%)" }}
/>
</div>
<MimeSearchResult isError={isError} isLoading={isLoading} data={data} />
);
};

Expand Down
118 changes: 27 additions & 91 deletions ui/src/pages/info/Mimetype/utils/constants.tsx
Original file line number Diff line number Diff line change
@@ -1,110 +1,46 @@
import { ColumnsType } from "antd/es/table";
import { MIME_TABLE_TYPE } from "./types";
import { MimeTableDataType } from "./types";
import { Tag } from "antd";
import CodeHighlightWithCopy from "components/General/CodeHighlightWithCopy";

// Define a function to render different MIME types
const renderMIMEType = (value: string) => {
if (
value.endsWith(".jpg") ||
value.endsWith(".jpeg") ||
value.endsWith(".png") ||
value.endsWith(".avif")
) {
return (
<div>
<img
style={{
width: "80px",
height: "50px",
objectFit: "contain",
}}
src={value}
alt=""
/>
</div>
);
}

if (value.endsWith(".mp3")) {
return (
<div>
<audio src={value} controls></audio>
</div>
);
}
if (value.endsWith(".mp4")) {
return (
<div>
<video width={200} height={100} src={value} controls></video>
</div>
);
}
return <code>{value}</code>;
};

// Define MIME_COLUMNS as a constant array of column configurations
const MIME_COLUMNS: ColumnsType<MIME_TABLE_TYPE> = [
// Column
const MIME_COLUMNS: ColumnsType<MimeTableDataType> = [
{
title: "Name",
dataIndex: "name",
key: "name",
render: (value) => {
return <Tag>{value}</Tag>;
},
},
{
title: "Example",
dataIndex: "example",
key: "example",
render: renderMIMEType,
render: (value) => {
return (
<div>
<p>{value}</p>
</div>
);
},
},
{
title: "Code",
dataIndex: "code",
key: "code",
width: 200,
render: (value) => {
return (
<div>
<CodeHighlightWithCopy
language="typescript"
codeString={JSON.stringify(value)}
/>
</div>
);
},
},
];

// Define MIME_DATA as a constant array of data
const MIME_DATA: MIME_TABLE_TYPE[] = [
{
key: "1",
name: ".json",
example: `{"x": 1}`,
code: "application/json",
},
{
key: "2",
name: ".jpeg or .jpg",
example: `/mime/image.jpg`,
code: "image/jpeg",
},
{
key: "3",
name: ".js",
example: `let x = 10;`,
code: "text/javascript",
},
{
key: "4",
name: ".mp3",
example: `/mime/applause.mp3`,
code: "audio/mpeg",
},
{
key: "5",
name: ".mp4",
example: `/mime/video.mp4`,
code: "video/mp4",
},
{
key: "6",
name: ".avif",
example: `/mime/avif.avif`,
code: "video/mp4",
},
{
key: "7",
name: ".mp4",
example: `/mime/pdf.png`,
code: "video/mp4",
},
];

export { MIME_COLUMNS, MIME_DATA };
export { MIME_COLUMNS };
12 changes: 12 additions & 0 deletions ui/src/pages/info/Mimetype/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { MimeTableDataType } from "./types";

function filteredMimeType(items: MimeTableDataType[], searchQuery: string) {
const lowercaseSearchQuery = searchQuery.toLowerCase();
return items.filter(
(item) =>
item.name.includes(lowercaseSearchQuery) ||
item?.code?.["content-type"]?.includes(lowercaseSearchQuery)
);
}

export { filteredMimeType };
13 changes: 9 additions & 4 deletions ui/src/pages/info/Mimetype/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
interface MIME_TABLE_TYPE {
key: string;
interface MimeTableDataType {
name: string;
example: string;
code: string;
code: { "content-type": string };
}

export type { MIME_TABLE_TYPE };
interface MimeSearchResultPropsType {
data: MimeTableDataType[];
isLoading: boolean;
isError: boolean;
}

export type { MimeTableDataType, MimeSearchResultPropsType };