-
-
Notifications
You must be signed in to change notification settings - Fork 230
/
Peers.vue
167 lines (140 loc) · 5.25 KB
/
Peers.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<script lang="ts" setup>
import { codeToFlag, formatData, formatPercent, formatSpeed, isWindows } from '@/helpers'
import { useMaindataStore, usePreferenceStore, useVueTorrentStore } from '@/stores'
import { Peer } from '@/types/qbit/models'
import { Torrent } from '@/types/vuetorrent'
import { onBeforeMount, onUnmounted, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
const props = defineProps<{ torrent: Torrent; isActive: boolean }>()
type PeerType = Peer & { host: string }
const { t } = useI18n()
const maindataStore = useMaindataStore()
const preferenceStore = usePreferenceStore()
const vuetorrentStore = useVueTorrentStore()
const loading = ref(false)
const torrentPeers = ref<PeerType[]>([])
const newPeers = ref('')
const timer = ref<NodeJS.Timeout | null>(null)
const addPeersDialog = ref(false)
async function updatePeers() {
loading.value = true
torrentPeers.value = Object.entries((await maindataStore.getTorrentPeers(props.torrent.hash)).peers).map(([host, peer]) => ({
...peer,
host
}))
loading.value = false
}
async function addPeers() {
if (!newPeers.value.length) return
await maindataStore.addTorrentPeers(props.torrent.hash, newPeers.value.split('\n'))
await updatePeers()
closeAddDialog()
}
function closeAddDialog() {
addPeersDialog.value = false
newPeers.value = ''
}
async function banPeer(peer: PeerType) {
await maindataStore.banPeers([peer.host])
await preferenceStore.fetchPreferences()
await updatePeers()
}
async function setupTimer(forceState?: boolean) {
if (forceState ?? props.isActive) {
await updatePeers()
timer.value = setInterval(updatePeers, 5000)
} else {
clearInterval(timer.value!)
timer.value = null
}
}
onBeforeMount(setupTimer)
onUnmounted(() => setupTimer(false))
watch(() => props.isActive, setupTimer)
</script>
<template>
<v-list>
<template v-for="(peer, index) in torrentPeers">
<v-divider v-if="index > 0" color="white" />
<v-list-item>
<div class="d-flex">
<div>
<v-list-item-title class="overflow-visible">
<span v-if="peer.country_code">
<img v-if="isWindows" :alt="codeToFlag(peer.country_code).char" :src="codeToFlag(peer.country_code).url" :title="peer.country" style="max-width: 32px" />
<span v-else :title="peer.country">{{ codeToFlag(peer.country_code).char }}</span>
</span>
<span>{{ peer.ip }}</span>
<span class="text-subtitle-2 text-grey"> :{{ peer.port }}</span>
</v-list-item-title>
<v-list-item-subtitle class="d-block">
<div>
Flags: <span class="cursor-help" :title="peer.flags_desc">{{ peer.flags }}</span>
</div>
<div>Progress: {{ formatPercent(peer.progress) }}</div>
<div>
<v-icon icon="mdi-arrow-down" color="download" />
{{ formatSpeed(peer.dl_speed, vuetorrentStore.useBitSpeed) }}
<v-icon icon="mdi-arrow-up" color="upload" />
{{ formatSpeed(peer.up_speed, vuetorrentStore.useBitSpeed) }}
</div>
<div>
{{ formatData(peer.downloaded, vuetorrentStore.useBinarySize) }}
|
{{ formatData(peer.uploaded, vuetorrentStore.useBinarySize) }}
</div>
<div>Relevance: {{ formatPercent(peer.relevance) }}</div>
</v-list-item-subtitle>
</div>
<v-spacer />
<div class="d-flex flex-column">
<v-btn color="red" icon="mdi-cancel" variant="text" @click="banPeer(peer)" />
</div>
</div>
</v-list-item>
</template>
<v-list-item v-if="torrentPeers.length === 0">
{{ $t('torrentDetail.peers.emptyList') }}
</v-list-item>
<v-list-item>
<div :class="['d-flex gap py-5', $vuetify.display.mobile ? 'flex-column' : 'justify-space-evenly']">
<v-dialog v-model="addPeersDialog" max-width="750px">
<template v-slot:activator="{ props }">
<v-btn v-bind="props" variant="flat" :text="t('torrentDetail.peers.addPeers.title')" color="accent" />
</template>
<v-card>
<v-card-title>
<span class="text-h5">{{ t('torrentDetail.peers.addPeers.title') }}</span>
</v-card-title>
<v-card-text>
<v-container>
<v-row>
<v-col cols="12">
<v-textarea
v-model="newPeers"
:label="t('torrentDetail.peers.addPeers.newPeers')"
:placeholder="t('torrentDetail.peers.addPeers.newPeersPlaceholder')"
:hint="t('torrentDetail.peers.addPeers.newPeersHint')" />
</v-col>
</v-row>
</v-container>
</v-card-text>
<v-card-actions>
<v-spacer />
<v-btn color="error" @click="closeAddDialog">{{ t('common.cancel') }}</v-btn>
<v-btn color="accent" @click="addPeers">{{ t('common.ok') }}</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</div>
</v-list-item>
</v-list>
</template>
<style scoped>
.gap {
gap: 8px;
}
.cursor-help {
cursor: help;
}
</style>