Skip to content

Commit

Permalink
Finish Updater
Browse files Browse the repository at this point in the history
  • Loading branch information
Cattn committed Aug 15, 2024
1 parent e66eb2c commit 12a42c9
Showing 1 changed file with 142 additions and 31 deletions.
173 changes: 142 additions & 31 deletions src/internal/Updater.tsx
Original file line number Diff line number Diff line change
@@ -1,54 +1,165 @@
import { Button } from "@/components/ui/button";
import { Plugin } from "@/lib/types";
import { useEffect, useState } from "react";
import { toast } from "sonner";

const Updater: Plugin = {
name: "updater",
name: "Updater",
id: "bunker.updater",
tile() {
const [latestTag, setLatestTag] = useState<string>(() => {
localStorage.getItem("latestTag");
});
const currentVersion = "v0.1.5";
const [latestVersion, setLatestVersion] = useState<string>();
const [installedVersion, setInstalledVersion] = useState<string>();

useEffect(() => {
setInstalledVersion("v0.1.3");
async function getLatestVersion() {
const response = await fetch(
"https://api.github.com/repos/bunkerweb/bunker/tags"
);
const data = await response.json();
if (data.length > 0) {
const tagName = data[0].name;
setLatestVersion(tagName);
console.log("real");
checkIfUpToDate();
}
}

async function updateBunker() {
toast('New Bunker update available [' + latestTag + ']', {
getLatestVersion();

async function checkIfUpToDate() {
if (latestVersion == undefined) return;
if (latestVersion == installedVersion) {
toast("Your Bunker is up to date!", {
action: {
label: "Dismiss",
onClick: () => console.log("Dismissed."),
},
});
} else if (latestVersion !== undefined) {
toast("New Bunker update available [" + latestVersion + "]", {
action: {
label: 'Install Now',
onClick: () => console.log('Undo')
label: "Install Now",
onClick: () => updateBunker(),
},
});
}
}

async function updateBunker() {
toast("Starting update process...", {
action: {
label: "Cancel",
onClick: () => console.log("Update cancelled"),
},
});
}

useEffect(() => {
const fetchLatestTag = async () => {
try {
const response = await fetch(
"https://api.github.com/repos/bunkerweb/bunker/tags"
"https://raw.githubusercontent.com/bunkerweb/bunker/updates/index.html"
);
const data = await response.json();
if (data.length > 0) {
const tagName = data[0].name;
const blob = await response.blob();
toast("Processing update package...");

const storedTag = localStorage.getItem("latestTag");
if (storedTag === tagName) {
return;
}

setLatestTag(tagName);
localStorage.setItem("latestTag", tagName);

await updateBunker();
}
// @ts-ignore
const fileHandle = await window.showOpenFilePicker({
types: [
{
description: "Please Select Bunker",
accept: {
"text/html": [".html"],
},
},
],
});
const nfc = await blob.text();
const wf = await fileHandle[0].createWritable();
await wf.write(nfc);
await wf.close();
toast("Update complete! Refresh to apply changes.", {
action: {
label: "Refresh",
onClick: () => window.location.reload(),
},
});
} catch (error) {
console.error("Error fetching the latest tag:", error);
toast("Error updating bunker: ", {
action: {
label: "Dismiss",
onClick: () => console.log("Dismissed"),
},
});
console.error(error);
}
};

fetchLatestTag();
}, []);
}
});

return <></>;
return (
<>
<p>latest version: {latestVersion}</p>
<p>current version: {currentVersion}</p>
<Button onClick={() => localStorage.clear()}>reset</Button>
</>
);
},
// tile() {
// const [latestTag, setLatestTag] = useState<string>(
// localStorage.getItem("latestTag") || "lol no localstorage thing"
// );

// async function updateBunker() {
// console.log(latestTag);
// console.log(localStorage.getItem("latestTag"));
// toast("New Bunker update available [" + latestTag + "]", {
// action: {
// label: "Install Now",
// onClick: () => console.log("Undo"),
// },
// });
// }

// useEffect(() => {
// const fetchLatestTag = async () => {
// try {
// const response = await fetch(
// "https://api.github.com/repos/bunkerweb/bunker/tags"
// );
// const data = await response.json();
// if (data.length > 0) {
// const tagName = data[0].name;

// const storedTag = localStorage.getItem("latestTag");
// if (storedTag === tagName) {
// return;
// }

// localStorage.setItem("latestTag", tagName);
// setLatestTag(tagName);

// await updateBunker();
// }
// } catch (error) {
// console.error("Error fetching the latest tag:", error);
// }
// };

// fetchLatestTag();
// }, []);

// return (
// <>
// <p>Latest version: {latestTag}</p>
// <Button
// onClick={() => {
// localStorage.clear();
// }}
// >
// reset
// </Button>
// </>
// );
// },
};

export default Updater;

0 comments on commit 12a42c9

Please sign in to comment.