|
| 1 | +import { defineWidget, useState, useEffect } from "trilium:preact"; |
| 2 | + |
| 3 | +const REPO = 'browneyedsoul/trilium-plugins' |
| 4 | +const CHECK_INTERVAL = 24 * 60 * 60 * 1000 |
| 5 | +const CACHE_KEY = 'trilium-plugins-update-check' |
| 6 | +const PLUGIN_PREFIX = 'trilium-plugin-' |
| 7 | + |
| 8 | +const getInstalledPlugins = () => { |
| 9 | + const plugins = [] |
| 10 | + for (let i = 0; i < localStorage.length; i++) { |
| 11 | + const key = localStorage.key(i) |
| 12 | + if (key.startsWith(PLUGIN_PREFIX) && key !== CACHE_KEY) { |
| 13 | + plugins.push({ |
| 14 | + name: key.slice(PLUGIN_PREFIX.length), |
| 15 | + version: localStorage.getItem(key), |
| 16 | + }) |
| 17 | + } |
| 18 | + } |
| 19 | + return plugins |
| 20 | +} |
| 21 | + |
| 22 | +const compareVersions = (a, b) => { |
| 23 | + const pa = a.split('.').map(Number) |
| 24 | + const pb = b.split('.').map(Number) |
| 25 | + for (let i = 0; i < 3; i++) { |
| 26 | + if ((pa[i] || 0) > (pb[i] || 0)) return 1 |
| 27 | + if ((pa[i] || 0) < (pb[i] || 0)) return -1 |
| 28 | + } |
| 29 | + return 0 |
| 30 | +} |
| 31 | + |
| 32 | +const checkForUpdates = async () => { |
| 33 | + const lastCheck = localStorage.getItem(CACHE_KEY) |
| 34 | + if (lastCheck && Date.now() - Number(lastCheck) < CHECK_INTERVAL) return [] |
| 35 | + |
| 36 | + localStorage.setItem(CACHE_KEY, String(Date.now())) |
| 37 | + |
| 38 | + try { |
| 39 | + const res = await fetch(`https://api.github.com/repos/${REPO}/tags`) |
| 40 | + if (!res.ok) return [] |
| 41 | + |
| 42 | + const tags = (await res.json()).map(t => t.name) |
| 43 | + const updates = [] |
| 44 | + |
| 45 | + for (const plugin of getInstalledPlugins()) { |
| 46 | + const prefix = `${plugin.name}@` |
| 47 | + const versions = tags |
| 48 | + .filter(t => t.startsWith(prefix)) |
| 49 | + .map(t => t.slice(prefix.length)) |
| 50 | + .sort(compareVersions) |
| 51 | + |
| 52 | + if (versions.length === 0) continue |
| 53 | + |
| 54 | + const latest = versions[versions.length - 1] |
| 55 | + if (compareVersions(latest, plugin.version) > 0) { |
| 56 | + updates.push({ |
| 57 | + name: plugin.name, |
| 58 | + current: plugin.version, |
| 59 | + latest, |
| 60 | + tag: `${prefix}${latest}`, |
| 61 | + }) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return updates |
| 66 | + } catch (_) { |
| 67 | + return [] |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +const toastStyle = { |
| 72 | + position: 'fixed', |
| 73 | + bottom: '20px', |
| 74 | + right: '20px', |
| 75 | + zIndex: 99999, |
| 76 | + background: '#1e1e1e', |
| 77 | + color: '#ccc', |
| 78 | + border: '1px solid #444', |
| 79 | + borderRadius: '8px', |
| 80 | + padding: '14px 18px', |
| 81 | + fontSize: '13px', |
| 82 | + maxWidth: '360px', |
| 83 | + boxShadow: '0 4px 12px rgba(0,0,0,0.4)', |
| 84 | +} |
| 85 | + |
| 86 | +const UpdateToast = () => { |
| 87 | + const [updates, setUpdates] = useState([]) |
| 88 | + const [visible, setVisible] = useState(true) |
| 89 | + |
| 90 | + useEffect(() => { |
| 91 | + checkForUpdates().then(setUpdates) |
| 92 | + }, []) |
| 93 | + |
| 94 | + if (updates.length === 0 || !visible) return null |
| 95 | + |
| 96 | + return ( |
| 97 | + <div style={toastStyle}> |
| 98 | + <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '8px' }}> |
| 99 | + <strong style={{ color: '#e6e6e6' }}>Plugin updates available</strong> |
| 100 | + <button |
| 101 | + onClick={() => setVisible(false)} |
| 102 | + style={{ background: 'none', border: 'none', color: '#aaa', cursor: 'pointer', fontSize: '16px' }} |
| 103 | + > |
| 104 | + ✕ |
| 105 | + </button> |
| 106 | + </div> |
| 107 | + <ul style={{ listStyle: 'none', padding: 0, margin: 0 }}> |
| 108 | + {updates.map(u => ( |
| 109 | + <li key={u.name} style={{ margin: '4px 0' }}> |
| 110 | + <strong>{u.name}</strong> v{u.current} → v{u.latest} |
| 111 | + <a |
| 112 | + href={`https://github.com/${REPO}/releases/tag/${u.tag}`} |
| 113 | + target="_blank" |
| 114 | + style={{ color: '#58a6ff', marginLeft: '6px' }} |
| 115 | + > |
| 116 | + Release |
| 117 | + </a> |
| 118 | + </li> |
| 119 | + ))} |
| 120 | + </ul> |
| 121 | + </div> |
| 122 | + ) |
| 123 | +} |
| 124 | + |
| 125 | +export default defineWidget({ |
| 126 | + parent: "center-pane", |
| 127 | + position: 9999, |
| 128 | + render: () => <UpdateToast />, |
| 129 | +}) |
0 commit comments