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/file viewer #57

Merged
merged 4 commits into from
Sep 10, 2024
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
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"vite-tsconfig-paths": "^5.0.1"
},
"dependencies": {
"@cyntler/react-doc-viewer": "^1.17.0",
"@filebase/client": "^0.0.5",
"@kleros/curate-v2-templates": "workspace:^",
"@kleros/ui-components-library": "^2.13.1",
Expand Down
2 changes: 2 additions & 0 deletions web/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import SubmitItem from "./pages/SubmitItem";
import SubmitList from "./pages/SubmitList";
import { RegistryDetailsProvider } from "context/RegistryDetailsContext";
import { SubmitListProvider } from "./context/SubmitListContext";
import AttachmentDisplay from "./pages/AttachmentDisplay";

const App: React.FC = () => {
return (
Expand All @@ -39,6 +40,7 @@ const App: React.FC = () => {
}
/>
<Route path="submit-list/*" element={<SubmitList />} />
<Route path="attachment/*" element={<AttachmentDisplay />} />
<Route path="*" element={<h1>404 not found</h1>} />
</Route>
</SentryRoutes>
Expand Down
10 changes: 10 additions & 0 deletions web/src/assets/svgs/icons/arrow-left.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions web/src/assets/svgs/icons/new-tab.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions web/src/assets/svgs/icons/paperclip.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions web/src/components/FileViewer/Viewers/MarkdownViewer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from "react";
import styled from "styled-components";

import { type DocRenderer } from "@cyntler/react-doc-viewer";
import ReactMarkdown from "react-markdown";

const Container = styled.div`
padding: 16px;
`;

const StyledMarkdown = styled(ReactMarkdown)`
background-color: ${({ theme }) => theme.whiteBackground};
a {
font-size: 16px;
}
code {
color: ${({ theme }) => theme.secondaryText};
}
`;

const MarkdownRenderer: DocRenderer = ({ mainState: { currentDocument } }) => {
if (!currentDocument) return null;
const base64String = (currentDocument.fileData as string).split(",")[1];

// Decode the base64 string
const decodedData = atob(base64String);

return (
<Container id="md-renderer">
<StyledMarkdown>{decodedData}</StyledMarkdown>
</Container>
);
};

MarkdownRenderer.fileTypes = ["md", "text/plain"];
MarkdownRenderer.weight = 1;

export default MarkdownRenderer;
53 changes: 53 additions & 0 deletions web/src/components/FileViewer/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from "react";
import styled from "styled-components";

import DocViewer, { DocViewerRenderers } from "@cyntler/react-doc-viewer";

import "@cyntler/react-doc-viewer/dist/index.css";

import MarkdownRenderer from "./Viewers/MarkdownViewer";
import { customScrollbar } from "styles/customScrollbar";

const Wrapper = styled.div`
background-color: ${({ theme }) => theme.whiteBackground};
border-radius: 3px;
box-shadow: 0px 2px 3px 0px rgba(0, 0, 0, 0.06);
max-height: 1050px;
overflow: scroll;

${customScrollbar}
`;

const StyledDocViewer = styled(DocViewer)`
background-color: ${({ theme }) => theme.whiteBackground} !important;
`;

/**
* @description this viewer supports loading multiple files, it can load urls, local files, etc
* @param url The url of the file to be displayed
* @returns renders the file
*/
const FileViewer: React.FC<{ url: string }> = ({ url }) => {
const docs = [{ uri: url }];
return (
<Wrapper className="file-viewer-wrapper">
<StyledDocViewer
documents={docs}
pluginRenderers={[...DocViewerRenderers, MarkdownRenderer]}
config={{
header: {
disableHeader: true,
disableFileName: true,
},
pdfZoom: {
defaultZoom: 0.8,
zoomJump: 0.1,
},
pdfVerticalScrollByDefault: true, // false as default
}}
/>
</Wrapper>
);
};

export default FileViewer;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getIpfsUrl } from "utils/getIpfsUrl";
import AttachmentIcon from "svgs/icons/attachment.svg";
import { customScrollbar } from "styles/customScrollbar";
import { Evidence } from "src/graphql/graphql";
import { Link } from "react-router-dom";

const Container = styled.div`
width: 100%;
Expand All @@ -24,7 +25,7 @@ const DescriptionContainer = styled.div`
${customScrollbar}
`;

const StyledA = styled.a`
const StyledA = styled(Link)`
display: flex;
gap: 6px;
> svg {
Expand All @@ -43,7 +44,7 @@ const JustificationDetails: React.FC<{ justification: Justification }> = ({ just
<ReactMarkdown>{justification.description ?? "Unable to determine description"}</ReactMarkdown>
</DescriptionContainer>
{justification?.fileURI && (
<StyledA href={getIpfsUrl(justification.fileURI)}>
<StyledA to={`/attachment/?url=${getIpfsUrl(justification.fileURI)}`}>
<AttachmentIcon />
View attached file
</StyledA>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { responsiveSize } from "styles/responsiveSize";
import { getIpfsUrl } from "utils/getIpfsUrl";
import { useRegistryDetailsQuery } from "queries/useRegistryDetailsQuery";
import { MAIN_CURATE_ADDRESS } from "src/consts";
import { Link } from "react-router-dom";

const ShadeArea = styled.div`
display: flex;
Expand Down Expand Up @@ -37,7 +38,7 @@ const StyledP = styled.p`
)};
`;

const StyledA = styled.a`
const StyledA = styled(Link)`
display: flex;
align-items: center;
gap: 4px;
Expand Down Expand Up @@ -69,11 +70,7 @@ export const Policies: React.FC<IPolicies> = ({ policyURI, isItem }) => {
{!isItem ? (
<>
{parentRegistryDetails ? (
<StyledA
href={getIpfsUrl(parentRegistryDetails.registry.policyURI ?? "")}
target="_blank"
rel="noreferrer"
>
<StyledA to={`/attachment/?url=${getIpfsUrl(parentRegistryDetails.registry.policyURI ?? "")}`}>
<StyledPolicyIcon />
Curation Policy
</StyledA>
Expand All @@ -83,7 +80,7 @@ export const Policies: React.FC<IPolicies> = ({ policyURI, isItem }) => {
</>
) : null}
{policyURI ? (
<StyledA href={getIpfsUrl(policyURI)} target="_blank" rel="noreferrer">
<StyledA to={`/attachment/?url=${getIpfsUrl(policyURI)}`}>
<StyledPolicyIcon />
List Policy
</StyledA>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { isUndefined } from "src/utils";
import { DEFAULT_LIST_LOGO } from "src/consts";
import { getIpfsUrl } from "utils/getIpfsUrl";
import { shortenAddress } from "utils/shortenAddress";
import { Link } from "react-router-dom";

const TopInfoContainer = styled.div`
display: flex;
Expand Down Expand Up @@ -122,12 +123,14 @@ const TopInfo: React.FC<ITopInfo> = ({
{isUndefined(logoURI) ? (
<SkeletonLogo />
) : (
<StyledLogo
src={imageSrc}
onError={() => setImageSrc(getIpfsUrl(DEFAULT_LIST_LOGO))}
alt="List Img"
isListView={false}
/>
<Link to={`/attachment/?url=${imageSrc}`}>
<StyledLogo
src={imageSrc}
onError={() => setImageSrc(getIpfsUrl(DEFAULT_LIST_LOGO))}
alt="List Img"
isListView={false}
/>
</Link>
)}
{isUndefined(title) ? <SkeletonTitle /> : <StyledTitle>{title}</StyledTitle>}
</LogoAndTitle>
Expand Down
5 changes: 3 additions & 2 deletions web/src/components/ItemCard/ItemField/FileField.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from "react";
import { Link } from "react-router-dom";
import styled from "styled-components";
import PDFIcon from "svgs/icons/pdf.svg";
import { getIpfsUrl } from "utils/getIpfsUrl";

const Container = styled.a`
const Container = styled(Link)`
display: flex;
gap: 8px;
`;
Expand All @@ -17,7 +18,7 @@ export interface IFileField {
const FileField: React.FC<IFileField> = ({ value, label }) => {
const fileUrl = getIpfsUrl(value);
return (
<Container href={fileUrl} onClick={(event) => event.stopPropagation()} target="blank" rel="noreferrer">
<Container to={`/attachment/?url=${fileUrl}`} onClick={(event) => event.stopPropagation()}>
<PDFIcon />
{label}
</Container>
Expand Down
7 changes: 6 additions & 1 deletion web/src/components/ItemCard/ItemField/ImageField.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import { Link } from "react-router-dom";
import styled from "styled-components";
import { getIpfsUrl } from "utils/getIpfsUrl";

Expand All @@ -15,7 +16,11 @@ export interface IImageField {

const ImageField: React.FC<IImageField> = ({ value, detailed }) => {
const imgUrl = getIpfsUrl(value);
return <StyledImg src={imgUrl} {...{ detailed }} />;
return (
<Link to={`/attachment/?url=${imgUrl}`}>
<StyledImg src={imgUrl} {...{ detailed }} />
</Link>
);
};

export default ImageField;
49 changes: 49 additions & 0 deletions web/src/components/Loader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from "react";
import styled, { type CSSProperties, keyframes } from "styled-components";

import KlerosIcon from "svgs/icons/kleros.svg";

type Width = CSSProperties["width"];
type Height = CSSProperties["height"];

const breathing = keyframes`
0% {
transform: scale(1);
}

50% {
transform: scale(1.3);
}

100% {
transform: scale(1);
}
`;

const StyledKlerosIcon = styled(KlerosIcon)`
path {
fill: ${({ theme }) => theme.klerosUIComponentsStroke};
}
animation: ${breathing} 2s ease-out infinite normal;
`;

const Container = styled.div<{ width?: Width; height?: Height }>`
width: ${({ width }) => width ?? "100%"};
height: ${({ height }) => height ?? "100%"};
`;

interface ILoader {
width?: Width;
height?: Height;
className?: string;
}

const Loader: React.FC<ILoader> = ({ width, height, className }) => {
return (
<Container {...{ width, height, className }}>
<StyledKlerosIcon />
</Container>
);
};

export default Loader;
16 changes: 11 additions & 5 deletions web/src/layout/Header/navbar/Explore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import styled, { css } from "styled-components";
import { landscapeStyle } from "styles/landscapeStyle";
import { Link, useLocation } from "react-router-dom";
import { useOpenContext } from "../MobileHeader";
import { IPFS_GATEWAY, MAIN_CURATE_ADDRESS } from "consts/index";
import { MAIN_CURATE_ADDRESS } from "consts/index";
import { useRegistryDetailsQuery } from "queries/useRegistryDetailsQuery";
import { isUndefined } from "utils/index";
import { getIpfsUrl } from "utils/getIpfsUrl";

const Container = styled.div`
display: flex;
Expand Down Expand Up @@ -57,9 +58,10 @@ const Explore: React.FC = () => {

const links = useMemo(
() => [
{ to: "/", text: "Home" },
{ identifier: "/", to: "/", text: "Home" },
{
to: isUndefined(mainCurate) ? "/" : `${IPFS_GATEWAY}${mainCurate.registry.policyURI}`,
identifier: "/attachment/",
to: isUndefined(mainCurate) ? "/" : `/attachment/?url=${getIpfsUrl(mainCurate.registry.policyURI ?? "")}`,
text: "Curation Policy",
},
],
Expand All @@ -69,9 +71,13 @@ const Explore: React.FC = () => {
return (
<Container>
<Title>Explore</Title>
{links.map(({ to, text }) => (
{links.map(({ to, text, identifier }) => (
<LinkContainer key={text}>
<StyledLink to={to} onClick={toggleIsOpen} isActive={location.pathname.startsWith(to)}>
<StyledLink
to={to}
onClick={toggleIsOpen}
isActive={identifier === "/" ? location.pathname === "/" : location.pathname.startsWith(identifier)}
>
{text}
</StyledLink>
</LinkContainer>
Expand Down
Loading