diff options
Diffstat (limited to 'frontend/src/utils')
| -rw-r--r-- | frontend/src/utils/Jwt.ts | 32 | ||||
| -rw-r--r-- | frontend/src/utils/Time.ts | 64 |
2 files changed, 60 insertions, 36 deletions
diff --git a/frontend/src/utils/Jwt.ts b/frontend/src/utils/Jwt.ts index ce351fb..dc6ec92 100644 --- a/frontend/src/utils/Jwt.ts +++ b/frontend/src/utils/Jwt.ts | |||
| @@ -1,44 +1,48 @@ | |||
| 1 | // llm ahh funcs | 1 | // llm ahh funcs |
| 2 | export function get_user_id_from_token(token: string | undefined): string | undefined { | 2 | export function get_user_id_from_token( |
| 3 | token: string | undefined | ||
| 4 | ): string | undefined { | ||
| 3 | if (!token) { | 5 | if (!token) { |
| 4 | return undefined; | 6 | return undefined; |
| 5 | } | 7 | } |
| 6 | const parts = token.split('.'); | 8 | const parts = token.split("."); |
| 7 | if (parts.length !== 3) { | 9 | if (parts.length !== 3) { |
| 8 | return undefined; | 10 | return undefined; |
| 9 | } | 11 | } |
| 10 | const base64Url = parts[1]; | 12 | const base64Url = parts[1]; |
| 11 | const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); | 13 | const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/"); |
| 12 | 14 | ||
| 13 | const jsonPayload = decodeURIComponent( | 15 | const jsonPayload = decodeURIComponent( |
| 14 | atob(base64) | 16 | atob(base64) |
| 15 | .split('') | 17 | .split("") |
| 16 | .map(function (c) { | 18 | .map(function (c) { |
| 17 | return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); | 19 | return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2); |
| 18 | }) | 20 | }) |
| 19 | .join('') | 21 | .join("") |
| 20 | ); | 22 | ); |
| 21 | return JSON.parse(jsonPayload).sub; | 23 | return JSON.parse(jsonPayload).sub; |
| 22 | }; | 24 | } |
| 23 | 25 | ||
| 24 | export function get_user_mod_from_token(token: string | undefined): boolean | undefined { | 26 | export function get_user_mod_from_token( |
| 27 | token: string | undefined | ||
| 28 | ): boolean | undefined { | ||
| 25 | if (!token) { | 29 | if (!token) { |
| 26 | return undefined; | 30 | return undefined; |
| 27 | } | 31 | } |
| 28 | const parts = token.split('.'); | 32 | const parts = token.split("."); |
| 29 | if (parts.length !== 3) { | 33 | if (parts.length !== 3) { |
| 30 | return undefined; | 34 | return undefined; |
| 31 | } | 35 | } |
| 32 | const base64Url = parts[1]; | 36 | const base64Url = parts[1]; |
| 33 | const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); | 37 | const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/"); |
| 34 | 38 | ||
| 35 | const jsonPayload = decodeURIComponent( | 39 | const jsonPayload = decodeURIComponent( |
| 36 | atob(base64) | 40 | atob(base64) |
| 37 | .split('') | 41 | .split("") |
| 38 | .map(function (c) { | 42 | .map(function (c) { |
| 39 | return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); | 43 | return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2); |
| 40 | }) | 44 | }) |
| 41 | .join('') | 45 | .join("") |
| 42 | ); | 46 | ); |
| 43 | return JSON.parse(jsonPayload).mod; | 47 | return JSON.parse(jsonPayload).mod; |
| 44 | }; | 48 | } |
diff --git a/frontend/src/utils/Time.ts b/frontend/src/utils/Time.ts index b83a7ed..8f2c03c 100644 --- a/frontend/src/utils/Time.ts +++ b/frontend/src/utils/Time.ts | |||
| @@ -1,42 +1,62 @@ | |||
| 1 | export function time_ago(date: any) { | 1 | export function time_ago(date: any) { |
| 2 | const now = new Date().getTime(); | 2 | const now = new Date().getTime(); |
| 3 | 3 | ||
| 4 | const localDate = new Date(date.getTime() - (date.getTimezoneOffset() * 60000)); | 4 | const localDate = new Date(date.getTime() - date.getTimezoneOffset() * 60000); |
| 5 | const seconds = Math.floor((now - localDate.getTime()) / 1000); | 5 | const seconds = Math.floor((now - localDate.getTime()) / 1000); |
| 6 | 6 | ||
| 7 | let interval = Math.floor(seconds / 31536000); | 7 | let interval = Math.floor(seconds / 31536000); |
| 8 | if (interval === 1) {return interval + ' year ago';} | 8 | if (interval === 1) { |
| 9 | if (interval > 1) {return interval + ' years ago';} | 9 | return interval + " year ago"; |
| 10 | } | ||
| 11 | if (interval > 1) { | ||
| 12 | return interval + " years ago"; | ||
| 13 | } | ||
| 10 | 14 | ||
| 11 | interval = Math.floor(seconds / 2592000); | 15 | interval = Math.floor(seconds / 2592000); |
| 12 | if (interval === 1) {return interval + ' month ago';} | 16 | if (interval === 1) { |
| 13 | if (interval > 1) {return interval + ' months ago';} | 17 | return interval + " month ago"; |
| 18 | } | ||
| 19 | if (interval > 1) { | ||
| 20 | return interval + " months ago"; | ||
| 21 | } | ||
| 14 | 22 | ||
| 15 | interval = Math.floor(seconds / 86400); | 23 | interval = Math.floor(seconds / 86400); |
| 16 | if (interval === 1) {return interval + ' day ago';} | 24 | if (interval === 1) { |
| 17 | if (interval > 1) {return interval + ' days ago';} | 25 | return interval + " day ago"; |
| 26 | } | ||
| 27 | if (interval > 1) { | ||
| 28 | return interval + " days ago"; | ||
| 29 | } | ||
| 18 | 30 | ||
| 19 | interval = Math.floor(seconds / 3600); | 31 | interval = Math.floor(seconds / 3600); |
| 20 | if (interval === 1) {return interval + ' hour ago';} | 32 | if (interval === 1) { |
| 21 | if (interval > 1) {return interval + ' hours ago';} | 33 | return interval + " hour ago"; |
| 34 | } | ||
| 35 | if (interval > 1) { | ||
| 36 | return interval + " hours ago"; | ||
| 37 | } | ||
| 22 | 38 | ||
| 23 | interval = Math.floor(seconds / 60); | 39 | interval = Math.floor(seconds / 60); |
| 24 | if (interval === 1) {return interval + ' minute ago';} | 40 | if (interval === 1) { |
| 25 | if (interval > 1) {return interval + ' minutes ago';} | 41 | return interval + " minute ago"; |
| 42 | } | ||
| 43 | if (interval > 1) { | ||
| 44 | return interval + " minutes ago"; | ||
| 45 | } | ||
| 26 | 46 | ||
| 27 | if(seconds < 10) return 'just now'; | 47 | if (seconds < 10) return "just now"; |
| 28 | 48 | ||
| 29 | return Math.floor(seconds) + ' seconds ago'; | 49 | return Math.floor(seconds) + " seconds ago"; |
| 30 | }; | 50 | } |
| 31 | 51 | ||
| 32 | export function ticks_to_time(ticks: number) { | 52 | export function ticks_to_time(ticks: number) { |
| 33 | let seconds = Math.floor(ticks / 60) | 53 | let seconds = Math.floor(ticks / 60); |
| 34 | let minutes = Math.floor(seconds / 60) | 54 | let minutes = Math.floor(seconds / 60); |
| 35 | let hours = Math.floor(minutes / 60) | 55 | let hours = Math.floor(minutes / 60); |
| 36 | 56 | ||
| 37 | let milliseconds = Math.floor((ticks % 60) * 1000 / 60) | 57 | let milliseconds = Math.floor(((ticks % 60) * 1000) / 60); |
| 38 | seconds = seconds % 60; | 58 | seconds = seconds % 60; |
| 39 | minutes = minutes % 60; | 59 | minutes = minutes % 60; |
| 40 | 60 | ||
| 41 | return `${hours === 0 ? "" : hours + ":"}${minutes === 0 ? "" : hours > 0 ? minutes.toString().padStart(2, '0') + ":" : (minutes + ":")}${minutes > 0 ? seconds.toString().padStart(2, '0') : seconds}.${milliseconds.toString().padStart(3, '0')}`; | 61 | return `${hours === 0 ? "" : hours + ":"}${minutes === 0 ? "" : hours > 0 ? minutes.toString().padStart(2, "0") + ":" : minutes + ":"}${minutes > 0 ? seconds.toString().padStart(2, "0") : seconds}.${milliseconds.toString().padStart(3, "0")}`; |
| 42 | }; \ No newline at end of file | 62 | } |