Skip to content

Commit

Permalink
feat: pr lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hamster1963 committed Dec 13, 2024
1 parent 17946d5 commit 55cc033
Show file tree
Hide file tree
Showing 85 changed files with 1,725 additions and 2,209 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/auto-fix-lint-format-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Auto Fix Lint and Format

on:
pull_request_target:
types: [opened, synchronize]

jobs:
auto-fix:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.head_ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}

- name: Set up Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: "latest"

- name: Install dependencies
run: bun install

- name: Run linter and fix issues
run: bun run lint:fix

- name: Run formatter
run: bun run format

- name: Check for changes
id: check_changes
run: |
git diff --exit-code || echo "has_changes=true" >> $GITHUB_ENV
- name: Commit and push changes
if: steps.check_changes.outputs.has_changes == 'true' || env.has_changes == 'true'
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "chore: auto-fix linting and formatting issues"
commit_options: "--no-verify"
file_pattern: "."

- name: Add PR comment
if: steps.check_changes.outputs.has_changes == 'true' || env.has_changes == 'true'
uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Linting and formatting issues were automatically fixed. Please review the changes.'
});
12 changes: 12 additions & 0 deletions .prettierrc.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
semi: false,
singleQuote: false,
printWidth: 100,
tabWidth: 2,
trailingComma: "all",
importOrder: ["^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],
importOrderSeparation: true,
importOrderSortSpecifiers: true,
endOfLine: "auto",
plugins: ["prettier-plugin-tailwindcss", "@trivago/prettier-plugin-sort-imports"],
}
49 changes: 23 additions & 26 deletions app/(main)/ClientComponents/Global.tsx
Original file line number Diff line number Diff line change
@@ -1,52 +1,49 @@
"use client";
"use client"

import { ServerApi } from "@/app/types/nezha-api";
import { nezhaFetcher } from "@/lib/utils";
import useSWRImmutable from "swr/immutable";
import { ServerApi } from "@/app/types/nezha-api"
import { nezhaFetcher } from "@/lib/utils"
import useSWRImmutable from "swr/immutable"

import { geoJsonString } from "../../../lib/geo-json-string";
import GlobalInfo from "./GlobalInfo";
import GlobalLoading from "./GlobalLoading";
import { InteractiveMap } from "./InteractiveMap";
import { TooltipProvider } from "./TooltipContext";
import { geoJsonString } from "../../../lib/geo-json-string"
import GlobalInfo from "./GlobalInfo"
import GlobalLoading from "./GlobalLoading"
import { InteractiveMap } from "./InteractiveMap"
import { TooltipProvider } from "./TooltipContext"

export default function ServerGlobal() {
const { data: nezhaServerList, error } = useSWRImmutable<ServerApi>(
"/api/server",
nezhaFetcher,
);
const { data: nezhaServerList, error } = useSWRImmutable<ServerApi>("/api/server", nezhaFetcher)

if (error)
return (
<div className="flex flex-col items-center justify-center">
<p className="text-sm font-medium opacity-40">{error.message}</p>
</div>
);
)

if (!nezhaServerList) {
return <GlobalLoading />;
return <GlobalLoading />
}

const countryList: string[] = [];
const serverCounts: { [key: string]: number } = {};
const countryList: string[] = []
const serverCounts: { [key: string]: number } = {}

nezhaServerList.result.forEach((server) => {
if (server.host.CountryCode) {
const countryCode = server.host.CountryCode.toUpperCase();
const countryCode = server.host.CountryCode.toUpperCase()
if (!countryList.includes(countryCode)) {
countryList.push(countryCode);
countryList.push(countryCode)
}
serverCounts[countryCode] = (serverCounts[countryCode] || 0) + 1;
serverCounts[countryCode] = (serverCounts[countryCode] || 0) + 1
}
});
})

const width = 900;
const height = 500;
const width = 900
const height = 500

const geoJson = JSON.parse(geoJsonString);
const geoJson = JSON.parse(geoJsonString)
const filteredFeatures = geoJson.features.filter(
(feature: any) => feature.properties.iso_a3_eh !== "",
);
)

return (
<section className="flex flex-col gap-4 mt-[3.2px]">
Expand All @@ -64,5 +61,5 @@ export default function ServerGlobal() {
</TooltipProvider>
</div>
</section>
);
)
}
12 changes: 6 additions & 6 deletions app/(main)/ClientComponents/GlobalInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
"use client";
"use client"

import { useTranslations } from "next-intl";
import { useTranslations } from "next-intl"

type GlobalInfoProps = {
countries: string[];
};
countries: string[]
}

export default function GlobalInfo({ countries }: GlobalInfoProps) {
const t = useTranslations("Global");
const t = useTranslations("Global")
return (
<section className="flex items-center justify-between">
<p className="text-sm font-medium opacity-40">
{t("Distributions")} {countries.length} {t("Regions")}
</p>
</section>
);
)
}
10 changes: 5 additions & 5 deletions app/(main)/ClientComponents/GlobalLoading.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"use client";
"use client"

import { Loader } from "@/components/loading/Loader";
import { useTranslations } from "next-intl";
import { Loader } from "@/components/loading/Loader"
import { useTranslations } from "next-intl"

export default function GlobalLoading() {
const t = useTranslations("Global");
const t = useTranslations("Global")
return (
<section className="flex flex-col gap-4 mt-[3.2px]">
<div className="flex min-h-40 flex-col items-center justify-center font-medium text-sm">
{t("Loading")}
<Loader visible={true} />
</div>
</section>
);
)
}
80 changes: 35 additions & 45 deletions app/(main)/ClientComponents/InteractiveMap.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
"use client";
"use client"

import { countryCoordinates } from "@/lib/geo-limit";
import { geoEquirectangular, geoPath } from "d3-geo";
import { countryCoordinates } from "@/lib/geo-limit"
import { geoEquirectangular, geoPath } from "d3-geo"

import MapTooltip from "./MapTooltip";
import { useTooltip } from "./TooltipContext";
import MapTooltip from "./MapTooltip"
import { useTooltip } from "./TooltipContext"

interface InteractiveMapProps {
countries: string[];
serverCounts: { [key: string]: number };
width: number;
height: number;
filteredFeatures: any[];
nezhaServerList: any;
countries: string[]
serverCounts: { [key: string]: number }
width: number
height: number
filteredFeatures: any[]
nezhaServerList: any
}

export function InteractiveMap({
Expand All @@ -23,20 +23,17 @@ export function InteractiveMap({
filteredFeatures,
nezhaServerList,
}: InteractiveMapProps) {
const { setTooltipData } = useTooltip();
const { setTooltipData } = useTooltip()

const projection = geoEquirectangular()
.scale(140)
.translate([width / 2, height / 2])
.rotate([-12, 0, 0]);
.rotate([-12, 0, 0])

const path = geoPath().projection(projection);
const path = geoPath().projection(projection)

return (
<div
className="relative w-full aspect-[2/1]"
onMouseLeave={() => setTooltipData(null)}
>
<div className="relative w-full aspect-[2/1]" onMouseLeave={() => setTooltipData(null)}>
<svg
width={width}
height={height}
Expand All @@ -60,11 +57,9 @@ export function InteractiveMap({
onMouseEnter={() => setTooltipData(null)}
/>
{filteredFeatures.map((feature, index) => {
const isHighlighted = countries.includes(
feature.properties.iso_a2_eh,
);
const isHighlighted = countries.includes(feature.properties.iso_a2_eh)

const serverCount = serverCounts[feature.properties.iso_a2_eh] || 0;
const serverCount = serverCounts[feature.properties.iso_a2_eh] || 0

return (
<path
Expand All @@ -77,70 +72,65 @@ export function InteractiveMap({
}
onMouseEnter={() => {
if (!isHighlighted) {
setTooltipData(null);
return;
setTooltipData(null)
return
}
if (path.centroid(feature)) {
const countryCode = feature.properties.iso_a2_eh;
const countryCode = feature.properties.iso_a2_eh
const countryServers = nezhaServerList.result
.filter(
(server: any) =>
server.host.CountryCode?.toUpperCase() ===
countryCode,
(server: any) => server.host.CountryCode?.toUpperCase() === countryCode,
)
.map((server: any) => ({
name: server.name,
status: server.online_status,
}));
}))
setTooltipData({
centroid: path.centroid(feature),
country: feature.properties.name,
count: serverCount,
servers: countryServers,
});
})
}
}}
/>
);
)
})}

{/* 渲染不在 filteredFeatures 中的国家标记点 */}
{countries.map((countryCode) => {
// 检查该国家是否已经在 filteredFeatures 中
const isInFilteredFeatures = filteredFeatures.some(
(feature) => feature.properties.iso_a2_eh === countryCode,
);
)

// 如果已经在 filteredFeatures 中,跳过
if (isInFilteredFeatures) return null;
if (isInFilteredFeatures) return null

// 获取国家的经纬度
const coords = countryCoordinates[countryCode];
if (!coords) return null;
const coords = countryCoordinates[countryCode]
if (!coords) return null

// 使用投影函数将经纬度转换为 SVG 坐标
const [x, y] = projection([coords.lng, coords.lat]) || [0, 0];
const serverCount = serverCounts[countryCode] || 0;
const [x, y] = projection([coords.lng, coords.lat]) || [0, 0]
const serverCount = serverCounts[countryCode] || 0

return (
<g
key={countryCode}
onMouseEnter={() => {
const countryServers = nezhaServerList.result
.filter(
(server: any) =>
server.host.CountryCode?.toUpperCase() === countryCode,
)
.filter((server: any) => server.host.CountryCode?.toUpperCase() === countryCode)
.map((server: any) => ({
name: server.name,
status: server.online_status,
}));
}))
setTooltipData({
centroid: [x, y],
country: coords.name,
count: serverCount,
servers: countryServers,
});
})
}}
className="cursor-pointer"
>
Expand All @@ -151,11 +141,11 @@ export function InteractiveMap({
className="fill-sky-700 stroke-white hover:fill-sky-600 dark:fill-sky-900 dark:hover:fill-sky-700 transition-all"
/>
</g>
);
)
})}
</g>
</svg>
<MapTooltip />
</div>
);
)
}
Loading

0 comments on commit 55cc033

Please sign in to comment.