-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from hotosm/feature/file_structure
Feature File Structure
- Loading branch information
Showing
3 changed files
with
273 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,59 @@ | ||
import { Avatar, IconButton, Typography } from '@mui/material' | ||
import React, { useContext, useEffect } from 'react' | ||
import AuthContext from '../../Context/AuthContext' | ||
import axios from '../../axios' | ||
import { useQuery } from 'react-query' | ||
const UserProfile = props => { | ||
|
||
const { accessToken,authenticate } = useContext(AuthContext); | ||
|
||
const authMe = async () => { | ||
|
||
if (!accessToken) return; | ||
try { | ||
const headers = { | ||
"access-token": accessToken | ||
} | ||
const res = await axios.get(`/auth/me/`, { headers }); | ||
|
||
if (res.error) { | ||
// setMapError(res.error.response.statusText); | ||
console.log(res); | ||
if (res.error.response.status === 403) | ||
{ | ||
authenticate('') | ||
} | ||
} | ||
else { | ||
console.log('Auth me ', res.data) | ||
return res.data; | ||
} | ||
|
||
} catch (e) { | ||
console.log("isError", e); | ||
|
||
} finally { | ||
|
||
import { Avatar, IconButton, Typography } from "@mui/material"; | ||
import React, { useContext, useEffect } from "react"; | ||
import AuthContext from "../../Context/AuthContext"; | ||
import axios from "../../axios"; | ||
import { useQuery } from "react-query"; | ||
const UserProfile = (props) => { | ||
const { accessToken, authenticate } = useContext(AuthContext); | ||
|
||
const authMe = async () => { | ||
if (!accessToken) return; | ||
try { | ||
const headers = { | ||
"access-token": accessToken, | ||
}; | ||
const res = await axios.get(`/auth/me/`, { headers }); | ||
|
||
if (res.error) { | ||
// setMapError(res.error.response.statusText); | ||
console.log(res); | ||
if (res.error.response.status === 403) { | ||
authenticate(""); | ||
} | ||
}; | ||
const { data, refetch } = useQuery("authMe" + props.aoiId, authMe, { refetchInterval: 120000 }); | ||
|
||
useEffect(() => { | ||
if (accessToken) | ||
refetch() | ||
|
||
return () => { | ||
|
||
} | ||
}, [accessToken, refetch]) | ||
|
||
return ( | ||
|
||
<div> | ||
<IconButton onClick={props.handleOpenUserMenu} sx={{ p: 0 }} > | ||
<Avatar alt="Remy Sharp" src={accessToken && data && data.img_url} /> | ||
</IconButton> | ||
<Typography variant="caption" display="block" textAlign="center" gutterBottom>{accessToken && data ? data.username : "Login here"}</Typography> | ||
</div> | ||
) | ||
} | ||
|
||
export default UserProfile | ||
} else { | ||
// console.log('Auth me ', res.data) | ||
return res.data; | ||
} | ||
} catch (e) { | ||
console.log("isError", e); | ||
} finally { | ||
} | ||
}; | ||
const { data, refetch } = useQuery("authMe" + props.aoiId, authMe, { | ||
refetchInterval: 120000, | ||
}); | ||
|
||
useEffect(() => { | ||
if (accessToken) refetch(); | ||
|
||
return () => {}; | ||
}, [accessToken, refetch]); | ||
|
||
return ( | ||
<div> | ||
<IconButton onClick={props.handleOpenUserMenu} sx={{ p: 0 }}> | ||
<Avatar alt="Remy Sharp" src={accessToken && data && data.img_url} /> | ||
</IconButton> | ||
<Typography | ||
variant="caption" | ||
display="block" | ||
textAlign="center" | ||
gutterBottom | ||
> | ||
{accessToken && data ? data.username : "Login here"} | ||
</Typography> | ||
</div> | ||
); | ||
}; | ||
|
||
export default UserProfile; |
146 changes: 146 additions & 0 deletions
146
frontend/src/components/Layout/AIModels/AIModelEditor/FileStructure.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import React, { useState } from "react"; | ||
import { | ||
ListItem, | ||
ListItemIcon, | ||
ListItemText, | ||
CircularProgress, | ||
Collapse, | ||
Box, | ||
Button, | ||
} from "@mui/material"; | ||
import { | ||
Folder, | ||
InsertDriveFile, | ||
ExpandLess, | ||
ExpandMore, | ||
} from "@mui/icons-material"; | ||
|
||
const FileStructure = ({ | ||
name, | ||
content, | ||
length, | ||
size, | ||
path, | ||
isFile, | ||
downloadUrl, | ||
onDirClick, | ||
}) => { | ||
const [open, setOpen] = useState(false); | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const handleClick = () => { | ||
if (!isFile) { | ||
setOpen(!open); | ||
setIsLoading(true); | ||
onDirClick(`${path}/`); | ||
setIsLoading(false); | ||
} else { | ||
window.open(`${downloadUrl}${path}`, "_blank"); | ||
} | ||
}; | ||
const formatFileSize = (sizeInBytes) => { | ||
const units = ["bytes", "KB", "MB", "GB", "TB"]; | ||
|
||
let formattedSize = sizeInBytes; | ||
let unitIndex = 0; | ||
|
||
while (formattedSize > 1024 && unitIndex < units.length - 1) { | ||
formattedSize /= 1024; | ||
unitIndex++; | ||
} | ||
|
||
return `${formattedSize.toFixed(2)} ${units[unitIndex]}`; | ||
}; | ||
|
||
const renderContent = () => { | ||
if (isFile) return null; | ||
|
||
const dirContent = Object.entries(content.dir || {}).map(([key, value]) => { | ||
return ( | ||
<FileStructure | ||
key={key} | ||
name={key} | ||
length={value["len"]} | ||
size={value["size"]} | ||
content={value} | ||
path={`${path}${key}/`} | ||
isFile={false} | ||
downloadUrl={downloadUrl} | ||
onDirClick={onDirClick} | ||
/> | ||
); | ||
}); | ||
|
||
const fileContent = Object.entries(content.file || {}).map( | ||
([key, value]) => ( | ||
<FileStructure | ||
key={key} | ||
name={key} | ||
size={value["size"]} | ||
length={value["len"]} | ||
content={value} | ||
path={`${path}${key}`} | ||
isFile={true} | ||
downloadUrl={downloadUrl} | ||
/> | ||
) | ||
); | ||
|
||
return [...dirContent, ...fileContent]; | ||
}; | ||
|
||
const iconStyles = { | ||
minWidth: "32px", | ||
color: "#757575", | ||
}; | ||
|
||
const listItemTextStyles = { | ||
fontSize: "0.875rem", | ||
color: "white", | ||
}; | ||
|
||
return ( | ||
<Box borderRadius="5px" padding="8px"> | ||
<ListItem | ||
Button | ||
onClick={handleClick} | ||
style={{ | ||
paddingLeft: isFile ? "32px" : "16px", | ||
color: "white", | ||
background: "#E7D6BD", | ||
}} | ||
> | ||
<ListItemIcon style={iconStyles}> | ||
{isFile ? ( | ||
<InsertDriveFile fontSize="small" /> | ||
) : ( | ||
<Folder fontSize="small" /> | ||
)} | ||
</ListItemIcon> | ||
<ListItemText | ||
primary={name} | ||
secondary={`${size ? formatFileSize(size) : ""} ${ | ||
length ? `${length} file` : "" | ||
}`} | ||
primaryTypographyProps={{ style: listItemTextStyles }} | ||
/> | ||
|
||
{!isFile && | ||
(open ? ( | ||
<ExpandLess fontSize="small" /> | ||
) : ( | ||
<ExpandMore fontSize="small" /> | ||
))} | ||
</ListItem> | ||
<Collapse in={open} timeout="auto" unmountOnExit> | ||
{isLoading ? ( | ||
<CircularProgress size={20} style={{ margin: "16px" }} /> | ||
) : ( | ||
renderContent() | ||
)} | ||
</Collapse> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default FileStructure; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters