From a7a71f45dd5b7c2af9997d0a3b1770c696e359b2 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Fri, 19 Feb 2021 17:32:30 +0100 Subject: [PATCH] links --- packages/dashboard/package.json | 1 + packages/dashboard/src/components/Table.js | 17 +++++++++++++++-- packages/dashboard/src/pages/downloads.js | 10 +++++++--- packages/dashboard/src/pages/uploads.js | 10 +++++++--- .../src/services/authServerSideProps.js | 4 +++- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/dashboard/package.json b/packages/dashboard/package.json index 776945db..79fb14e9 100644 --- a/packages/dashboard/package.json +++ b/packages/dashboard/package.json @@ -24,6 +24,7 @@ "pretty-bytes": "^5.5.0", "react": "17.0.1", "react-dom": "17.0.1", + "skynet-js": "^3.0.0", "superagent": "^6.1.0", "swr": "^0.4.1", "tailwindcss": "^2.0.3", diff --git a/packages/dashboard/src/components/Table.js b/packages/dashboard/src/components/Table.js index 8dc0187c..ac4e87ee 100644 --- a/packages/dashboard/src/components/Table.js +++ b/packages/dashboard/src/components/Table.js @@ -59,12 +59,25 @@ export default function Table({ items, count, headers, actions, offset, setOffse {items.map((row, index) => ( - {headers.map(({ key, formatter, nowrap = true }) => ( + {headers.map(({ key, formatter, href, nowrap = true }) => ( - {(formatter ? formatter(row[key]) : row[key]) || <>—} + {(formatter ? ( + formatter(row, key) + ) : href ? ( + + {row[key]} + + ) : ( + row[key] + )) || <>—} ))} {actions.map(({ key, name, action }) => ( diff --git a/packages/dashboard/src/pages/downloads.js b/packages/dashboard/src/pages/downloads.js index a3b4cae1..256ef215 100644 --- a/packages/dashboard/src/pages/downloads.js +++ b/packages/dashboard/src/pages/downloads.js @@ -5,14 +5,18 @@ import useSWR from "swr"; import Layout from "../components/Layout"; import Table from "../components/Table"; import authServerSideProps from "../services/authServerSideProps"; +import { SkynetClient } from "skynet-js"; +const skynetClient = new SkynetClient(process.env.SKYNET_PORTAL_API ?? "https://siasky.net"); const apiPrefix = process.env.NODE_ENV === "development" ? "/api/stubs" : ""; const fetcher = (url) => fetch(url).then((r) => r.json()); +const getSkylinkLink = ({ skylink }) => skynetClient.getSkylinkUrl(skylink); +const getRelativeDate = ({ downloadedOn }) => dayjs(downloadedOn).format("YYYY-MM-DD HH:mm:ss"); const headers = [ - { key: "name", name: "Name", nowrap: false }, + { key: "name", name: "Name", nowrap: false, href: getSkylinkLink }, { key: "skylink", name: "Skylink" }, - { key: "size", name: "Size", formatter: prettyBytes }, - { key: "downloadedOn", name: "Accessed on", formatter: (date) => dayjs(date).format("YYYY-MM-DD HH:mm:ss") }, + { key: "size", name: "Size", formatter: ({ size }) => prettyBytes(size) }, + { key: "downloadedOn", name: "Accessed on", formatter: getRelativeDate }, ]; const actions = []; diff --git a/packages/dashboard/src/pages/uploads.js b/packages/dashboard/src/pages/uploads.js index c85cb857..125f2f69 100644 --- a/packages/dashboard/src/pages/uploads.js +++ b/packages/dashboard/src/pages/uploads.js @@ -5,14 +5,18 @@ import useSWR from "swr"; import Layout from "../components/Layout"; import Table from "../components/Table"; import authServerSideProps from "../services/authServerSideProps"; +import { SkynetClient } from "skynet-js"; +const skynetClient = new SkynetClient(process.env.SKYNET_PORTAL_API ?? "https://siasky.net"); const apiPrefix = process.env.NODE_ENV === "development" ? "/api/stubs" : ""; const fetcher = (url) => fetch(url).then((r) => r.json()); +const getSkylinkLink = ({ skylink }) => skynetClient.getSkylinkUrl(skylink); +const getRelativeDate = ({ uploadedOn }) => dayjs(uploadedOn).format("YYYY-MM-DD HH:mm:ss"); const headers = [ - { key: "name", name: "Name", nowrap: false }, + { key: "name", name: "Name", nowrap: false, href: getSkylinkLink }, { key: "skylink", name: "Skylink" }, - { key: "size", name: "Size", formatter: prettyBytes }, - { key: "uploadedOn", name: "Uploaded on", formatter: (date) => dayjs(date).format("YYYY-MM-DD HH:mm:ss") }, + { key: "size", name: "Size", formatter: ({ size }) => prettyBytes(size) }, + { key: "uploadedOn", name: "Uploaded on", formatter: getRelativeDate }, ]; const actions = []; diff --git a/packages/dashboard/src/services/authServerSideProps.js b/packages/dashboard/src/services/authServerSideProps.js index 859266d3..98acf09c 100644 --- a/packages/dashboard/src/services/authServerSideProps.js +++ b/packages/dashboard/src/services/authServerSideProps.js @@ -1,6 +1,8 @@ +const isProduction = process.env.NODE_ENV === "production"; + export default function authServerSideProps(getServerSideProps) { return function authenticate(context) { - if (!("ory_kratos_session" in context.req.cookies) || !("skynet-jwt" in context.req.cookies)) { + if (isProduction && (!("ory_kratos_session" in context.req.cookies) || !("skynet-jwt" in context.req.cookies))) { return { redirect: { permanent: false,