-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
142 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |