Skip to content
Snippets Groups Projects
Commit 4826ce8b authored by Jose Manuel Serrano Amaut's avatar Jose Manuel Serrano Amaut
Browse files

[FEAT]: Add context provider for icons and darkmode

parent a9218f57
No related branches found
No related tags found
No related merge requests found
import { IconsMap } from "../core";
export type FilesUiConfig = { export type FilesUiConfig = {
darkMode?:boolean; darkMode?:boolean;
} icons?:IconsConfig;
\ No newline at end of file }
export type IconsConfig=IconsMap;
\ No newline at end of file
...@@ -109,7 +109,7 @@ const FileCard: React.FC<FileCardProps> = (props: FileCardProps) => { ...@@ -109,7 +109,7 @@ const FileCard: React.FC<FileCardProps> = (props: FileCardProps) => {
//} = mergeProps(props, FileCardPropsDefault); //} = mergeProps(props, FileCardPropsDefault);
} = props; } = props;
//context //context
const { darkMode: darkModeContext } = React.useContext(FilesUiContext); const { darkMode: darkModeContext , icons} = React.useContext(FilesUiContext);
const darkMode: boolean | undefined = const darkMode: boolean | undefined =
darkModeProp !== undefined ? darkModeProp : darkModeContext; darkModeProp !== undefined ? darkModeProp : darkModeContext;
...@@ -150,7 +150,8 @@ const darkMode: boolean | undefined = ...@@ -150,7 +150,8 @@ const darkMode: boolean | undefined =
valid, valid,
preview as boolean, preview as boolean,
imageUrl, imageUrl,
videoUrl videoUrl,
icons
); );
//The size formatted and rounded in 2 decimals //The size formatted and rounded in 2 decimals
const sizeFormatted: string | undefined = fileSizeFormater(localSize); const sizeFormatted: string | undefined = fileSizeFormater(localSize);
......
...@@ -71,10 +71,10 @@ const FileMosaic: React.FC<FileMosaicProps> = (props: FileMosaicProps) => { ...@@ -71,10 +71,10 @@ const FileMosaic: React.FC<FileMosaicProps> = (props: FileMosaicProps) => {
} = props; } = props;
//context //context
const { darkMode: darkModeContext } = React.useContext(FilesUiContext); const { darkMode: darkModeContext, icons } = React.useContext(FilesUiContext);
const darkMode: boolean | undefined = const darkMode: boolean | undefined =
darkModeProp !== undefined ? darkModeProp : darkModeContext; darkModeProp !== undefined ? darkModeProp : darkModeContext;
console.log("globalConfig", darkMode); console.log("globalConfig", darkMode, icons);
//localizers //localizers
...@@ -120,7 +120,8 @@ const FileMosaic: React.FC<FileMosaicProps> = (props: FileMosaicProps) => { ...@@ -120,7 +120,8 @@ const FileMosaic: React.FC<FileMosaicProps> = (props: FileMosaicProps) => {
valid, valid,
preview as boolean, preview as boolean,
imageUrl, imageUrl,
videoUrl videoUrl,
icons
); );
//The size formatted and rounded in 2 decimals //The size formatted and rounded in 2 decimals
......
import * as React from "react"; import * as React from "react";
import { getURLFileIco, readAsDataURL } from "../../../core"; import { getURLFileIco, readAsDataURL } from "../../../core";
import { getURLFileIcoFromNameAndType } from "../../../core/mime/mime"; import { getURLFileIcoFromNameAndType } from "../../../core/mime/mime";
import { IconsConfig } from "../../../FilesUiProvider/FilesUiContextType";
/** /**
* Initializer hook for FileItemNeo * Initializer hook for FileItemNeo
...@@ -20,6 +21,7 @@ const useFileMosaicInitializer = ( ...@@ -20,6 +21,7 @@ const useFileMosaicInitializer = (
preview: boolean, preview: boolean,
imageUrl: string | undefined, imageUrl: string | undefined,
videoUrl: string | undefined, videoUrl: string | undefined,
customIcons?:IconsConfig,
xhr?: XMLHttpRequest, xhr?: XMLHttpRequest,
): [boolean, boolean, boolean, string, string | undefined, File | string | undefined] => { ): [boolean, boolean, boolean, string, string | undefined, File | string | undefined] => {
...@@ -40,6 +42,7 @@ const useFileMosaicInitializer = ( ...@@ -40,6 +42,7 @@ const useFileMosaicInitializer = (
preview: boolean, preview: boolean,
imageUrl: string | undefined, imageUrl: string | undefined,
videoUrl: string | undefined, videoUrl: string | undefined,
customIcons?:IconsConfig,
xhr?: XMLHttpRequest, xhr?: XMLHttpRequest,
progress?: number progress?: number
) => { ) => {
...@@ -48,8 +51,8 @@ const useFileMosaicInitializer = ( ...@@ -48,8 +51,8 @@ const useFileMosaicInitializer = (
if (!file && (!name && !type)) return; if (!file && (!name && !type)) return;
const { url } = file ? getURLFileIco(file) : const { url } = file ? getURLFileIco(file,customIcons) :
getURLFileIcoFromNameAndType(name, type); getURLFileIcoFromNameAndType(name, type,customIcons);
//console.log("init", url); //console.log("init", url);
...@@ -107,7 +110,7 @@ const useFileMosaicInitializer = ( ...@@ -107,7 +110,7 @@ const useFileMosaicInitializer = (
////// CLEAN UP ////// CLEAN UP
React.useEffect(() => { React.useEffect(() => {
init(file, name, type, valid, preview || false, imageUrl, videoUrl); init(file, name, type, valid, preview || false, imageUrl, videoUrl,customIcons);
return () => { return () => {
setImageSource(undefined); setImageSource(undefined);
setIsImage(false); setIsImage(false);
...@@ -115,7 +118,7 @@ const useFileMosaicInitializer = ( ...@@ -115,7 +118,7 @@ const useFileMosaicInitializer = (
setIsReady(false); setIsReady(false);
}; };
// eslint-disable-next-line // eslint-disable-next-line
}, [file, name, type, valid, preview, imageUrl, videoUrl]); }, [file, name, type, valid, preview, imageUrl, videoUrl,customIcons]);
return [isReady, isImage, isVideo, url, imageSource, videoSource]; return [isReady, isImage, isVideo, url, imageSource, videoSource];
} }
......
...@@ -87,7 +87,8 @@ export type { ...@@ -87,7 +87,8 @@ export type {
UploadPromiseResponse, UploadPromiseResponse,
UploadResponse, UploadResponse,
UploadConfig, UploadConfig,
UPLOADSTATUS UPLOADSTATUS,
IconsMap
} from "./types"; } from "./types";
export { export {
......
...@@ -19,14 +19,15 @@ import { ...@@ -19,14 +19,15 @@ import {
zip zip
} from "./icons"; } from "./icons";
import { getExt } from "../utils/getExt"; import { getExt } from "../utils/getExt";
import { IconsMap } from "../types";
const DEF_GEN_MIME: string = "octet"; const DEF_GEN_MIME: keyof IconsMap = "octet";
/** /**
* *
* @param tailMime * @param tailMime
* @returns * @returns
*/ */
export const audioSelector = (tailMime: string): string => { export const audioSelector = (tailMime: string): keyof IconsMap => {
switch (tailMime) { switch (tailMime) {
case "aac": return "aac"; case "aac": return "aac";
case "midi": return "midi"; case "midi": return "midi";
...@@ -43,7 +44,7 @@ export const audioSelector = (tailMime: string): string => { ...@@ -43,7 +44,7 @@ export const audioSelector = (tailMime: string): string => {
default: return DEF_GEN_MIME; default: return DEF_GEN_MIME;
} }
} }
export const textSelector = (tailMime: string): string => { export const textSelector = (tailMime: string): keyof IconsMap => {
switch (tailMime) { switch (tailMime) {
case "css": return "css"; case "css": return "css";
case "csv": return "csv"; case "csv": return "csv";
...@@ -57,12 +58,12 @@ export const textSelector = (tailMime: string): string => { ...@@ -57,12 +58,12 @@ export const textSelector = (tailMime: string): string => {
} }
} }
export const imageSelector = (tailMime: string): string => { export const imageSelector = (tailMime: string): keyof IconsMap => {
switch (tailMime) { switch (tailMime) {
case "bmp": return "bmp"; case "bmp": return "bmp";
case "gif": return "gif"; case "gif": return "gif";
// case "vnd.microsoft.icon": return "ico"; // case "vnd.microsoft.icon": return "ico";
case "ico": return "ico"; //case "ico": return "ico";
case "jpg": return "jpeg"; case "jpg": return "jpeg";
case "jpeg": return "jpeg"; case "jpeg": return "jpeg";
case "png": return "png"; case "png": return "png";
...@@ -74,7 +75,7 @@ export const imageSelector = (tailMime: string): string => { ...@@ -74,7 +75,7 @@ export const imageSelector = (tailMime: string): string => {
} }
} }
export const fontSelector = (tailMime: string): string => { export const fontSelector = (tailMime: string): keyof IconsMap => {
switch (tailMime) { switch (tailMime) {
case "otf": return "otf"; case "otf": return "otf";
case "ttf": return "ttf"; case "ttf": return "ttf";
...@@ -85,7 +86,7 @@ export const fontSelector = (tailMime: string): string => { ...@@ -85,7 +86,7 @@ export const fontSelector = (tailMime: string): string => {
} }
} }
export const videoSelector = (tailMime: string): string => { export const videoSelector = (tailMime: string): keyof IconsMap => {
switch (tailMime) { switch (tailMime) {
case "x-msvideo": return "avi"; case "x-msvideo": return "avi";
case "msvideo": return "avi"; case "msvideo": return "avi";
...@@ -108,7 +109,7 @@ export const videoSelector = (tailMime: string): string => { ...@@ -108,7 +109,7 @@ export const videoSelector = (tailMime: string): string => {
* @param tailMime * @param tailMime
* @returns * @returns
*/ */
export const applicationSelector = (tailMime: string): string => { export const applicationSelector = (tailMime: string): keyof IconsMap => {
switch (tailMime) { switch (tailMime) {
case "x-abiword": return "abw"; case "x-abiword": return "abw";
case "abiword": return "abw"; case "abiword": return "abw";
...@@ -164,7 +165,7 @@ export const applicationSelector = (tailMime: string): string => { ...@@ -164,7 +165,7 @@ export const applicationSelector = (tailMime: string): string => {
* @returns the generic type, * @returns the generic type,
if not found it return "octet" that means generic binary file if not found it return "octet" that means generic binary file
*/ */
export const mimeSelector = (mimeType?: string): string => { export const mimeSelector = (mimeType?: string): keyof IconsMap => {
// let genericMime: string | undefined = undefined; // let genericMime: string | undefined = undefined;
if (!mimeType || !mimeType.includes("/")) { if (!mimeType || !mimeType.includes("/")) {
return DEF_GEN_MIME; return DEF_GEN_MIME;
...@@ -194,8 +195,8 @@ export const mimeSelector = (mimeType?: string): string => { ...@@ -194,8 +195,8 @@ export const mimeSelector = (mimeType?: string): string => {
* @param extension * @param extension
* @returns * @returns
*/ */
export const extensionSelector = (extension?: string): string => { export const extensionSelector = (extension?: string): keyof IconsMap => {
let genericMime: string = "octet"; let genericMime: keyof IconsMap = "octet";
if (extension && extension !== "") { if (extension && extension !== "") {
if (extension.includes("zip") || extension.includes("rar")) { if (extension.includes("zip") || extension.includes("rar")) {
...@@ -219,7 +220,7 @@ export const extensionSelector = (extension?: string): string => { ...@@ -219,7 +220,7 @@ export const extensionSelector = (extension?: string): string => {
} else if (extension === "java") { } else if (extension === "java") {
genericMime = "java"; genericMime = "java";
} else if (extension === "ts") { } else if (extension === "ts") {
genericMime = "ts"; genericMime = "typescript";
} else if (extension === "sass" || extension === "scss") { } else if (extension === "sass" || extension === "scss") {
genericMime = "sass"; genericMime = "sass";
} }
...@@ -232,8 +233,8 @@ export const extensionSelector = (extension?: string): string => { ...@@ -232,8 +233,8 @@ export const extensionSelector = (extension?: string): string => {
* @param extension * @param extension
* @returns * @returns
*/ */
export const checkIsCode = (extension?: string): string => { export const checkIsCode = (extension?: string): keyof IconsMap => {
let genericMime = "text"; let genericMime: keyof IconsMap = "text";
if (extension && extension !== "") { if (extension && extension !== "") {
if (extension === "jsx") { if (extension === "jsx") {
genericMime = "react"; genericMime = "react";
...@@ -258,17 +259,22 @@ export const checkIsCode = (extension?: string): string => { ...@@ -258,17 +259,22 @@ export const checkIsCode = (extension?: string): string => {
/** /**
* Looks for a suitable file icon * Looks for a suitable file icon
* If not found, returns octet-stream url
* @param props mime and extension from file to search * @param props mime and extension from file to search
* @returns the result file ico, if not found, turns octet-stream url * @returns the result file ico
*/ */
export const getURLFileIco = ( export const getURLFileIco = (
file: File | undefined file: File | undefined,
customIcons: IconsMap | undefined
): ResultFileIco => { ): ResultFileIco => {
let result = ""; let result: keyof IconsMap = "fallBack";
//if not file, return octet //if not file, return octet
if (!file) { if (!file) {
result = DEF_GEN_MIME; result = DEF_GEN_MIME;
if (customIcons?.fallBack)
return { url: customIcons?.fallBack, mimeResume: result };
return { url: mimeUrlList[result], mimeResume: result }; return { url: mimeUrlList[result], mimeResume: result };
} else { } else {
result = mimeSelector(file.type); result = mimeSelector(file.type);
...@@ -285,6 +291,11 @@ export const getURLFileIco = ( ...@@ -285,6 +291,11 @@ export const getURLFileIco = (
result = extensionSelector(extention); result = extensionSelector(extention);
} }
const customUrl = customIcons?.[result];
if (customUrl !== undefined)
return { url: customUrl, mimeResume: result };
return { url: mimeUrlList[result], mimeResume: result }; return { url: mimeUrlList[result], mimeResume: result };
} }
/** /**
...@@ -295,12 +306,15 @@ export const getURLFileIco = ( ...@@ -295,12 +306,15 @@ export const getURLFileIco = (
export const getURLFileIcoFromNameAndType = ( export const getURLFileIcoFromNameAndType = (
name: string | undefined, name: string | undefined,
type: string | undefined, type: string | undefined,
customIcons: IconsMap | undefined
): ResultFileIco => { ): ResultFileIco => {
let result = ""; let result: keyof IconsMap = "octet";
//if not nam and type, return octet //if not nam and type, return octet
if (!name) { if (!name) {
result = DEF_GEN_MIME; result = DEF_GEN_MIME;
if (customIcons?.fallBack)
return { url: customIcons?.fallBack, mimeResume: result };
return { url: mimeUrlList[result], mimeResume: result }; return { url: mimeUrlList[result], mimeResume: result };
} else { } else {
result = mimeSelector(type); result = mimeSelector(type);
...@@ -316,12 +330,15 @@ export const getURLFileIcoFromNameAndType = ( ...@@ -316,12 +330,15 @@ export const getURLFileIcoFromNameAndType = (
if (result === DEF_GEN_MIME) { if (result === DEF_GEN_MIME) {
result = extensionSelector(extention); result = extensionSelector(extention);
} }
const customUrl = customIcons?.[result];
if (customUrl !== undefined)
return { url: customUrl, mimeResume: result };
return { url: mimeUrlList[result], mimeResume: result }; return { url: mimeUrlList[result], mimeResume: result };
} }
interface ResultFileIco { interface ResultFileIco {
url: string; url: string;
mimeResume: string; mimeResume: keyof IconsMap;
} }
/** /**
* set of registered mimes on MDN * set of registered mimes on MDN
...@@ -333,9 +350,6 @@ interface MimeSelector { ...@@ -333,9 +350,6 @@ interface MimeSelector {
} }
const mimeUrlList: MimeSelector = { const mimeUrlList: MimeSelector = {
img: "https://ssl.gstatic.com/docs/doclist/images/mediatype/icon_1_image_x16.png",
video: "https://ssl.gstatic.com/docs/doclist/images/mediatype/icon_1_video_x16.png",
audio: "https://ssl.gstatic.com/docs/doclist/images/mediatype/icon_1_audio_x16.png",
aac: aac, aac: aac,
accdb: accdb, accdb: accdb,
abw: abw, abw: abw,
...@@ -416,4 +430,6 @@ const mimeUrlList: MimeSelector = { ...@@ -416,4 +430,6 @@ const mimeUrlList: MimeSelector = {
react: react, react: react,
vue: vue, vue: vue,
fallBack: octet,
}; };
\ No newline at end of file
export type IconsMap = {
aac?: string;
accdb?: string;
abw?: string;
arc?: string;
avi?: string;
azw?: string;
octet?: string;
bmp?: string;
bz?: string;
bz2?: string;
cda?: string;
csh?: string;
css?: string;
csv?: string;
docx?: string;
drawio?: string;
eot?: string;
epub?: string;
gzip?: string;
gif?: string;
html?: string;
//ico: ico,
icalendar?: string;
jar?: string;
jpeg?: string;
javascript?: string;
json?: string;
jsonld?: string;
midi?: string;
// js: js,
mp3?: string;
mp4?: string;
mpeg?: string;
mpkg?: string;
mp2t?: string;
odp?: string;
ods?: string;
odt?: string;
oga?: string;
ogv?: string;
ogx?: string;
opus?: string;
otf?: string;
png?: string;
pdf?: string;
php?: string;
pptx?: string;
psd?: string;
rar?: string;
rtf?: string;
sass?: string;
sh?: string;
//svg: svg,
swf?: string;
tar?: string;
tiff?: string;
ttf?: string;
//ts: ts,
typescript?: string;
text?: string;
vsd?: string;
wav?: string;
weba?: string;
webm?: string;
webp?: string;
woff?: string;
wma?: string;
wmv?: string;
xhtml?: string;
xlsx?: string;
xml?: string;
xul?: string;
zip?: string;
// threegp: threegp,
sevenzip?: string;
python?: string;
java?: string;
react?: string;
vue?: string;
fallBack?: string;
}
\ No newline at end of file
...@@ -21,4 +21,7 @@ export type { ServerResponse, UploadPromiseResponse, UploadResponse } from "./up ...@@ -21,4 +21,7 @@ export type { ServerResponse, UploadPromiseResponse, UploadResponse } from "./up
export type { ValidateFileResponse, FileValidatorProps } from "./validation"; export type { ValidateFileResponse, FileValidatorProps } from "./validation";
export type { UploadConfig } from "./UploadConfig"; export type { UploadConfig } from "./UploadConfig";
export { createUploadConfig } from "./UploadConfig";
\ No newline at end of file export { createUploadConfig } from "./UploadConfig";
export type { IconsMap } from "./IconsMap";
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment