import { Box, Grid, Stack, styled, Typography } from "@mui/material";
import React, { useContext, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { FileResponse, FileType } from "../../../../api/explorer.ts";
import { useAppSelector } from "../../../../redux/hooks.ts";
import DndWrappedFile from "../../Dnd/DndWrappedFile.tsx";
import { FmIndexContext } from "../../FmIndexContext.tsx";
import GridFile from "./GridFile.tsx";
import { CrUriPrefix } from "../../../../util/uri.ts";
export interface GridViewProps {
[key: string]: any;
}
export interface FmFile extends FileResponse {
id: string;
first?: boolean;
placeholder?: boolean;
}
interface listComponents {
Folders?: JSX.Element[];
Files: JSX.Element[];
}
const AutoFillGrid = styled(Grid)(({ theme }) => ({
[theme.breakpoints.down("md")]: {
gridTemplateColumns: "repeat(auto-fill,minmax(160px,1fr))!important",
},
[theme.breakpoints.up("md")]: {
gridTemplateColumns: "repeat(auto-fill,minmax(220px,1fr))!important",
},
gridGap: theme.spacing(2),
display: "grid!important",
padding: theme.spacing(1),
}));
const GridItem = styled(Grid)(() => ({
flex: "1 1 220px!important",
}));
export const loadingPlaceHolderNumb = 3;
// Path for a "loading more" placeholder row. Every row builds a CrUri from its
// path (useActionDisplayOpt), so this must parse as a URL. The page token goes
// in the path segment, percent-encoded: search tokens contain "|" (and tokens
// may contain "%"), which the URL parser rejects in the host position.
export const loadingPlaceholderPath = (id: string) => `${CrUriPrefix}placeholder/${encodeURIComponent(id)}`;
const GridView = React.forwardRef(({ ...rest }: GridViewProps, ref) => {
const { t } = useTranslation("application");
const fmIndex = useContext(FmIndexContext);
const files = useAppSelector((state) => state.fileManager[fmIndex].list?.files);
const mixedType = useAppSelector((state) => state.fileManager[fmIndex].list?.mixed_type);
const pagination = useAppSelector((state) => state.fileManager[fmIndex].list?.pagination);
const showThumb = useAppSelector((state) => state.fileManager[fmIndex].showThumb);
const search_params = useAppSelector((state) => state.fileManager[fmIndex]?.search_params);
const list = useMemo(() => {
const list: listComponents = {
Files: [],
};
if (files) {
files.forEach((file, index) => {
if (file.type === FileType.folder && !mixedType) {
if (!list.Folders) {
list.Folders = [];
}
list.Folders.push(
,
);
} else {
list.Files.push(
,
);
}
});
// Add loading placeholder if there is next page
if (pagination && pagination.next_token) {
for (let i = 0; i < loadingPlaceHolderNumb; i++) {
const id = `loadingPlaceholder-${pagination.next_token}-${i}`;
const loadingPlaceholder = (
0 ? showThumb : mixedType}
file={{
...files[0],
path: loadingPlaceholderPath(id),
id: id,
first: i == 0,
placeholder: true,
}}
/>
);
const _ =
list.Files.length > 0 ? list.Files.push(loadingPlaceholder) : list.Folders?.push(loadingPlaceholder);
}
}
}
return list;
}, [files, mixedType, pagination, showThumb]);
return (
{list.Folders && list.Folders.length > 0 && (
{t("fileManager.folders")}
{list.Folders.map((f) => f)}
)}
{list.Files.length > 0 && (
{!mixedType && (
{t("fileManager.files")}
)}
{list.Files.map((f) => f)}
)}
);
});
export default GridView;