|
| 1 | +<!-- This file is part of Flus Browser |
| 2 | + -- SPDX-License-Identifier: AGPL-3.0-or-later |
| 3 | + --> |
| 4 | + |
| 5 | +<template> |
| 6 | + <Screen :title="title" header> |
| 7 | + <div v-if="ready && alert.type === ''" class="flow"> |
| 8 | + <p class="text--center"> |
| 9 | + {{ t("feeds.count_detected", feeds.length) }} |
| 10 | + </p> |
| 11 | + |
| 12 | + <ul v-if="feeds.length > 0" class="list--nostyle list--padded list--separated"> |
| 13 | + <li v-for="feed in feeds" class="cols cols--center cols--always cols--gap-smaller"> |
| 14 | + <span class="col--extend"> |
| 15 | + {{ feed.name }} |
| 16 | + </span> |
| 17 | + |
| 18 | + <div class="cols cols--always cols--center cols--gap-small"> |
| 19 | + <button |
| 20 | + v-if="!feed.isFollowed" |
| 21 | + @click.prevent="() => follow(feed)" |
| 22 | + > |
| 23 | + {{ t("feeds.follow") }} |
| 24 | + </button> |
| 25 | + |
| 26 | + <button |
| 27 | + v-else |
| 28 | + @click.prevent="() => unfollow(feed)" |
| 29 | + > |
| 30 | + {{ t("feeds.unfollow") }} |
| 31 | + </button> |
| 32 | + |
| 33 | + <a |
| 34 | + class="button button--icon" |
| 35 | + :href="feedUrl(feed)" |
| 36 | + :title="t('feeds.open_in_flus')" |
| 37 | + target="_blank" |
| 38 | + > |
| 39 | + <Icon name="pop-out" /> |
| 40 | + <span class="sr-only"> |
| 41 | + {{ t("feeds.open_in_flus") }} |
| 42 | + </span> |
| 43 | + </a> |
| 44 | + </div> |
| 45 | + </li> |
| 46 | + </ul> |
| 47 | + </div> |
| 48 | + |
| 49 | + <div v-else-if="ready && alert.type == 'info'"> |
| 50 | + <p class="panel panel--rounded panel--caribbean text--bold" role="alert"> |
| 51 | + <Icon name="info" /> |
| 52 | + |
| 53 | + {{ alert.message }} |
| 54 | + </p> |
| 55 | + </div> |
| 56 | + |
| 57 | + <div v-else-if="ready && alert.type == 'error'"> |
| 58 | + <p class="panel panel--rounded panel--danger text--bold" role="alert"> |
| 59 | + <Icon name="error" /> |
| 60 | + |
| 61 | + {{ t("forms.error") }} |
| 62 | + {{ alert.message }} |
| 63 | + </p> |
| 64 | + </div> |
| 65 | + |
| 66 | + <div v-else-if="!ready" class="flow text--center"> |
| 67 | + <div class="spinner"></div> |
| 68 | + |
| 69 | + <p> |
| 70 | + {{ t("feeds.loading") }} |
| 71 | + </p> |
| 72 | + </div> |
| 73 | + </Screen> |
| 74 | +</template> |
| 75 | + |
| 76 | +<script setup> |
| 77 | +import { ref, reactive, onMounted } from "vue"; |
| 78 | +import { useI18n } from "vue-i18n"; |
| 79 | +import browser from "webextension-polyfill"; |
| 80 | +
|
| 81 | +import { store } from "../store.js"; |
| 82 | +import api from "../api.js"; |
| 83 | +import http from "../http.js"; |
| 84 | +import Feed from "../models/feed.js"; |
| 85 | +
|
| 86 | +const { t, locale } = useI18n(); |
| 87 | +locale.value = store.locale; |
| 88 | +
|
| 89 | +const title = t("feeds.title"); |
| 90 | +
|
| 91 | +const ready = ref(false); |
| 92 | +const alert = ref({ |
| 93 | + type: "", |
| 94 | + message: "", |
| 95 | +}); |
| 96 | +
|
| 97 | +const feeds = ref([]); |
| 98 | +
|
| 99 | +function feedUrl(feed) { |
| 100 | + return `${store.auth.server}/collections/${feed.id}`; |
| 101 | +} |
| 102 | +
|
| 103 | +async function getCurrentTab() { |
| 104 | + return await browser.tabs |
| 105 | + .query({ |
| 106 | + active: true, |
| 107 | + currentWindow: true, |
| 108 | + }) |
| 109 | + .then((tabs) => { |
| 110 | + return tabs[0]; |
| 111 | + }); |
| 112 | +} |
| 113 | +
|
| 114 | +async function refreshForCurrentTab() { |
| 115 | + const url = (await getCurrentTab()).url; |
| 116 | +
|
| 117 | + if (!url.startsWith("http://") && !url.startsWith("https://")) { |
| 118 | + alert.value = { |
| 119 | + type: "info", |
| 120 | + message: t("link.invalid_protocol"), |
| 121 | + }; |
| 122 | +
|
| 123 | + ready.value = true; |
| 124 | +
|
| 125 | + return; |
| 126 | + } |
| 127 | +
|
| 128 | + api.search(url) |
| 129 | + .then((data) => { |
| 130 | + // This array is used to deduplicate feeds with the same name. |
| 131 | + // This usually happens when a site declares both Atom and RSS |
| 132 | + // feeds. In our case, it's generally disturbing for the user to |
| 133 | + // have several feeds with the same name. |
| 134 | + const feedsNames = []; |
| 135 | + data.feeds.forEach((searchedFeed) => { |
| 136 | + const feed = reactive(new Feed()); |
| 137 | + feed.init(searchedFeed); |
| 138 | + if (!feedsNames.includes(feed.name)) { |
| 139 | + feeds.value = [...feeds.value, feed]; |
| 140 | + feedsNames.push(feed.name); |
| 141 | + } |
| 142 | + }); |
| 143 | + ready.value = true; |
| 144 | + }) |
| 145 | + .catch((error) => { |
| 146 | + if (error instanceof http.HttpError) { |
| 147 | + alert.value = { |
| 148 | + type: "error", |
| 149 | + message: error.errors.url.map((error) => { |
| 150 | + return t(`errors.url.${error.code}`); |
| 151 | + }), |
| 152 | + }; |
| 153 | + } else { |
| 154 | + alert.value = { |
| 155 | + type: "error", |
| 156 | + message: t("errors.unknown"), |
| 157 | + }; |
| 158 | + } |
| 159 | +
|
| 160 | + ready.value = true; |
| 161 | + }); |
| 162 | +} |
| 163 | +
|
| 164 | +async function follow(feed) { |
| 165 | + api.follow(feed) |
| 166 | + .then(() => { |
| 167 | + feed.follow(); |
| 168 | + }) |
| 169 | + .catch(() => { |
| 170 | + alert.value = { |
| 171 | + type: "error", |
| 172 | + message: t("errors.unknown"), |
| 173 | + }; |
| 174 | + }); |
| 175 | +} |
| 176 | +
|
| 177 | +async function unfollow(feed) { |
| 178 | + api.unfollow(feed) |
| 179 | + .then(() => { |
| 180 | + feed.unfollow(); |
| 181 | + }) |
| 182 | + .catch(() => { |
| 183 | + alert.value = { |
| 184 | + type: "error", |
| 185 | + message: t("errors.unknown"), |
| 186 | + }; |
| 187 | + }); |
| 188 | +} |
| 189 | +
|
| 190 | +onMounted(refreshForCurrentTab); |
| 191 | +</script> |
0 commit comments