hippofish/packages/client/src/components/MkRenoteButton.vue

283 lines
6.3 KiB
Vue
Raw Normal View History

<template>
2023-04-08 02:01:42 +02:00
<button
v-if="canRenote"
ref="buttonRef"
v-tooltip.noDelay.bottom="i18n.ts.renote"
class="button _button"
2023-06-03 18:57:51 +02:00
:class="{ renoted: hasRenotedBefore }"
@click.stop="renote(false, $event)"
2023-04-08 02:01:42 +02:00
>
<i :class="icon('ph-rocket-launch')"></i>
2023-05-19 19:02:41 +02:00
<p v-if="count > 0 && !detailedView" class="count">{{ count }}</p>
2023-04-08 02:01:42 +02:00
</button>
<button
v-else
2023-09-02 01:27:33 +02:00
v-tooltip.noDelay.bottom="i18n.ts.disabled"
class="_button"
disabled="true"
>
<i :class="icon('ph-rocket-launch')"></i>
2023-04-08 02:01:42 +02:00
</button>
</template>
2022-07-24 08:45:16 +02:00
<script lang="ts" setup>
2023-04-08 02:01:42 +02:00
import { computed, ref } from "vue";
2023-09-24 06:27:16 +02:00
import type * as firefish from "firefish-js";
2023-04-08 02:01:42 +02:00
import Ripple from "@/components/MkRipple.vue";
import XDetails from "@/components/MkUsersTooltip.vue";
import { pleaseLogin } from "@/scripts/please-login";
import * as os from "@/os";
import { $i, isSignedIn } from "@/reactiveAccount";
2023-04-08 02:01:42 +02:00
import { useTooltip } from "@/scripts/use-tooltip";
import { i18n } from "@/i18n";
import { defaultStore } from "@/store";
2023-09-02 01:27:33 +02:00
import type { MenuItem } from "@/types/menu";
2023-09-17 23:59:09 +02:00
import { vibrate } from "@/scripts/vibrate";
import icon from "@/scripts/icon";
2022-07-24 08:45:16 +02:00
const props = defineProps<{
2023-09-24 06:27:16 +02:00
note: firefish.entities.Note;
2023-04-08 02:01:42 +02:00
count: number;
2023-05-19 19:02:41 +02:00
detailedView?;
2023-04-08 02:01:42 +02:00
}>();
2022-07-24 08:45:16 +02:00
const buttonRef = ref<HTMLElement>();
2023-04-08 02:01:42 +02:00
const canRenote = computed(
() =>
["public", "home"].includes(props.note.visibility) ||
2023-07-06 03:28:27 +02:00
props.note.userId === $i.id,
2023-04-08 02:01:42 +02:00
);
2022-07-24 08:45:16 +02:00
useTooltip(buttonRef, async (showing) => {
2023-04-08 02:01:42 +02:00
const renotes = await os.api("notes/renotes", {
2022-07-24 08:45:16 +02:00
noteId: props.note.id,
limit: 11,
});
2023-04-08 02:01:42 +02:00
const users = renotes.map((x) => x.user);
2022-07-24 08:45:16 +02:00
if (users.length < 1) return;
2023-04-08 02:01:42 +02:00
os.popup(
XDetails,
{
showing,
users,
count: props.count,
targetElement: buttonRef.value,
},
{},
2023-07-06 03:28:27 +02:00
"closed",
2023-04-08 02:01:42 +02:00
);
2022-07-24 08:45:16 +02:00
});
2023-09-02 01:27:33 +02:00
const hasRenotedBefore = ref(false);
2023-10-19 07:46:20 +02:00
if (isSignedIn) {
2023-10-19 07:46:20 +02:00
os.api("notes/renotes", {
noteId: props.note.id,
userId: $i.id,
limit: 1,
}).then((res) => {
hasRenotedBefore.value = res.length > 0;
});
}
2023-06-03 18:57:51 +02:00
const renote = (viaKeyboard = false, ev?: MouseEvent) => {
pleaseLogin();
2023-09-02 01:27:33 +02:00
const buttonActions: Array<MenuItem> = [];
if (props.note.visibility === "public") {
buttonActions.push({
text: i18n.ts.renote,
icon: `${icon("ph-rocket-launch")}`,
danger: false,
action: () => {
2023-04-08 02:01:42 +02:00
os.api("notes/create", {
renoteId: props.note.id,
2023-04-08 02:01:42 +02:00
visibility: "public",
});
hasRenotedBefore.value = true;
2023-04-08 02:01:42 +02:00
const el =
ev &&
((ev.currentTarget ?? ev.target) as
| HTMLElement
| null
| undefined);
if (el) {
const rect = el.getBoundingClientRect();
2023-04-08 02:01:42 +02:00
const x = rect.left + el.offsetWidth / 2;
const y = rect.top + el.offsetHeight / 2;
os.popup(Ripple, { x, y }, {}, "end");
}
},
});
}
if (["public", "home"].includes(props.note.visibility)) {
buttonActions.push({
2023-04-13 04:58:42 +02:00
text: `${i18n.ts.renote} (${i18n.ts._visibility.home})`,
icon: `${icon("ph-house")}`,
danger: false,
action: () => {
2023-04-08 02:01:42 +02:00
os.api("notes/create", {
renoteId: props.note.id,
2023-04-08 02:01:42 +02:00
visibility: "home",
});
hasRenotedBefore.value = true;
2023-04-08 02:01:42 +02:00
const el =
ev &&
((ev.currentTarget ?? ev.target) as
| HTMLElement
| null
| undefined);
if (el) {
const rect = el.getBoundingClientRect();
2023-04-08 02:01:42 +02:00
const x = rect.left + el.offsetWidth / 2;
const y = rect.top + el.offsetHeight / 2;
os.popup(Ripple, { x, y }, {}, "end");
}
},
});
}
2023-04-08 02:01:42 +02:00
if (props.note.visibility === "specified") {
buttonActions.push({
2023-04-13 04:58:42 +02:00
text: `${i18n.ts.renote} (${i18n.ts.recipient})`,
icon: `${icon("ph-envelope-simple-open")}`,
danger: false,
action: () => {
2023-04-08 02:01:42 +02:00
os.api("notes/create", {
renoteId: props.note.id,
2023-04-08 02:01:42 +02:00
visibility: "specified",
visibleUserIds: props.note.visibleUserIds,
});
hasRenotedBefore.value = true;
2023-04-08 02:01:42 +02:00
const el =
ev &&
((ev.currentTarget ?? ev.target) as
| HTMLElement
| null
| undefined);
if (el) {
const rect = el.getBoundingClientRect();
2023-04-08 02:01:42 +02:00
const x = rect.left + el.offsetWidth / 2;
const y = rect.top + el.offsetHeight / 2;
os.popup(Ripple, { x, y }, {}, "end");
}
},
});
} else {
buttonActions.push({
2023-04-13 04:58:42 +02:00
text: `${i18n.ts.renote} (${i18n.ts._visibility.followers})`,
icon: `${icon("ph-lock")}`,
danger: false,
action: () => {
2023-04-08 02:01:42 +02:00
os.api("notes/create", {
renoteId: props.note.id,
2023-04-08 02:01:42 +02:00
visibility: "followers",
});
hasRenotedBefore.value = true;
2023-04-08 02:01:42 +02:00
const el =
ev &&
((ev.currentTarget ?? ev.target) as
| HTMLElement
| null
| undefined);
if (el) {
const rect = el.getBoundingClientRect();
2023-04-08 02:01:42 +02:00
const x = rect.left + el.offsetWidth / 2;
const y = rect.top + el.offsetHeight / 2;
os.popup(Ripple, { x, y }, {}, "end");
}
},
});
}
2022-12-02 08:19:37 +01:00
2023-09-02 01:27:33 +02:00
if (canRenote.value) {
2023-05-07 01:26:24 +02:00
buttonActions.push({
2023-05-11 13:10:19 +02:00
text: `${i18n.ts.renote} (${i18n.ts.local})`,
icon: `${icon("ph-users")}`,
2023-05-11 13:10:19 +02:00
danger: false,
action: () => {
2023-09-17 23:59:09 +02:00
vibrate([30, 30, 60]);
2023-05-11 13:28:32 +02:00
os.api(
"notes/create",
2023-05-11 13:10:19 +02:00
props.note.visibility === "specified"
2023-05-11 13:28:32 +02:00
? {
renoteId: props.note.id,
visibility: props.note.visibility,
visibleUserIds: props.note.visibleUserIds,
localOnly: true,
2024-02-10 18:30:10 +01:00
}
2023-05-11 13:28:32 +02:00
: {
renoteId: props.note.id,
visibility: props.note.visibility,
localOnly: true,
2024-02-10 18:30:10 +01:00
},
2023-05-11 13:28:32 +02:00
);
hasRenotedBefore.value = true;
2023-05-11 13:10:19 +02:00
const el =
ev &&
((ev.currentTarget ?? ev.target) as
| HTMLElement
| null
| undefined);
if (el) {
const rect = el.getBoundingClientRect();
2023-04-08 02:01:42 +02:00
const x = rect.left + el.offsetWidth / 2;
const y = rect.top + el.offsetHeight / 2;
os.popup(Ripple, { x, y }, {}, "end");
}
},
2023-05-07 01:26:24 +02:00
});
}
if (!defaultStore.state.seperateRenoteQuote) {
buttonActions.push({
2022-09-28 18:22:03 +02:00
text: i18n.ts.quote,
icon: `${icon("ph-quotes")}`,
2022-12-02 08:28:16 +01:00
danger: false,
2022-09-28 18:22:03 +02:00
action: () => {
os.post({
renote: props.note,
});
},
});
}
if (hasRenotedBefore.value) {
buttonActions.push({
text: i18n.ts.unrenote,
icon: `${icon("ph-trash")}`,
danger: true,
action: () => {
2023-04-08 02:01:42 +02:00
os.api("notes/unrenote", {
noteId: props.note.id,
});
hasRenotedBefore.value = false;
},
});
}
2023-06-13 02:06:42 +02:00
buttonActions[0].textStyle = "font-weight: bold";
os.popupMenu(buttonActions, buttonRef.value, { viaKeyboard });
2022-07-24 08:45:16 +02:00
};
</script>
2023-04-08 02:01:42 +02:00
<style lang="scss" scoped>
2023-05-24 02:30:55 +02:00
.button {
&:not(.button) {
2023-04-08 02:01:42 +02:00
cursor: default;
}
&.renoted {
2023-06-03 18:57:51 +02:00
color: var(--accent) !important;
opacity: 1 !important;
2023-06-03 19:43:08 +02:00
font-weight: 700;
2023-04-08 02:01:42 +02:00
}
}
</style>