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

Frontend refactorings & dashboard features #53

Merged
merged 7 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 30 additions & 0 deletions docs/coding-style.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# TypeScript

- avoid `any`; get rid of any existing `any` whenever you can so that we can enable `"strict": true` later on in `tsconfig.json`
- define custom types for common data structures
- don't worry about `interface` vs `type`, [both are fine](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#differences-between-type-aliases-and-interfaces)

## Typescript and React/Next

- use `React.FC<Props>` type for React components, e.g. `const MyComponent: React.FC<Props> = ({ ... }) => { ... };`
- use `NextPage<Props>` for typing stuff in `src/pages/`
- use generic versions of `GetServerSideProps<Props>` and `GetStaticProps<Props>`

# React

- create one file per one component (tiny helper components in the same file are fine)
- if
Copy link
Collaborator

Choose a reason for hiding this comment

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

if?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Already fixed in 8d6cf48

- name file identically to the component it describes (e.g. `const DisplayForecasts: React.FC<Props> = ...` in `DisplayForecasts.ts`)
- use named export instead of default export for all React components
- it's better for refactoring
- and it plays well with `React.FC` typing

# Styles

- use [Tailwind](https://tailwindcss.com/)
- avoid positioning styles in components, position elements from the outside (e.g. with [space-\*](https://tailwindcss.com/docs/space) or grid/flexbox)

# General notes

- use `const` instead of `let` whenever possible
- set up [prettier](https://prettier.io/) to format code on save
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the documenting!

4 changes: 2 additions & 2 deletions src/pages/about.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import React from "react";
import ReactMarkdown from "react-markdown";
import gfm from "remark-gfm";

import Layout from "../web/display/layout";
import { Layout } from "../web/display/Layout";

let readmeMarkdownText = `# About
const readmeMarkdownText = `# About

This webpage is a search engine for probabilities. Given a query, it searches for relevant questions in various prediction markets and forecasting platforms. For example, try searching for "China", "North Korea", "Semiconductors", "COVID", "Trump", or "X-risk". In addition to search, we also provide various [tools](http://localhost:3000/tools).

Expand Down
2 changes: 1 addition & 1 deletion src/pages/capture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextPage } from "next";
import React from "react";

import { displayForecastsWrapperForCapture } from "../web/display/displayForecastsWrappers";
import Layout from "../web/display/layout";
import { Layout } from "../web/display/Layout";
import { Props } from "../web/search/anySearchPage";
import CommonDisplay from "../web/search/CommonDisplay";

Expand Down
23 changes: 12 additions & 11 deletions src/pages/dashboards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { useState } from "react";

import { DashboardItem } from "../backend/dashboards";
import { getPlatformsConfig, PlatformConfig } from "../backend/platforms";
import { DashboardCreator } from "../web/display/dashboardCreator";
import displayForecasts from "../web/display/displayForecasts";
import Layout from "../web/display/layout";
import { DashboardCreator } from "../web/display/DashboardCreator";
import { DisplayForecasts } from "../web/display/DisplayForecasts";
import { Layout } from "../web/display/Layout";
import { addLabelsToForecasts, FrontendForecast } from "../web/platforms";
import { getDashboardForecastsByDashboardId } from "../web/worker/getDashboardForecasts";

Expand Down Expand Up @@ -70,12 +70,12 @@ const DashboardsPage: NextPage<Props> = ({
);
const [dashboardItem, setDashboardItem] = useState(initialDashboardItem);

let handleSubmit = async (data) => {
const handleSubmit = async (data) => {
console.log(data);
// Send to server to create
// Get back the id
let response = await axios({
url: `/api/create-dashboard-from-ids`,
url: "/api/create-dashboard-from-ids",
method: "POST",
headers: { "Content-Type": "application/json" },
data: JSON.stringify(data),
Expand Down Expand Up @@ -107,7 +107,8 @@ const DashboardsPage: NextPage<Props> = ({
}
};

let isGraubardEasterEgg = (name) => (name == "Clay Graubard" ? true : false);
let isGraubardEasterEgg = (name: string) =>
name == "Clay Graubard" ? true : false;

return (
<Layout page="dashboard">
Expand Down Expand Up @@ -162,11 +163,11 @@ const DashboardsPage: NextPage<Props> = ({
</div>

<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 mb-6">
{displayForecasts({
results: dashboardForecasts,
numDisplay: dashboardForecasts.length,
showIdToggle: false,
})}
<DisplayForecasts
results={dashboardForecasts}
numDisplay={dashboardForecasts.length}
showIdToggle={false}
/>
</div>
{/* */}
<h3 className="flex items-center col-start-2 col-end-2 w-full justify-center mt-8 mb-4">
Expand Down
2 changes: 1 addition & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextPage } from "next";
import React from "react";

import { displayForecastsWrapperForSearch } from "../web/display/displayForecastsWrappers";
import Layout from "../web/display/layout";
import { Layout } from "../web/display/Layout";
import { Props } from "../web/search/anySearchPage";
import CommonDisplay from "../web/search/CommonDisplay";

Expand Down
5 changes: 3 additions & 2 deletions src/pages/recursion.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextPage } from "next";
import React, { useEffect } from "react";

function Recursion() {
const Recursion: NextPage = () => {
useEffect(() => {
if (typeof window !== "undefined") {
window.location.href = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
Expand All @@ -12,6 +13,6 @@ function Recursion() {
<h2>You have now reached the fourth level of recursion!!</h2>
</div>
);
}
};

export default Recursion;
12 changes: 6 additions & 6 deletions src/pages/secretDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useRouter } from "next/router"; // https://nextjs.org/docs/api-referenc
import { useState } from "react";

import { getPlatformsConfig } from "../backend/platforms";
import displayForecasts from "../web/display/displayForecasts";
import { DisplayForecasts } from "../web/display/DisplayForecasts";
import { addLabelsToForecasts } from "../web/platforms";
import { getDashboardForecastsByDashboardId } from "../web/worker/getDashboardForecasts";

Expand Down Expand Up @@ -88,11 +88,11 @@ export default function Home({
numCols || 3
} gap-4 mb-6`}
>
{displayForecasts({
results: dashboardForecasts,
numDisplay: dashboardForecasts.length,
showIdToggle: false,
})}
<DisplayForecasts
results={dashboardForecasts}
numDisplay={dashboardForecasts.length}
showIdToggle={false}
/>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/secretEmbed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GetServerSideProps, NextPage } from "next";
import React from "react";

import { platforms } from "../backend/platforms";
import { DisplayForecast } from "../web/display/displayForecasts";
import { DisplayForecast } from "../web/display/DisplayForecast";
import { FrontendForecast } from "../web/platforms";
import searchAccordingToQueryData from "../web/worker/searchAccordingToQueryData";

Expand Down
110 changes: 51 additions & 59 deletions src/pages/tools.tsx
Original file line number Diff line number Diff line change
@@ -1,102 +1,94 @@
import Link from "next/link";
import React from "react";

import Layout from "../web/display/layout";
import { Card } from "../web/display/Card";
import { Layout } from "../web/display/Layout";

type AnyTool = {
title: string;
description: string;
img?: string;
};

type InnerTool = AnyTool & { innerLink: string };
type ExternalTool = AnyTool & { externalLink: string };
type UpcomingTool = AnyTool;

type Tool = InnerTool | ExternalTool | UpcomingTool;

/* Display one tool */
function displayTool({
sameWebpage,
title,
description,
link,
url,
img,
i,
}: any) {
switch (sameWebpage) {
case true:
return (
<Link href={link} passHref key={`tool-${i}`}>
<div className="hover:bg-gray-100 hover:no-underline cursor-pointer flex flex-col px-4 py-3 bg-white rounded-md shadow place-content-stretch flex-grow no-underline b-6">
<div className="flex-grow items-stretch">
<div className={`text-gray-800 text-lg mb-2 font-medium `}>
{title}
</div>
<div className={`text-gray-500 mb-3 `}>{description}</div>
{}
<img src={img} className={`text-gray-500 mb-2`} />
</div>
</div>
</Link>
);
break;
default:
return (
<a
href={url}
key={`tool-${i}`}
className="hover:bg-gray-100 hover:no-underline cursor-pointer flex flex-col px-4 py-3 bg-white rounded-md shadow place-content-stretch flex-grow no-underline b-6"
>
<div className="flex-grow items-stretch">
<div className={`text-gray-800 text-lg mb-2 font-medium `}>
{title}
</div>
<div className={`text-gray-500 mb-3 `}>{description}</div>
{}
<img src={img} className={`text-gray-500 mb-2`} />
</div>
</a>
);
break;
const ToolCard: React.FC<Tool> = (tool) => {
const inner = (
<Card>
<div className="grid content-start gap-3">
<div className="text-gray-800 text-lg font-medium">{tool.title}</div>
<div className="text-gray-500">{tool.description}</div>
{tool.img && <img src={tool.img} className="text-gray-500" />}
</div>
</Card>
);

if ("innerLink" in tool) {
return (
<Link href={tool.innerLink} passHref>
<a className="text‑inherit no-underline">{inner}</a>
</Link>
);
} else if ("externalLink" in tool) {
return (
<a href={tool.externalLink} className="text‑inherit no-underline">
{inner}
</a>
);
} else {
return inner;
}
}
};

export default function Tools({ lastUpdated }) {
let tools = [
let tools: Tool[] = [
{
title: "Search",
description: "Find forecasting questions on many platforms",
link: "/",
sameWebpage: true,
description: "Find forecasting questions on many platforms.",
innerLink: "/",
img: "https://i.imgur.com/Q94gVqG.png",
},
{
title: "[Beta] Present",
description: "Present forecasts in dashboards.",
sameWebpage: true,
link: "/dashboards",
innerLink: "/dashboards",
img: "https://i.imgur.com/x8qkuHQ.png",
},
{
title: "Capture",
description:
"Capture forecasts save them to Imgur. Useful for posting them somewhere else as images. Currently rate limited by Imgur, so if you get a .gif of a fox falling flat on his face, that's why.",
link: "/capture",
sameWebpage: true,
innerLink: "/capture",
img: "https://i.imgur.com/EXkFBzz.png",
},
{
title: "Summon",
description:
"Summon metaforecast on Twitter by mentioning @metaforecast, or on Discord by using Fletcher and !metaforecast, followed by search terms",
url: "https://twitter.com/metaforecast",
"Summon metaforecast on Twitter by mentioning @metaforecast, or on Discord by using Fletcher and !metaforecast, followed by search terms.",
externalLink: "https://twitter.com/metaforecast",
img: "https://i.imgur.com/BQ4Zzjw.png",
},
{
title: "[Upcoming] Request",
description:
"Interact with metaforecast's API and fetch forecasts for your application. Currently possible but documentation is poor, get in touch.",
},

{
title: "[Upcoming] Record",
description: "Save your forecasts or bets.",
},
];
return (
<Layout page="tools">
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-2 gap-4 mb-8">
{tools.map((tool, i) => displayTool({ ...tool, i }))}
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-2 gap-4 mb-8 place-content-stretch">
{tools.map((tool, i) => (
<ToolCard {...tool} key={`tool-${i}`} />
))}
</div>
</Layout>
);
Expand Down
15 changes: 15 additions & 0 deletions src/web/display/Card.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const CardTitle: React.FC = ({ children }) => (
<div className="text-gray-800 text-lg font-medium">{children}</div>
);

type CardType = React.FC & {
Title: typeof CardTitle;
};

export const Card: CardType = ({ children }) => (
<div className="h-full px-4 py-3 bg-white hover:bg-gray-100 rounded-md shadow">
{children}
</div>
);

Card.Title = CardTitle;
Loading