|
14 | 14 | ~ along with this program. If not, see <http://www.gnu.org/licenses/>. |
15 | 15 | --> |
16 | 16 | <script> |
| 17 | + import { stores } from '../../stores'; |
| 18 | + import { setUserCookies } from '../../util/auth'; |
| 19 | + import { validateCookie } from '../../util/halo'; |
| 20 | + import { auth, getHaloCookies, isValidCookieObject } from '../../util/util'; |
| 21 | +
|
17 | 22 | export let user; |
18 | 23 |
|
19 | 24 | // ------- state ------- |
20 | 25 | let isDropdownOpen = false; |
| 26 | + let isSyncingInfo = false; |
21 | 27 |
|
22 | 28 | // daisyui dropdown stuff from https://svelte.dev/repl/4c5dfd34cc634774bd242725f0fc2dab |
23 | 29 | const handleDropdownClick = () => (isDropdownOpen = !isDropdownOpen); |
24 | 30 | const handleDropdownFocusLoss = ({ relatedTarget, currentTarget }) => { |
25 | 31 | if (relatedTarget instanceof HTMLElement && currentTarget.contains(relatedTarget)) return; |
26 | 32 | isDropdownOpen = false; |
27 | 33 | }; |
| 34 | +
|
| 35 | + const syncHaloInfo = async function () { |
| 36 | + if (isSyncingInfo) return; |
| 37 | + isSyncingInfo = true; |
| 38 | + try { |
| 39 | + console.log('sweeping cookies - manual'); |
| 40 | + const cookies = await getHaloCookies(); |
| 41 | + if (!isValidCookieObject(cookies)) throw new Error('Invalid cookie object'); |
| 42 | + if (!(await validateCookie({ cookie: cookies }))) throw new Error('cookie failed validateCookie check'); |
| 43 | +
|
| 44 | + stores.halo_cookies.update(cookies); |
| 45 | + !!auth.currentUser && (await setUserCookies({ uid: auth.currentUser.uid, cookies })); |
| 46 | + } catch (e) { |
| 47 | + console.error('[error] sweping cookies manual', e); |
| 48 | + alert('Failed to resync Halo info'); |
| 49 | + } finally { |
| 50 | + isSyncingInfo = false; |
| 51 | + } |
| 52 | + }; |
28 | 53 | </script> |
29 | 54 |
|
30 | 55 | <div class="navbar"> |
|
48 | 73 | class="mt-3 p-2 shadow menu menu-compact dropdown-content rounded-box bg-base-200 w-52" |
49 | 74 | style:visibility={isDropdownOpen ? 'visible' : 'hidden'} |
50 | 75 | > |
51 | | - <li><a>{user.username}#{user.discriminator}</a></li> |
52 | | - <!-- <li><a>Logout</a></li> --> |
| 76 | + <li> |
| 77 | + <a>{user.username}#{user.discriminator} </a> |
| 78 | + </li> |
| 79 | + <li on:click={syncHaloInfo}> |
| 80 | + <a |
| 81 | + >Sync Halo Info |
| 82 | + {#if isSyncingInfo} |
| 83 | + <div class="badge badge-lg"> |
| 84 | + <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 512 512" |
| 85 | + ><circle cx="256" cy="256" r="48" /><circle cx="416" cy="256" r="48" /><circle |
| 86 | + cx="96" |
| 87 | + cy="256" |
| 88 | + r="48" |
| 89 | + /></svg |
| 90 | + > |
| 91 | + </div> |
| 92 | + {:else} |
| 93 | + <div class="badge badge-lg"> |
| 94 | + <svg |
| 95 | + xmlns="http://www.w3.org/2000/svg" |
| 96 | + width="16" |
| 97 | + height="16" |
| 98 | + fill="currentColor" |
| 99 | + class="bi bi-check" |
| 100 | + viewBox="0 0 16 16" |
| 101 | + > |
| 102 | + <path |
| 103 | + d="M10.97 4.97a.75.75 0 0 1 1.07 1.05l-3.99 4.99a.75.75 0 0 1-1.08.02L4.324 8.384a.75.75 0 1 1 1.06-1.06l2.094 2.093 3.473-4.425a.267.267 0 0 1 .02-.022z" |
| 104 | + /> |
| 105 | + </svg> |
| 106 | + </div> |
| 107 | + {/if} |
| 108 | + </a> |
| 109 | + </li> |
53 | 110 | </ul> |
54 | 111 | </div> |
55 | 112 | </div> |
|
0 commit comments