|
| 1 | +<script setup lang="ts"> |
| 2 | +import { useI18n } from "vue-i18n"; |
| 3 | +import Spinner from "@repo/ui/src/components/progress/Spinner.vue"; |
| 4 | +import { ref } from "vue"; |
| 5 | +import type { SessionInfo } from "../../utils/types.ts"; |
| 6 | +import { notifyErr, requestWithToken } from "../../utils/network.ts"; |
| 7 | +import Tag from "@repo/ui/src/components/misc/tag/Tag.vue"; |
| 8 | +import Button from "@repo/ui/src/components/button/Button.vue"; |
| 9 | +import { formatDate } from "@repo/ui/src/utils/utils.ts"; |
| 10 | +import dayjs from "dayjs"; |
| 11 | +import { MCSLNotif } from "@repo/ui/src/utils/notifications.ts"; |
| 12 | +import ConfirmModal from "@repo/shared/src/components/ConfirmModal.vue"; |
| 13 | +
|
| 14 | +const t = useI18n().t; |
| 15 | +
|
| 16 | +const sessions = ref<SessionInfo[]>(); |
| 17 | +let sessionToDelete = ""; |
| 18 | +const showDeleteConfirm = ref(false); |
| 19 | +
|
| 20 | +async function refresh() { |
| 21 | + sessions.value = await requestWithToken<SessionInfo[]>( |
| 22 | + "/session/self", |
| 23 | + "GET", |
| 24 | + (e) => notifyErr(e, "web.user-center.sessions.error"), |
| 25 | + ); |
| 26 | +} |
| 27 | +
|
| 28 | +async function deleteSession() { |
| 29 | + await requestWithToken<void>("/session/" + sessionToDelete, "DELETE", (e) => |
| 30 | + notifyErr(e, "web.user-center.sessions.delete.error"), |
| 31 | + ); |
| 32 | +
|
| 33 | + new MCSLNotif({ |
| 34 | + data: { |
| 35 | + color: "success", |
| 36 | + title: t("ui.notification.title.success"), |
| 37 | + message: t("web.user-center.sessions.delete.success"), |
| 38 | + }, |
| 39 | + }).open(); |
| 40 | +
|
| 41 | + await refresh(); |
| 42 | +} |
| 43 | +
|
| 44 | +refresh(); |
| 45 | +</script> |
| 46 | + |
| 47 | +<template> |
| 48 | + <div class="sessions"> |
| 49 | + <ConfirmModal |
| 50 | + v-model:visible="showDeleteConfirm" |
| 51 | + :title="t('web.user-center.sessions.delete.confirm')" |
| 52 | + @confirm="deleteSession" |
| 53 | + /> |
| 54 | + <table v-if="sessions" class="sessions__table"> |
| 55 | + <thead> |
| 56 | + <tr> |
| 57 | + <th>{{ t("web.user-center.sessions.info.type.label") }}</th> |
| 58 | + <th>{{ t("web.user-center.sessions.info.user-agent") }}</th> |
| 59 | + <th>{{ t("web.user-center.sessions.info.created-at") }}</th> |
| 60 | + <th>{{ t("web.user-center.sessions.info.last-active-ip") }}</th> |
| 61 | + <th>{{ t("web.user-center.sessions.info.last-active-at") }}</th> |
| 62 | + <th>{{ t("ui.common.actions") }}</th> |
| 63 | + </tr> |
| 64 | + </thead> |
| 65 | + <tbody> |
| 66 | + <tr v-for="session in sessions" :key="session.token_id"> |
| 67 | + <td> |
| 68 | + <Tag :color="session.remember ? 'primary' : 'default'"> |
| 69 | + {{ |
| 70 | + t( |
| 71 | + "web.user-center.sessions.info.type." + |
| 72 | + (session.remember ? "remember" : "temporary"), |
| 73 | + ) |
| 74 | + }} |
| 75 | + </Tag> |
| 76 | + </td> |
| 77 | + <td v-tooltip="session.user_agent" class="sessions__text-eclipse"> |
| 78 | + {{ session.user_agent }} |
| 79 | + </td> |
| 80 | + <td> |
| 81 | + {{ formatDate(dayjs(session.created_at)) }} |
| 82 | + </td> |
| 83 | + <td> |
| 84 | + {{ session.last_active_ip }} |
| 85 | + </td> |
| 86 | + <td> |
| 87 | + {{ formatDate(dayjs(session.last_active_at)) }} |
| 88 | + </td> |
| 89 | + <td> |
| 90 | + <Button |
| 91 | + icon="fa-solid fa-trash-can" |
| 92 | + color="danger" |
| 93 | + rounded |
| 94 | + size="small" |
| 95 | + v-tooltip="t('ui.common.delete')" |
| 96 | + @click=" |
| 97 | + () => { |
| 98 | + sessionToDelete = session.token_id; |
| 99 | + showDeleteConfirm = true; |
| 100 | + } |
| 101 | + " |
| 102 | + /> |
| 103 | + </td> |
| 104 | + </tr> |
| 105 | + </tbody> |
| 106 | + </table> |
| 107 | + <Spinner v-else class="sessions__spinner" /> |
| 108 | + </div> |
| 109 | +</template> |
| 110 | + |
| 111 | +<style scoped lang="scss"> |
| 112 | +.sessions { |
| 113 | + overflow: auto; |
| 114 | +} |
| 115 | +
|
| 116 | +.sessions__table { |
| 117 | + min-width: 540px; |
| 118 | +} |
| 119 | +
|
| 120 | +.sessions__text-eclipse { |
| 121 | + max-width: 80px; |
| 122 | + overflow: hidden; |
| 123 | + text-overflow: ellipsis; |
| 124 | + white-space: nowrap; |
| 125 | + cursor: help; |
| 126 | +} |
| 127 | +
|
| 128 | +.sessions__spinner { |
| 129 | + height: 6rem; |
| 130 | + display: flex; |
| 131 | + justify-content: center; |
| 132 | + align-items: center; |
| 133 | +} |
| 134 | +</style> |
0 commit comments