Skip to content

Commit

Permalink
Merge pull request #3 from UltimateDoge5/amd-products
Browse files Browse the repository at this point in the history
Re-scraped AMD products
  • Loading branch information
UltimateDoge5 committed Apr 7, 2023
2 parents 897d815 + 490b5af commit a62730c
Show file tree
Hide file tree
Showing 15 changed files with 546 additions and 463 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name: Run all tests
env:
UPSTASH_REDIS_REST_URL: ${{ secrets.UPSTASH_REDIS_REST_URL }}
UPSTASH_REDIS_REST_TOKEN: ${{ secrets.UPSTASH_REDIS_REST_TOKEN }}
BROWSERLESS_URL: ${{ secrets.BROWSERLESS_URL }}
BROWSERLESS_TOKEN: ${{ secrets.BROWSERLESS_TOKEN }}

on:
push:
Expand Down
6 changes: 3 additions & 3 deletions CPU.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ interface Cores {
}

interface Memory {
types: ({
types: {
speed: number;
type: string;
} | null)[];
}[];
maxSize: number | null;
}

Expand All @@ -48,4 +48,4 @@ export const Manufacturer = {
AMD: "amd",
} as const;

export type Manufacturer = typeof Manufacturer[keyof typeof Manufacturer];
export type Manufacturer = (typeof Manufacturer)[keyof typeof Manufacturer];
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build": "next build",
"start": "next start",
"lint": "next lint",
"test": "vitest"
"test": "vitest run"
},
"dependencies": {
"@headlessui/react": "^1.7.13",
Expand Down
8 changes: 7 additions & 1 deletion src/components/comparison.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ const Comparison = ({ cpus }: { cpus: [CPU, CPU] }) => {
render: "Price for " + cpu.name + " loaded",
type: "success",
autoClose: 2500,
isLoading: false,
});
} else {
toast.update($toast, {
render: "Failed to load price for " + cpu.name,
type: "error",
autoClose: 2500,
isLoading: false,
});
}
}
Expand All @@ -53,7 +55,11 @@ const Comparison = ({ cpus }: { cpus: [CPU, CPU] }) => {
className="p-2 text-left underline transition-colors hover:text-white"
key={cpu.name}
>
<Link href={`/cpu/${cpu.name}`} target="_blank" rel="noreferrer">
<Link
href={cpu?.ref ?? `/cpu/${cpu.name?.replace(/ /g, "-")}`}
target="_blank"
rel="noreferrer"
>
{cpu.name}
</Link>
</th>
Expand Down
2 changes: 1 addition & 1 deletion src/components/selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const Selector = ({ setCPU, urlId }: SelectorProps) => {
}
setCountdownBarPercent(percent);
},
window.matchMedia("(max-width: 768px)").matches ? 35 : 20,
35,
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/cpu/amd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return;
}

model = model.trim().replace(/ /g, "-").toLowerCase();
if (!model.startsWith("amd-")) model = `amd-${model}`;
model = model.trim().toLowerCase();
if (!model.startsWith("amd")) model = `amd-${model}`;

const noCache = req.query["no-cache"] !== undefined;

Expand Down
6 changes: 3 additions & 3 deletions src/pages/api/cpu/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (q === undefined) q = "";

// Get 5 cpus matching the query
const names = INTEL_PRODUCTS.concat(
AMD_PRODUCTS.map((p) => (p.split("/").pop() as string).toLowerCase().replaceAll("-", " "))
).filter((cpu) => cpu.toLowerCase().includes(q.toLowerCase().trim()));
const names = INTEL_PRODUCTS
.concat(AMD_PRODUCTS.map((p) => p.name))
.filter((cpu) => cpu.toLowerCase().includes(q.toLowerCase().trim())).map((cpu) => cpu.toLowerCase());

const remainingItems = Math.max(names.length - parseInt(p) * 5, 0);

Expand Down
10 changes: 4 additions & 6 deletions src/pages/api/cpu/tip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ const handler = async (req: NextRequest) => {

// Get 3 amd results close to the given model
if (manufacturer === "amd") {
model = model.trim().replace(/ /g, "-").toLowerCase();
if (!model.startsWith("amd-")) model = `amd-${model}`;
model = model.trim().toLowerCase();

const results = AMD_PRODUCTS.map((p) => p.split("/").pop() as string)
.filter((p) => p?.includes(model as string))
const results = AMD_PRODUCTS.map((p) => p.name.toLowerCase())
.filter((p) => p.replace("™","")?.includes(model as string))
.slice(0, 3)
.map((p) => p?.replace(/-/g, " ").replace("amd", "").replace("r", "R").trim());
.map((p) => p.replace("amd", "").replace("r", "R").trim());

return new Response(JSON.stringify(results));
} else if (manufacturer === "intel") {
// model = normaliseIntel(model);
model = model.trim().toLowerCase();
if(/i\d /i.test(model)) model = model.trim().replace(/(i\d) /i, "$1-");
return new Response(JSON.stringify(INTEL_PRODUCTS.filter((item) => item.toLowerCase().includes(model as string)).slice(0, 3)));
Expand Down
20 changes: 10 additions & 10 deletions src/pages/cpu/[cpu].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const Cpu = ({ data }: InferGetServerSidePropsType<typeof getServerSideProps>) =

const refreshCPU = async () => {
setRefetch(true);
const result = await fetch(`/api/cpu/${data.manufacturer}/${data.name}`);
const result = await fetch(`/api/cpu/${data.manufacturer}?model=${data.name}&no-cache`);

if (!result.ok) {
toast.error(
Expand Down Expand Up @@ -102,7 +102,7 @@ const RenderTable = ({ cpu, list }: { cpu: CPU; list: Table }) => (
<h2
className={`relative -left-4 ${i === 0 ? "mt-2" : "mt-4"} mb-1 border-b ${
cpu.manufacturer === "intel" ? "border-blue-500" : "border-red-500"
} px-2 text-3xl font-light`}
} px-2 pb-0.5 text-3xl font-light`}
>
{key}
</h2>
Expand Down Expand Up @@ -136,7 +136,7 @@ const RenderTable = ({ cpu, list }: { cpu: CPU; list: Table }) => (
{currentRow.title}
{currentRow.tooltip !== undefined && <Tooltip tip={currentRow.tooltip} />}
</span>
<span >
<span>
{currentRow.prefix !== false
? formatNumber(value, currentRow.unit)
: value + currentRow.unit}
Expand Down Expand Up @@ -231,7 +231,7 @@ const TableStructure: Table = {
path: "MSRP",
type: "number",
unit: "$",
tooltip: "Manufacturer's suggested retail price",
tooltip: "Manufacturer's suggested retail price.",
},
},
"CPU specifications": {
Expand Down Expand Up @@ -326,18 +326,18 @@ type Row = { title: string; hideOnUndefined?: true, tooltip?: string } & ( // Pr

const traversePath = (path: string, obj: any) => path.split(".").reduce((prev, curr) => prev && prev[curr], obj);

export const getServerSideProps: GetServerSideProps<{ data: CPU }> = async ({ req, params }) => {
export const getServerSideProps: GetServerSideProps<{ data: CPU }> = async ({ params }) => {
if (!params?.cpu) {
return {
notFound: true,
}
};
}

let model = params?.cpu as string;
const manufacturer = model?.split(" ")[0] as "intel" | "amd";
let model = (params?.cpu as string).toLowerCase();
const manufacturer = model.includes("intel") ? "intel" : model.includes("amd") ? "amd" : undefined;
if (process.env.NODE_ENV === "development") console.log(model, manufacturer);

if (!manufacturer || !model || !["intel", "amd"].includes(manufacturer)) {
if (!manufacturer) {
return {
notFound: true,
};
Expand All @@ -358,6 +358,6 @@ export const getServerSideProps: GetServerSideProps<{ data: CPU }> = async ({ re
return {
props: { data: result },
};
}
};

export default Cpu;

1 comment on commit a62730c

@vercel
Copy link

@vercel vercel bot commented on a62730c Apr 7, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.