diff --git a/package.json b/package.json index bd0c0fc..464dd7b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "food-waste-quotes", - "version": "1.0.14", + "version": "1.0.15", "description": "Quotes that will encourage you to not make food waste | Part of Prototype of Food Busters", "private": true, "scripts": { @@ -43,6 +43,7 @@ "@vitejs/plugin-react": "^1.2.0", "eslint": "^8.9.0", "eslint-plugin-react": "^7.28.0", + "prettier": "^2.5.1", "sass": "^1.49.7", "typescript": "^4.5.5", "vite": "^2.8.1" diff --git a/src/utils/time.ts b/src/utils/time.ts new file mode 100644 index 0000000..2919f12 --- /dev/null +++ b/src/utils/time.ts @@ -0,0 +1,19 @@ +// Copied from https://github.com/Leomotors/anime-captcha +export function howFar(date: string) { + const BuildDate = Date.parse(date); + // Calculate how far it is from BuildDate + + const now = Date.now(); + const diff = now - BuildDate; + const days = Math.floor(diff / (1000 * 60 * 60 * 24)); + const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); + const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); + const seconds = Math.floor((diff % (1000 * 60)) / 1000); + return days > 0 + ? `${days} days ago` + : hours > 0 + ? `${hours} hours ago` + : minutes > 0 + ? `${minutes} minutes ago` + : `${seconds} seconds ago`; +} diff --git a/src/views/Admin.tsx b/src/views/Admin.tsx index b07b79c..7f5d7d1 100644 --- a/src/views/Admin.tsx +++ b/src/views/Admin.tsx @@ -8,6 +8,7 @@ import AdminModal from "../components/AdminModal"; import FormModal from "../components/FormModal"; import DeleteModal from "../components/DeleteModal"; import Alert from "../components/Alert"; +import { howFar } from "../utils/time"; export default function Admin() { const [quotes, setQuotes] = useState([]); @@ -127,6 +128,7 @@ export default function Admin() { Quotes Image Count + Created At Actions @@ -137,6 +139,7 @@ export default function Admin() { {quote.quote} {quote.image} {quote.count} + {howFar(quote.created_at)}