Skip to content

Commit

Permalink
Clear out gitea access in the frontend
Browse files Browse the repository at this point in the history
Only endpoints needed for rendering projects are left.
  • Loading branch information
AbdulrhmnGhanem committed Jul 7, 2023
1 parent eafbea3 commit 8199297
Show file tree
Hide file tree
Showing 13 changed files with 15 additions and 576 deletions.
1 change: 0 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"meilisearch": "^0.25.1",
"morgan": "^1.10.0",
"next": "^12.1.6",
"node-fetch": "2",
"prop-types": "^15.7.2",
"raw-loader": "^4.0.0",
"react": "^17.0.2",
Expand Down
33 changes: 5 additions & 28 deletions frontend/src/components/SharedProjectPage/elements.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ import Readme from '@components/Board/Readme'
const PageElements = ({
assetPath,
repoName,
hasUploadPermission,
hasIBOM,
kitspaceYAML,
bomInfo,
zipUrl,
readme,
boardSpecs,
previewOnly,
description,
projectName,
projectFullname,
Expand All @@ -28,18 +26,6 @@ const PageElements = ({
readmeExists,
boardShowcaseAssetsExist,
}) => {
const AssetPlaceholderWithUploadPermissions = ({ asset }) => (
<AssetPlaceholder
asset={asset}
hasUploadPermission={hasUploadPermission}
previewOnly={previewOnly}
/>
)

AssetPlaceholderWithUploadPermissions.propTypes = {
asset: string.isRequired,
}

return (
<>
<InfoBar
Expand All @@ -59,7 +45,7 @@ const PageElements = ({
/>
</>
) : (
<AssetPlaceholderWithUploadPermissions asset="board" />
<AssetPlaceholder asset="board" />
)}
</div>
<div>
Expand All @@ -70,7 +56,7 @@ const PageElements = ({
zipUrl={zipUrl}
/>
) : (
<AssetPlaceholderWithUploadPermissions asset="gerber files" />
<AssetPlaceholder asset="gerber files" />
)}
{bomInfoExists ? (
<BuyParts
Expand All @@ -79,29 +65,23 @@ const PageElements = ({
projectFullName={projectFullname}
/>
) : (
<AssetPlaceholderWithUploadPermissions asset="bill of materials" />
<AssetPlaceholder asset="bill of materials" />
)}
</div>
<div>
{readmeExists ? (
<Readme renderedReadme={readme} />
) : (
<AssetPlaceholderWithUploadPermissions asset="README" />
<AssetPlaceholder asset="README" />
)}
</div>
</>
)
}

const AssetPlaceholder = ({ asset, hasUploadPermission, previewOnly }) => {
const AssetPlaceholder = ({ asset }) => {
let message = `No ${asset} files were found`

if (hasUploadPermission && previewOnly) {
message += ', commit files to the original repo and it will be synced'
} else if (hasUploadPermission && !previewOnly) {
message += ', upload some'
}

return (
<div
style={{
Expand All @@ -122,13 +102,10 @@ const AssetPlaceholder = ({ asset, hasUploadPermission, previewOnly }) => {

AssetPlaceholder.propTypes = {
asset: string.isRequired,
hasUploadPermission: bool.isRequired,
previewOnly: bool.isRequired,
}

PageElements.propTypes = {
assetPath: string.isRequired,
hasUploadPermission: bool.isRequired,
hasIBOM: bool.isRequired,
kitspaceYAML: shape({
summary: string,
Expand Down
13 changes: 1 addition & 12 deletions frontend/src/components/SharedProjectPage/index.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react'
import getConfig from 'next/config'
import { useRouter } from 'next/router'
import { Loader, Message } from 'semantic-ui-react'
import { Loader } from 'semantic-ui-react'
import { bool, object, string } from 'prop-types'

import Page from '@components/Page'
Expand Down Expand Up @@ -83,16 +83,6 @@ const SharedProjectPage = props => {
title={title}
url={`https://${KITSPACE_DOMAIN}/${props.username}/${props.repoName}`}
/>
{props.isSynced && props.hasUploadPermission ? (
<Message color="yellow" data-cy="sync-msg">
<Message.Header>A synced repository!</Message.Header>
<Message.Content>
<p>Files uploading isn&apos;t supported for synced repositories.</p>
Please commit files to the original git repository and it will be synced
automatically.
</Message.Content>
</Message>
) : null}
<PageElements
{...props}
assetPath={`${props.rootAssetPath}/${props.projectName}`}
Expand All @@ -115,7 +105,6 @@ SharedProjectPage.propTypes = {
isEmpty: bool.isRequired,
finishedProcessing: bool.isRequired,
isSynced: bool.isRequired,
hasUploadPermission: bool.isRequired,
}

export default SharedProjectPage
60 changes: 0 additions & 60 deletions frontend/src/contexts/AuthContext.jsx

This file was deleted.

10 changes: 3 additions & 7 deletions frontend/src/hooks/useForm.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { useState, useContext, useCallback, useEffect } from 'react'
import { AuthContext } from '@contexts/AuthContext'
import { useState, useCallback, useEffect } from 'react'
import { Schema } from 'joi'

export default function UseForm(schema: Schema, validateOnBlur?: boolean) {
const { csrf } = useContext(AuthContext)
const [form, setForm] = useState<any>({})
const [formValidationErrors, setFormValidationErrors] = useState<any>([])
/*
Expand All @@ -19,13 +17,11 @@ export default function UseForm(schema: Schema, validateOnBlur?: boolean) {

if (isCheckBox) {
setForm(prevForm => ({
_csrf: csrf,
...prevForm,
[data.name]: data.checked,
}))
} else {
setForm(prevForm => ({
_csrf: csrf,
...prevForm,
[event.target.name]: data.value,
}))
Expand All @@ -50,10 +46,10 @@ export default function UseForm(schema: Schema, validateOnBlur?: boolean) {
*/
(data, predicate = true) => {
if (predicate) {
setForm({ _csrf: csrf, ...data })
setForm(data)
}
},
[csrf],
[],
)

const isValid = Object.keys(formValidationErrors).length === 0
Expand Down
9 changes: 2 additions & 7 deletions frontend/src/pages/[user]/[repo]/[project]/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import { bool } from 'prop-types'
import getConfig from 'next/config'

import { canCommit, getRepo, repoExists } from '@utils/giteaApi'
import { getRepo, repoExists } from '@utils/giteaApi'
import {
getBoardBomInfo,
getBoardGerberInfo,
Expand All @@ -19,14 +19,13 @@ const assetUrl = getConfig().publicRuntimeConfig.KITSPACE_ASSET_URL
const MultiProjectPage = props =>
props.notFound ? <Custom404 /> : <SharedProjectPage {...props} />

MultiProjectPage.getInitialProps = async ({ query, req = null, res = null }) => {
MultiProjectPage.getInitialProps = async ({ query, res = null }) => {
const { user: username, repo: repoName, project: projectName } = query

const repoFullName = `${username}/${repoName}`

const rootAssetPath = `${assetUrl}/${repoFullName}/HEAD`
const assetPath = `${rootAssetPath}/${projectName}`
const session = req?.session ?? JSON.parse(sessionStorage.getItem('session'))

const exists = await repoExists(repoFullName)
if (exists) {
Expand All @@ -38,7 +37,6 @@ MultiProjectPage.getInitialProps = async ({ query, req = null, res = null }) =>
[kitspaceYAMLExists, kitspaceYamlArray],
finishedProcessing,
hasIBOM,
hasUploadPermission,
] = await Promise.all([
getRepo(repoFullName),
getReadme(assetPath),
Expand All @@ -47,8 +45,6 @@ MultiProjectPage.getInitialProps = async ({ query, req = null, res = null }) =>
getKitspaceYamlArray(rootAssetPath),
getIsProcessingDone(rootAssetPath),
hasInteractiveBom(assetPath),
// The repo owner and collaborators can upload files.
canCommit(repoFullName, session.user?.username, session.apiToken),
])

const kitspaceYAML = kitspaceYamlArray.find(p => p.name === projectName)
Expand All @@ -68,7 +64,6 @@ MultiProjectPage.getInitialProps = async ({ query, req = null, res = null }) =>
rootAssetPath,
repo,
projectFullname: repoFullName,
hasUploadPermission,
hasIBOM,
kitspaceYAML,
zipUrl,
Expand Down
11 changes: 3 additions & 8 deletions frontend/src/pages/_app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import 'semantic-ui-css/components/dimmer.min.css'
import 'semantic-ui-css/components/table.min.css'
import 'semantic-ui-css/components/progress.min.css'

import AuthProvider from '@contexts/AuthContext'
import './_app.scss'

if (typeof window !== 'undefined') {
Expand All @@ -42,7 +41,7 @@ if (typeof window !== 'undefined') {
}
}

function KitspaceApp({ Component, pageProps, session, isStaticFallback }) {
function KitspaceApp({ Component, pageProps, isStaticFallback }) {
const setStaticFallback = isStaticFallback ? (
<script
// using dangerouslySetInnerHTML to avoid browser parsing errors
Expand All @@ -57,25 +56,21 @@ function KitspaceApp({ Component, pageProps, session, isStaticFallback }) {
isStaticFallback = isStaticFallback || window.isStaticFallback
}
return (
<AuthProvider initialSession={session}>
<>
<Head>{setStaticFallback}</Head>
<Component {...pageProps} />
{isStaticFallback ? <ErrorMessage /> : null}
</AuthProvider>
</>
)
}

KitspaceApp.getInitialProps = async appContext => {
const appProps = await App.getInitialProps(appContext)

const { isStaticFallback } = appContext.ctx.query
const session =
appContext.ctx?.req?.session ??
JSON.parse(window?.sessionStorage.getItem('session'))

return {
...appProps,
session,
isStaticFallback,
}
}
Expand Down
4 changes: 0 additions & 4 deletions frontend/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const express = require('express')
const morgan = require('morgan')
const next = require('next')
const fetch = require('node-fetch')
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const hostname = process.env.KITSPACE_DOMAIN
Expand Down Expand Up @@ -34,9 +33,6 @@ async function main() {
})

server.all('*', async (req, res, next) => {
req.session = await fetch('http://gitea:3000/user/kitspace/session', {
headers: { ...req.headers, accept: 'application/json' },
}).then(r => r.json())
nextHandler(req, res, next)
})

Expand Down
Loading

0 comments on commit 8199297

Please sign in to comment.