aboutsummaryrefslogtreecommitdiff
path: root/src-vue/src/components/ThunderstoreModCard.vue
blob: 2fbbac40dcc5f28689baf86ebace05ecedfd199e (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<template>
    <el-card :body-style="getBodyStyle" :style="getCardStyle">
        <img
            :src="latestVersion.icon"
            class="image"
        />
        <div style="padding: 0 10px 10px;">
            <span class="statContainer">
                <el-icon class="no-inherit">
                    <Download />
                </el-icon>
                {{ modDownloadsCount }}
            </span>

            <span class="statContainer">
                {{ mod.rating_score }}
                <el-icon class="no-inherit">
                    <Star />
                </el-icon>
            </span>
            <br />

            <div class="name hide-text-overflow">{{ mod.name }}</div>
            <div class="author hide-text-overflow">{{ $t('mods.card.by') }} {{ mod.owner }}</div>
            <div class="desc">
                {{ latestVersion.description }}
            </div>

            <span style="display: flex">
                <el-button
                    :type="modButtonType"
                    style="flex: 6"
                    :loading="isBeingInstalled || isBeingUpdated"
                    @click.stop="installMod(mod)"
                >
                    {{ $t(modButtonText) }}
                </el-button>

                <!-- Information dropdown menu -->
                 <el-button v-if="!modIsRemovable"
                            link type="info" class="infoBtn" @click="openURL(mod.package_url)">
                    <el-icon>
                        <InfoFilled />
                    </el-icon>
                </el-button>

                <el-dropdown v-else>
                    <el-icon class="infoBtn moreBtn">
                        <MoreFilled />
                    </el-icon>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item @click="openURL(mod.package_url)">
                                {{ $t('mods.card.more_info') }}
                            </el-dropdown-item>
                            <el-dropdown-item @click="deleteMod(mod)">
                                {{ $t('mods.card.remove') }}
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </span>
        </div>
    </el-card>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import { ThunderstoreMod } from "../../../src-tauri/bindings/ThunderstoreMod";
import { ThunderstoreModVersion } from "../../../src-tauri/bindings/ThunderstoreModVersion";
import { invoke, shell } from "@tauri-apps/api";
import { ThunderstoreModStatus } from "../utils/thunderstore/ThunderstoreModStatus";
import { NorthstarMod } from "../../../src-tauri/bindings/NorthstarMod";
import { GameInstall } from "../utils/GameInstall";
import { NorthstarState } from "../utils/NorthstarState";
import { ElMessageBox } from "element-plus";
import { showErrorNotification, showNotification } from "../utils/ui";

export default defineComponent({
    name: "ThunderstoreModCard",
    props: {
        mod: {
            required: true,
            type: Object as () => ThunderstoreMod
        }
    },
    data: () => ({
        isBeingInstalled: false,
        isBeingUpdated: false
    }),
    computed: {
        getBodyStyle(): Object {
            return this.mod.is_deprecated ? { 'background-color': 'rgba(255, 0, 0, 0.42)' } : {};
        },

        getCardStyle(): Object {
            return this.mod.is_deprecated ? { 'border': '1px solid red' } : {};
        },

        latestVersion(): ThunderstoreModVersion {
            return this.mod.versions[0];
        },

        /**
         * Returns the status of a given mod.
         */
        modStatus(): ThunderstoreModStatus {
            if (this.isBeingInstalled) {
                return ThunderstoreModStatus.BEING_INSTALLED;
            }
            if (this.isBeingUpdated) {
                return ThunderstoreModStatus.BEING_UPDATED;
            }

            // Ensure mod is up-to-date.
            const tsModPrefix = this.getThunderstoreDependencyStringPrefix(this.latestVersion.full_name);
            const matchingMods: NorthstarMod[] = this.$store.state.installed_mods.filter((mod: NorthstarMod) => {
                if (!mod.thunderstore_mod_string) return false;
                return this.getThunderstoreDependencyStringPrefix(mod.thunderstore_mod_string!) === tsModPrefix;
            });
            if (matchingMods.length !== 0) {
                // There shouldn't be several mods with same dependency string, but we never know...
                const matchingMod = matchingMods[0];
                // A mod is outdated if its dependency strings differs from Thunderstore dependency string
                // (no need for semver check here)
                return matchingMod.thunderstore_mod_string === this.latestVersion.full_name
                    ? ThunderstoreModStatus.INSTALLED
                    : ThunderstoreModStatus.OUTDATED;
            }

            return ThunderstoreModStatus.NOT_INSTALLED;
        },

        /**
         * Returns button text associated to a mod.
         */
        modButtonText(): string {
            switch (this.modStatus) {
                case ThunderstoreModStatus.BEING_INSTALLED:
                    return "mods.card.button.being_installed";
                case ThunderstoreModStatus.BEING_UPDATED:
                    return "mods.card.button.being_updated";
                case ThunderstoreModStatus.INSTALLED:
                    return "mods.card.button.installed";
                case ThunderstoreModStatus.NOT_INSTALLED:
                    return "mods.card.button.install";
                case ThunderstoreModStatus.OUTDATED:
                    return "mods.card.button.outdated";
            }
        },

        /**
         * Returns button type associated to a mod.
         */
        modButtonType(): string {
            switch (this.modStatus) {
                case ThunderstoreModStatus.BEING_INSTALLED:
                    return "primary";
                case ThunderstoreModStatus.INSTALLED:
                    return "success";
                case ThunderstoreModStatus.NOT_INSTALLED:
                    return "primary";
                case ThunderstoreModStatus.OUTDATED:
                case ThunderstoreModStatus.BEING_UPDATED:
                    return "warning";
            }
        },

        /**
         * Tells if a Thunderstore mod can be removed.
         * This is used to tell if we should display the "Remove mod" option.
         **/
        modIsRemovable(): boolean {
            return [ThunderstoreModStatus.INSTALLED, ThunderstoreModStatus.OUTDATED]
                .includes(this.modStatus);
        },

        /**
         * This computes the total count of downloads of a given mod, by adding
         * download count of each of its releases.
         */
        modDownloadsCount(): number {
            let totalDownloads = 0;
            this.mod.versions.map((version: ThunderstoreModVersion) => totalDownloads += version.downloads);
            return totalDownloads;
        },
    },
    methods: {
        /**
         * This opens an URL in user's favorite web browser.
         * This is used to open Thunderstore mod pages.
         */
        openURL(url: string): void {
            shell.open(url);
        },

        /**
         * Strips off a Thunderstore dependency string from its version
         * (e.g. "taskinoz-WallrunningTitans-1.0.0" to
         * "taskinoz-WallrunningTitans").
         */
        getThunderstoreDependencyStringPrefix(dependency: string): string {
            const dependencyStringMembers = dependency.split('-');
            return `${dependencyStringMembers[0]}-${dependencyStringMembers[1]}`;
        },

        async deleteMod(mod: ThunderstoreMod) {

            // Show pop-up to confirm delete
            ElMessageBox.confirm(
                this.$t('mods.card.remove_dialog_text'),
                this.$t('mods.card.remove_dialog_title'),
                {
                    confirmButtonText: this.$t('generic.yes'),
                    cancelButtonText: this.$t('generic.cancel'),
                    type: 'warning',
                }
            )
                .then(async () => { // Deletion confirmed
                    let game_install = {
                        game_path: this.$store.state.game_path,
                        install_type: this.$store.state.install_type
                    } as GameInstall;

                    await invoke<string>("delete_thunderstore_mod", { gameInstall: game_install, thunderstoreModString: this.latestVersion.full_name })
                        .then((message) => {
                            showNotification(this.$t('mods.card.remove_success', { modName: mod.name }), message);
                        })
                        .catch((error) => {
                            showErrorNotification(error);
                        })
                        .finally(() => {
                            this.$store.commit('loadInstalledMods');
                        });
                })
                .catch(() => { // Deletion cancelled
                    console.log("Deleting Thunderstore mod cancelled.")
                })
        },

        async installMod(mod: ThunderstoreMod) {
            let game_install = {
                game_path: this.$store.state.game_path,
                install_type: this.$store.state.install_type
            } as GameInstall;

            // set internal state according to current installation state
            if (this.modStatus === ThunderstoreModStatus.OUTDATED) {
                this.isBeingUpdated = true;
            } else {
                this.isBeingInstalled = true;
            }

            // Capture translation method in a context, so it can be used outside Vue component context.
            // (see https://github.com/R2NorthstarTools/FlightCore/issues/384)
            (async (translate: Function) => {
                await invoke<string>("install_mod_caller", { gameInstall: game_install, thunderstoreModString: this.latestVersion.full_name }).then((message) => {
                showNotification(translate('mods.card.install_success', { modName: mod.name }), message);
            })
                .catch((error) => {
                    showErrorNotification(error);
                })
                .finally(() => {
                    this.isBeingInstalled = false;
                    this.isBeingUpdated = false;
                    this.$store.commit('loadInstalledMods');
                });
            // @ts-ignore
            })(this.$i18n.t);

        },
    }
});
</script>

<style scoped>
.el-card {
    display: inline-block;
    max-width: 178px;
    margin: 5px;
    --el-card-padding: 0;
}

.deprecated {
    background-color: red !important;
}

.author {
    font-size: 14px;
    font-style: italic;
}

.hide-text-overflow {
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
}

.desc {
    font-size: 12px;
    margin: 8px 0 16px;
    height: 57px;
    text-overflow: ellipsis;
    overflow: hidden;
}

.statContainer {
    font-size: 14px;
}

.statContainer:nth-child(2) {
    float: right;
}

.infoBtn {
    width: 20px;
    padding: 0 !important;
    font-size: 20px;
    border: none;
}

.moreBtn {
    margin-left: 10px;
    height: auto;
}
</style>