Skip to content

Commit cdcefda

Browse files
committed
[refactor] simplify Copilot's source codes
1 parent 60514f3 commit cdcefda

File tree

17 files changed

+292
-12229
lines changed

17 files changed

+292
-12229
lines changed

app/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ComponentCard } from "@/components/component-card";
1+
import { ComponentCard } from "@/components/example/component-card";
22
import { HelloWorld } from "@/registry/new-york/blocks/hello-world/hello-world";
33
import { ExampleForm } from "@/registry/new-york/blocks/example-form/example-form";
44
import PokemonPage from "@/registry/new-york/blocks/complex-component/page";

components/example/form.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { GitRepository } from "mobx-github";
2+
3+
import { i18n, topicStore } from "@/models/example";
4+
import { Field } from "@/registry/new-york/blocks/rest-form/rest-form";
5+
import { SearchableInput } from "@/registry/new-york/blocks/searchable-input/searchable-input";
6+
7+
export const fields: Field<GitRepository>[] = [
8+
{
9+
key: "full_name",
10+
renderLabel: "Repository Name",
11+
required: true,
12+
minLength: 3,
13+
invalidMessage: "Input 3 characters at least",
14+
},
15+
{ key: "homepage", type: "url", renderLabel: "Home Page" },
16+
{ key: "language", renderLabel: "Programming Language" },
17+
{
18+
key: "topics",
19+
renderLabel: "Topic",
20+
renderInput: ({ topics }) => (
21+
<SearchableInput
22+
translator={i18n}
23+
store={topicStore}
24+
labelKey="name"
25+
valueKey="name"
26+
placeholder="search GitHub topics"
27+
multiple
28+
defaultValue={topics?.map((value) => ({ value, label: value }))}
29+
/>
30+
),
31+
},
32+
{ key: "stargazers_count", type: "number", renderLabel: "Star Count" },
33+
{ key: "description", renderLabel: "Description", rows: 3 },
34+
];

models/example.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { components, operations } from "@octokit/openapi-types";
2+
import { githubClient, RepositoryModel } from "mobx-github";
3+
import { TranslationModel } from "mobx-i18n";
4+
import { ListModel, Filter } from "mobx-restful";
5+
import { buildURLData } from "web-utility";
6+
7+
export const i18n = new TranslationModel({
8+
en_US: {
9+
load_more: "Load more",
10+
no_more: "No more",
11+
submit: "Submit",
12+
cancel: "Cancel",
13+
},
14+
});
15+
16+
type Topic = components["schemas"]["topic-search-result-item"];
17+
18+
type TopicSearchResponse =
19+
operations["search/topics"]["responses"]["200"]["content"]["application/json"];
20+
21+
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
22+
23+
githubClient.use(({ request }, next) => {
24+
if (GITHUB_TOKEN)
25+
request.headers = {
26+
...request.headers,
27+
Authorization: `Bearer ${GITHUB_TOKEN}`,
28+
};
29+
return next();
30+
});
31+
32+
class GitHubTopicModel extends ListModel<Topic> {
33+
baseURI = "search/topics";
34+
client = githubClient;
35+
36+
async loadPage(pageIndex: number, pageSize: number, { name }: Filter<Topic>) {
37+
const { body } = await this.client.get<TopicSearchResponse>(
38+
`${this.baseURI}?${buildURLData({
39+
q: name,
40+
page: pageIndex,
41+
per_page: pageSize,
42+
})}`
43+
);
44+
return { pageData: body!.items, totalCount: body!.total_count };
45+
}
46+
}
47+
48+
export const repositoryStore = new RepositoryModel("idea2app"),
49+
topicStore = new GitHubTopicModel();

0 commit comments

Comments
 (0)