|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { useEffect, useState } from "react" |
|
4
|
import { Link } from "@/components/Link" |
|
5
|
import { MDXComponents } from "@/components/MDXComponents" |
|
6
|
|
|
7
|
export type SolutionsProps = { |
|
8
|
feedback: boolean |
|
9
|
message?: string |
|
10
|
} |
|
11
|
|
|
12
|
export type GitHubSearchItem = { |
|
13
|
url: string |
|
14
|
html_url: string |
|
15
|
title: string |
|
16
|
[key: string]: unknown |
|
17
|
} |
|
18
|
|
|
19
|
const Ul = MDXComponents["ul"] as React.FC< |
|
20
|
React.HTMLAttributes<HTMLUListElement> |
|
21
|
> |
|
22
|
const Li = MDXComponents["li"] as React.FC<React.HTMLAttributes<HTMLLIElement>> |
|
23
|
|
|
24
|
export const Solutions = ({ feedback, message }: SolutionsProps) => { |
|
25
|
const [possibleSolutionsQuery, setPossibleSolutionsQuery] = |
|
26
|
useState<string>("") |
|
27
|
const [possibleSolutions, setPossibleSolutions] = useState< |
|
28
|
GitHubSearchItem[] |
|
29
|
>([]) |
|
30
|
|
|
31
|
function constructQuery(searchQuery: string) { |
|
32
|
return `${searchQuery} repo:medusajs/medusa is:closed is:issue` |
|
33
|
} |
|
34
|
|
|
35
|
async function searchGitHub(query: string) { |
|
36
|
return fetch( |
|
37
|
`https://api.github.com/search/issues?q=${encodeURIComponent(query)}&sort=updated&per_page=3&advanced_search=true`, |
|
38
|
{ |
|
39
|
headers: { |
|
40
|
Accept: "application/vnd.github.v3+json", |
|
41
|
}, |
|
42
|
} |
|
43
|
).then(async (res) => res.json() as Promise<{ items: GitHubSearchItem[] }>) |
|
44
|
} |
|
45
|
|
|
46
|
useEffect(() => { |
|
47
|
if (!feedback) { |
|
48
|
let query = constructQuery( |
|
49
|
// Github does not allow queries longer than 256 characters |
|
50
|
message ? message.substring(0, 256) : document.title |
|
51
|
) |
|
52
|
searchGitHub(query) |
|
53
|
.then(async (result) => { |
|
54
|
if (!result.items.length && message) { |
|
55
|
query = constructQuery(document.title) |
|
56
|
result = await searchGitHub(query) |
|
57
|
} |
|
58
|
|
|
59
|
setPossibleSolutionsQuery(query) |
|
60
|
setPossibleSolutions(result.items) |
|
61
|
}) |
|
62
|
.catch((err) => console.error(err)) |
|
63
|
} else { |
|
64
|
setPossibleSolutionsQuery("") |
|
65
|
setPossibleSolutions([]) |
|
66
|
} |
|
67
|
}, [feedback, message]) |
|
68
|
|
|
69
|
return ( |
|
70
|
<> |
|
71
|
{possibleSolutions.length > 0 && ( |
|
72
|
<div className="txt-medium"> |
|
73
|
<span className="my-docs_1 mx-0 inline-block"> |
|
74
|
If you faced a problem, here are some possible solutions from |
|
75
|
GitHub: |
|
76
|
</span> |
|
77
|
<Ul> |
|
78
|
{possibleSolutions.map((solution) => ( |
|
79
|
<Li key={solution.url} data-testid="solution-item"> |
|
80
|
<Link |
|
81
|
href={solution.html_url} |
|
82
|
target="_blank" |
|
83
|
rel="noreferrer" |
|
84
|
variant="content" |
|
85
|
> |
|
86
|
{solution.title} |
|
87
|
</Link> |
|
88
|
</Li> |
|
89
|
))} |
|
90
|
</Ul> |
|
91
|
<span> |
|
92
|
Explore more issues in{" "} |
|
93
|
<Link |
|
94
|
href={`https://github.com/medusajs/medusa/issues?q=${possibleSolutionsQuery}`} |
|
95
|
target="_blank" |
|
96
|
rel="noreferrer" |
|
97
|
variant="content" |
|
98
|
> |
|
99
|
the GitHub repository |
|
100
|
</Link> |
|
101
|
</span> |
|
102
|
</div> |
|
103
|
)} |
|
104
|
</> |
|
105
|
) |
|
106
|
} |