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

Activity page #1169

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
5,329 changes: 5,329 additions & 0 deletions packages/worker-activity-page/package-lock.json

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions packages/worker-activity-page/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@alexwilson/worker-activity-page",
"private": true,
"version": "0.0.1",
"description": "Activity statistics",
"main": "index.js",
"author": "Alex Wilson <[email protected]>",
"license": "ISC",
"scripts": {
"deploy": "wrangler publish"
},
"devDependencies": {
"@cloudflare/workers-types": "^2.0.0",
"@cloudflare/wrangler": "^1.9.1",
"@types/react": "^16.9.44",
"@types/react-dom": "^16.9.8",
"jest": "^26.2.2",
"ts-jest": "^26.1.4",
"ts-loader": "^8.0.2",
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
},
"dependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1"
}
}
18 changes: 18 additions & 0 deletions packages/worker-activity-page/src/components/activity-page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react"

import Layout from "./layout"

export const ActivityPage = ({activities}: any) => {
return <Layout title="Activity">
{activities.map((activity: any) => (
<React.Fragment key={activity.attribute}>
<h2>{activity.label}</h2>
<ul>{activity.values.map((value: any) => (
<li key={value.date}>{`${value.date}: ${value.value}`}</li>
))}</ul>
</React.Fragment>
))}
</Layout>
}

export default ActivityPage
11 changes: 11 additions & 0 deletions packages/worker-activity-page/src/components/error-page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react"

import Layout from "./layout"

export const ErrorPage = () => {
return <Layout title="Error!">
<h1>An error occurred!</h1>
</Layout>
}

export default ErrorPage
12 changes: 12 additions & 0 deletions packages/worker-activity-page/src/components/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from "react"

export const Layout = (props: any) => {
return <html>
<head>
<title>{props.title}</title>
</head>
<body>{props.children}</body>
</html>
}

export default Layout
40 changes: 40 additions & 0 deletions packages/worker-activity-page/src/controllers/activity.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {ActivityPage} from "../components/activity-page"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Missing semicolon.

Suggested change
import {ActivityPage} from "../components/activity-page"
import {ActivityPage} from "../components/activity-page";

import {ErrorPage} from "../components/error-page"

export default class Activity {

private _existToken: string;

constructor(existToken: string) {
this._existToken = existToken;
}


async getActivity() {
const query = [
'attributes=events,commits,tweets',
'limit=31'
]

try {
const activities = await fetch(`https://exist.io/api/1/users/$self/attributes/?${query.join('&')}`, {
method: "GET",
headers: {
"Authorization": `Token ${this._existToken}`,
"Content-Type": "application/json"
}
}).then(res => res.json())
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Expected parentheses around arrow function argument.

Suggested change
}).then(res => res.json())
}).then((res) => res.json())


return {
status: 200,
view: ActivityPage({ activities })
}
} catch(e) {
return {
status: 503,
view: ErrorPage()
}
}

}
}
23 changes: 23 additions & 0 deletions packages/worker-activity-page/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {renderToString} from "react-dom/server"
import ActivityController from "./controllers/activity"

const renderToDocument = (component: React.ReactElement): string => `<!DOCTYPE html>${renderToString(component)}`


//@ts-ignore
const activityController = new ActivityController(EXISTIO_TOKEN)
self.addEventListener("fetch", (event) => {
event.respondWith(new Promise(async (resolve) => {

const result = await activityController.getActivity()
resolve(new Response(renderToDocument(result.view), {
status: result.status,
statusText: "OK",
headers: {
'content-type': 'text/html'
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Strings must use doublequote.

Suggested change
'content-type': 'text/html'
"content-type": 'text/html'

}
}))

}));
});

17 changes: 17 additions & 0 deletions packages/worker-activity-page/src/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react"

export const Layout = (props: any) => {
return <html>
<head>
<title>{props.title}</title>
</head>
<body>{props.children}</body>
</html>
}

export const TestPage = () => {
return <Layout title="test">
<div>Activity</div>
</Layout>
}

25 changes: 25 additions & 0 deletions packages/worker-activity-page/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"outDir": "./dist",
"module": "commonjs",
"target": "esnext",
"lib": ["esnext", "webworker"],
"jsx": "react",
"alwaysStrict": true,
"strict": true,
"preserveConstEnums": true,
"moduleResolution": "node",
"sourceMap": true,
"esModuleInterop": true,
"types": ["@cloudflare/workers-types"]
},
"include": [
"./src/*.ts",
"./test/*.ts",
"./src/**/*.ts",
"./test/**/*.ts",
"./node_modules/@cloudflare/workers-types/index.d.ts"
],
"exclude": ["node_modules/", "dist/"]
}

16 changes: 16 additions & 0 deletions packages/worker-activity-page/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
target: "webworker",
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json']
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Strings must use doublequote.

Suggested change
extensions: ['.ts', '.tsx', '.js', '.json']
extensions: [".ts", '.tsx', '.js', '.json']

},
entry: {
main: "./src/index.ts"
},
module: {
rules: [{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
}]
}
}
4 changes: 4 additions & 0 deletions packages/worker-activity-page/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name = "activity"
type = "webpack"
routes = ["https://alexwilson.tech/activity"]
webpack_config = "webpack.config.js"