File latest url, change to parser string from githubapi (#312)
Co-authored-by: Hien To <tominhhien97@gmail.com>
This commit is contained in:
parent
107d4c997f
commit
a369669537
@ -3,32 +3,27 @@ import { Fragment } from "react";
|
|||||||
import { Menu, Transition } from "@headlessui/react";
|
import { Menu, Transition } from "@headlessui/react";
|
||||||
import { ChevronDownIcon } from "@heroicons/react/20/solid";
|
import { ChevronDownIcon } from "@heroicons/react/20/solid";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import yaml from "js-yaml";
|
|
||||||
|
|
||||||
const systemsTemplate = [
|
const systemsTemplate = [
|
||||||
{
|
{
|
||||||
name: "Download for Mac (M1/M2)",
|
name: "Download for Mac (M1/M2)",
|
||||||
logo: require("@site/static/img/apple-logo-white.png").default,
|
logo: require("@site/static/img/apple-logo-white.png").default,
|
||||||
ymlFile: "latest-mac.yml",
|
fileFormat: "{appname}-mac-arm64-{tag}.dmg",
|
||||||
ymlIndex: 3, // Index of the M1/M2 file in the files array in latest-mac.yml
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Download for Mac (Intel)",
|
name: "Download for Mac (Intel)",
|
||||||
logo: require("@site/static/img/apple-logo-white.png").default,
|
logo: require("@site/static/img/apple-logo-white.png").default,
|
||||||
ymlFile: "latest-mac.yml",
|
fileFormat: "{appname}-mac-x64-{tag}.dmg",
|
||||||
ymlIndex: 2, // Index of the Intel file in the files array in latest-mac.yml
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Download for Windows",
|
name: "Download for Windows",
|
||||||
logo: require("@site/static/img/windows-logo-white.png").default,
|
logo: require("@site/static/img/windows-logo-white.png").default,
|
||||||
ymlFile: "latest.yml",
|
fileFormat: "{appname}-win-x64-{tag}.exe",
|
||||||
ymlIndex: 0, // Index of the file in the files array in latest.yml
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Download for Linux",
|
name: "Download for Linux",
|
||||||
logo: require("@site/static/img/linux-logo-white.png").default,
|
logo: require("@site/static/img/linux-logo-white.png").default,
|
||||||
ymlFile: "latest-linux.yml",
|
fileFormat: "{appname}-linux-amd64-{tag}.deb",
|
||||||
ymlIndex: 0, // Index of the file in the files array in latest-linux.yml
|
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -38,63 +33,62 @@ function classNames(...classes) {
|
|||||||
|
|
||||||
export default function Dropdown() {
|
export default function Dropdown() {
|
||||||
const [systems, setSystems] = useState(systemsTemplate);
|
const [systems, setSystems] = useState(systemsTemplate);
|
||||||
const [defaultSystem] = useState(systems[0]);
|
const [defaultSystem, setDefaultSystem] = useState(systems[0]);
|
||||||
|
|
||||||
const getLatestReleaseInfo = async (repoOwner, repoName) => {
|
const getLatestReleaseInfo = async (repoOwner, repoName) => {
|
||||||
const url = `https://api.github.com/repos/${repoOwner}/${repoName}/releases/latest`;
|
const url = `https://api.github.com/repos/${repoOwner}/${repoName}/releases/latest`;
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(url);
|
const response = await axios.get(url);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getDownloadUrlFromYml = async (tag_name, ymlFile, ymlIndex) => {
|
const extractAppName = (fileName) => {
|
||||||
const url = `https://github.com/janhq/jan/releases/download/${tag_name}/${ymlFile}`;
|
// Extract appname using a regex that matches the provided file formats
|
||||||
try {
|
const regex = /^(.*?)-(?:mac|win|linux)-(?:arm64|x64|amd64)-.*$/;
|
||||||
// Fetch the YAML file
|
const match = fileName.match(regex);
|
||||||
const response = await fetch(url);
|
return match ? match[1] : null;
|
||||||
|
|
||||||
// Check if the request was successful
|
|
||||||
if (!response.ok) {
|
|
||||||
console.error("Error fetching YAML file:", response.statusText);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert the response to text
|
|
||||||
const ymlText = await response.text();
|
|
||||||
|
|
||||||
// Parse the YAML text
|
|
||||||
const parsedYml = yaml.load(ymlText);
|
|
||||||
|
|
||||||
// Get the download URL from the parsed YAML
|
|
||||||
return parsedYml.files[ymlIndex].url;
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error fetching or parsing", url, ":", error);
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const updateDownloadLinks = async () => {
|
const updateDownloadLinks = async () => {
|
||||||
try {
|
try {
|
||||||
const releaseInfo = await getLatestReleaseInfo("janhq", "jan");
|
const releaseInfo = await getLatestReleaseInfo("janhq", "jan");
|
||||||
const updatedSystems = await Promise.all(systems.map(async (system) => {
|
|
||||||
const downloadUrl = await getDownloadUrlFromYml(releaseInfo.tag_name, system.ymlFile, system.ymlIndex);
|
// Extract appname from the first asset name
|
||||||
|
const firstAssetName = releaseInfo.assets[0].name;
|
||||||
|
const appname = extractAppName(firstAssetName);
|
||||||
|
|
||||||
|
if (!appname) {
|
||||||
|
console.error("Failed to extract appname from file name:", firstAssetName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove 'v' at the start of the tag_name
|
||||||
|
const tag = releaseInfo.tag_name.startsWith("v")
|
||||||
|
? releaseInfo.tag_name.substring(1)
|
||||||
|
: releaseInfo.tag_name;
|
||||||
|
|
||||||
|
const updatedSystems = systems.map((system) => {
|
||||||
|
const downloadUrl = system.fileFormat
|
||||||
|
.replace("{appname}", appname)
|
||||||
|
.replace("{tag}", tag);
|
||||||
return {
|
return {
|
||||||
...system,
|
...system,
|
||||||
href: `https://github.com/janhq/jan/releases/download/${releaseInfo.tag_name}/${downloadUrl}`
|
href: `https://github.com/janhq/jan/releases/download/${releaseInfo.tag_name}/${downloadUrl}`,
|
||||||
};
|
};
|
||||||
}));
|
});
|
||||||
|
|
||||||
setSystems(updatedSystems);
|
setSystems(updatedSystems);
|
||||||
|
setDefaultSystem(updatedSystems[0]);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to update download links:", error);
|
console.error("Failed to update download links:", error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
updateDownloadLinks();
|
updateDownloadLinks();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user