hardlounge/client/components/Username.vue

85 lines
2.1 KiB
Vue
Raw Normal View History

<template>
<span
:class="['user', {[nickColor]: store.state.settings.coloredNicks}, {active: active}]"
:data-name="user.nick"
role="button"
2020-01-27 09:44:36 +00:00
v-on="onHover ? {mouseenter: hover} : {}"
2019-11-23 14:26:20 +00:00
@click.prevent="openContextMenu"
@contextmenu.prevent="openContextMenu"
><slot>{{ mode }}{{ user.nick }}</slot></span
2019-07-17 09:33:59 +00:00
>
</template>
<script lang="ts">
import {computed, defineComponent, PropType} from "vue";
import {UserInMessage} from "../../server/models/msg";
2020-03-16 17:58:40 +00:00
import eventbus from "../js/eventbus";
2019-11-16 17:24:03 +00:00
import colorClass from "../js/helpers/colorClass";
import type {ClientChan, ClientNetwork, ClientUser} from "../js/types";
import {useStore} from "../js/store";
type UsernameUser = Partial<UserInMessage> & {
mode?: string;
nick: string;
};
export default defineComponent({
name: "Username",
props: {
user: {
// TODO: UserInMessage shouldn't be necessary here.
type: Object as PropType<UsernameUser | UserInMessage>,
required: true,
},
active: Boolean,
onHover: {
type: Function as PropType<(user: UserInMessage) => void>,
required: false,
},
channel: {type: Object as PropType<ClientChan>, required: false},
network: {type: Object as PropType<ClientNetwork>, required: false},
2018-07-10 23:25:21 +00:00
},
setup(props) {
const mode = computed(() => {
// Message objects have a singular mode, but user objects have modes array
if (props.user.modes) {
return props.user.modes[0];
}
return props.user.mode;
});
// TODO: Nick must be ! because our user prop union includes UserInMessage
const nickColor = computed(() => colorClass(props.user.nick!));
const hover = () => {
if (props.onHover) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return props.onHover(props.user as UserInMessage);
}
return null;
};
const openContextMenu = (event: Event) => {
2020-03-16 17:58:40 +00:00
eventbus.emit("contextmenu:user", {
2019-11-23 14:26:20 +00:00
event: event,
user: props.user,
network: props.network,
channel: props.channel,
2019-11-23 14:26:20 +00:00
});
};
const store = useStore();
return {
mode,
nickColor,
hover,
openContextMenu,
store,
};
},
});
</script>