Add auto update app download url on jan.ai (#311)
Co-authored-by: Hien To <tominhhien97@gmail.com>
This commit is contained in:
parent
62de74b602
commit
107d4c997f
2
.github/workflows/deploy-jan-docs.yml
vendored
2
.github/workflows/deploy-jan-docs.yml
vendored
@ -26,7 +26,7 @@ jobs:
|
||||
run: yarn install
|
||||
working-directory: docs
|
||||
- name: Build website
|
||||
run: yarn build
|
||||
run: sed -i '/process.env.DEBUG = namespaces;/c\// process.env.DEBUG = namespaces;' ./node_modules/debug/src/node.js && yarn build
|
||||
working-directory: docs
|
||||
|
||||
- name: Add Custome Domain file
|
||||
|
||||
2
.github/workflows/jan-docs-test.yml
vendored
2
.github/workflows/jan-docs-test.yml
vendored
@ -25,5 +25,5 @@ jobs:
|
||||
run: yarn install
|
||||
working-directory: docs
|
||||
- name: Test Build Command
|
||||
run: yarn build
|
||||
run: sed -i '/process.env.DEBUG = namespaces;/c\// process.env.DEBUG = namespaces;' ./node_modules/debug/src/node.js && yarn build
|
||||
working-directory: docs
|
||||
@ -21,7 +21,9 @@
|
||||
"@heroicons/react": "^2.0.18",
|
||||
"@mdx-js/react": "^1.6.22",
|
||||
"autoprefixer": "^10.4.16",
|
||||
"axios": "^1.5.1",
|
||||
"clsx": "^1.2.1",
|
||||
"js-yaml": "^4.1.0",
|
||||
"postcss": "^8.4.30",
|
||||
"prism-react-renderer": "^1.3.5",
|
||||
"react": "^17.0.2",
|
||||
|
||||
@ -2,27 +2,33 @@ import React, { useState, useEffect } from "react";
|
||||
import { Fragment } from "react";
|
||||
import { Menu, Transition } from "@headlessui/react";
|
||||
import { ChevronDownIcon } from "@heroicons/react/20/solid";
|
||||
import axios from "axios";
|
||||
import yaml from "js-yaml";
|
||||
|
||||
const systems = [
|
||||
const systemsTemplate = [
|
||||
{
|
||||
name: "Download for Mac (M1/M2)",
|
||||
href: "https://github.com/janhq/jan/releases/download/v0.1.3/jan-electron-mac-arm64-0.1.3.dmg",
|
||||
logo: require("@site/static/img/apple-logo-white.png").default,
|
||||
ymlFile: "latest-mac.yml",
|
||||
ymlIndex: 3, // Index of the M1/M2 file in the files array in latest-mac.yml
|
||||
},
|
||||
{
|
||||
name: "Download for Mac (Intel)",
|
||||
href: "https://github.com/janhq/jan/releases/download/v0.1.3/jan-electron-mac-x64-0.1.3.dmg",
|
||||
logo: require("@site/static/img/apple-logo-white.png").default,
|
||||
ymlFile: "latest-mac.yml",
|
||||
ymlIndex: 2, // Index of the Intel file in the files array in latest-mac.yml
|
||||
},
|
||||
{
|
||||
name: "Download for Windows",
|
||||
href: "https://github.com/janhq/jan/releases/download/v0.1.3/jan-electron-win-x64-0.1.3.exe",
|
||||
logo: require("@site/static/img/windows-logo-white.png").default,
|
||||
ymlFile: "latest.yml",
|
||||
ymlIndex: 0, // Index of the file in the files array in latest.yml
|
||||
},
|
||||
{
|
||||
name: "Download for Linux",
|
||||
href: "https://github.com/janhq/jan/releases/download/v0.1.3/jan-electron-linux-amd64-0.1.3.deb",
|
||||
logo: require("@site/static/img/linux-logo-white.png").default,
|
||||
ymlFile: "latest-linux.yml",
|
||||
ymlIndex: 0, // Index of the file in the files array in latest-linux.yml
|
||||
},
|
||||
];
|
||||
|
||||
@ -31,21 +37,65 @@ function classNames(...classes) {
|
||||
}
|
||||
|
||||
export default function Dropdown() {
|
||||
const [defaultSystem, setDefaultSystem] = useState(systems[0]);
|
||||
const [systems, setSystems] = useState(systemsTemplate);
|
||||
const [defaultSystem] = useState(systems[0]);
|
||||
|
||||
const getLatestReleaseInfo = async (repoOwner, repoName) => {
|
||||
const url = `https://api.github.com/repos/${repoOwner}/${repoName}/releases/latest`;
|
||||
try {
|
||||
const response = await axios.get(url);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const getDownloadUrlFromYml = async (tag_name, ymlFile, ymlIndex) => {
|
||||
const url = `https://github.com/janhq/jan/releases/download/${tag_name}/${ymlFile}`;
|
||||
try {
|
||||
// Fetch the YAML file
|
||||
const response = await fetch(url);
|
||||
|
||||
// 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(() => {
|
||||
const uAgent = window.navigator.userAgent;
|
||||
const updateDownloadLinks = async () => {
|
||||
try {
|
||||
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);
|
||||
return {
|
||||
...system,
|
||||
href: `https://github.com/janhq/jan/releases/download/${releaseInfo.tag_name}/${downloadUrl}`
|
||||
};
|
||||
}));
|
||||
setSystems(updatedSystems);
|
||||
} catch (error) {
|
||||
console.error("Failed to update download links:", error);
|
||||
}
|
||||
};
|
||||
|
||||
if (uAgent.indexOf("Win") !== -1) {
|
||||
setDefaultSystem(systems[2]);
|
||||
} else if (uAgent.indexOf("Mac") !== -1) {
|
||||
// Note: There's no way to detect ARM architecture from browser. Hardcoding to M1/M2 for now.
|
||||
setDefaultSystem(systems[0]);
|
||||
} else if (uAgent.indexOf("Linux") !== -1) {
|
||||
setDefaultSystem(systems[3]);
|
||||
} else {
|
||||
setDefaultSystem(systems[0]);
|
||||
}
|
||||
updateDownloadLinks();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
|
||||
@ -2695,6 +2695,11 @@ asap@~2.0.3:
|
||||
resolved "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz"
|
||||
integrity sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA==
|
||||
|
||||
asynckit@^0.4.0:
|
||||
version "0.4.0"
|
||||
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
|
||||
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==
|
||||
|
||||
at-least-node@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz"
|
||||
@ -2719,6 +2724,15 @@ axios@^0.25.0:
|
||||
dependencies:
|
||||
follow-redirects "^1.14.7"
|
||||
|
||||
axios@^1.5.1:
|
||||
version "1.5.1"
|
||||
resolved "https://registry.yarnpkg.com/axios/-/axios-1.5.1.tgz#11fbaa11fc35f431193a9564109c88c1f27b585f"
|
||||
integrity sha512-Q28iYCWzNHjAm+yEAot5QaAMxhMghWLFVf7rRdwhUI+c2jix2DUXjAHXVi+s1ibs3mjPO/cCgbA++3BjD0vP/A==
|
||||
dependencies:
|
||||
follow-redirects "^1.15.0"
|
||||
form-data "^4.0.0"
|
||||
proxy-from-env "^1.1.0"
|
||||
|
||||
babel-loader@^8.2.5:
|
||||
version "8.3.0"
|
||||
resolved "https://registry.npmjs.org/babel-loader/-/babel-loader-8.3.0.tgz"
|
||||
@ -3226,6 +3240,13 @@ combine-promises@^1.1.0:
|
||||
resolved "https://registry.npmjs.org/combine-promises/-/combine-promises-1.2.0.tgz"
|
||||
integrity sha512-VcQB1ziGD0NXrhKxiwyNbCDmRzs/OShMs2GqW2DlU2A/Sd0nQxE1oWDAE5O0ygSx5mgQOn9eIFh7yKPgFRVkPQ==
|
||||
|
||||
combined-stream@^1.0.8:
|
||||
version "1.0.8"
|
||||
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
|
||||
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
|
||||
dependencies:
|
||||
delayed-stream "~1.0.0"
|
||||
|
||||
comma-separated-tokens@^1.0.0:
|
||||
version "1.0.8"
|
||||
resolved "https://registry.npmjs.org/comma-separated-tokens/-/comma-separated-tokens-1.0.8.tgz"
|
||||
@ -3693,6 +3714,11 @@ del@^6.1.1:
|
||||
rimraf "^3.0.2"
|
||||
slash "^3.0.0"
|
||||
|
||||
delayed-stream@~1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
|
||||
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==
|
||||
|
||||
depd@2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz"
|
||||
@ -4277,6 +4303,11 @@ follow-redirects@^1.0.0, follow-redirects@^1.14.7:
|
||||
resolved "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz"
|
||||
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==
|
||||
|
||||
follow-redirects@^1.15.0:
|
||||
version "1.15.3"
|
||||
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.3.tgz#fe2f3ef2690afce7e82ed0b44db08165b207123a"
|
||||
integrity sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==
|
||||
|
||||
foreach@^2.0.4:
|
||||
version "2.0.6"
|
||||
resolved "https://registry.npmjs.org/foreach/-/foreach-2.0.6.tgz"
|
||||
@ -4301,6 +4332,15 @@ fork-ts-checker-webpack-plugin@^6.5.0:
|
||||
semver "^7.3.2"
|
||||
tapable "^1.0.0"
|
||||
|
||||
form-data@^4.0.0:
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
|
||||
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
|
||||
dependencies:
|
||||
asynckit "^0.4.0"
|
||||
combined-stream "^1.0.8"
|
||||
mime-types "^2.1.12"
|
||||
|
||||
forwarded@0.2.0:
|
||||
version "0.2.0"
|
||||
resolved "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz"
|
||||
@ -5190,7 +5230,7 @@ js-yaml@^3.13.1:
|
||||
|
||||
js-yaml@^4.1.0:
|
||||
version "4.1.0"
|
||||
resolved "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz"
|
||||
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.1.0.tgz#c1fb65f8f5017901cdd2c951864ba18458a10602"
|
||||
integrity sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==
|
||||
dependencies:
|
||||
argparse "^2.0.1"
|
||||
@ -5566,7 +5606,7 @@ mime-types@2.1.18:
|
||||
dependencies:
|
||||
mime-db "~1.33.0"
|
||||
|
||||
mime-types@^2.1.27, mime-types@^2.1.31, mime-types@~2.1.17, mime-types@~2.1.24, mime-types@~2.1.34:
|
||||
mime-types@^2.1.12, mime-types@^2.1.27, mime-types@^2.1.31, mime-types@~2.1.17, mime-types@~2.1.24, mime-types@~2.1.34:
|
||||
version "2.1.35"
|
||||
resolved "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz"
|
||||
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
|
||||
@ -6566,6 +6606,11 @@ proxy-addr@~2.0.7:
|
||||
forwarded "0.2.0"
|
||||
ipaddr.js "1.9.1"
|
||||
|
||||
proxy-from-env@^1.1.0:
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
|
||||
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==
|
||||
|
||||
pump@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user