Persist workspace tabs in the app shell with Ant Design styling aligned to the desktop top bar, and fix tab bar layout flicker on select. Add shared PageHeader components, query helpers, and missing header state across CRM views; improve mobile shell layout and Vite HMR stability on Windows. Co-authored-by: Cursor <cursoragent@cursor.com>
540 lines
11 KiB
Vue
540 lines
11 KiB
Vue
<script setup lang="ts">
|
|
import { computed, nextTick, onMounted, onUnmounted, ref } from 'vue';
|
|
import axios from 'axios';
|
|
import { useAuthStore } from '@/stores/auth';
|
|
import { USER_ROLE_LABELS } from '@/lib/roles';
|
|
import type { OrganizationMembershipDto } from '@mento/shared';
|
|
import UiIcon from '@/components/UiIcon.vue';
|
|
|
|
const props = withDefaults(
|
|
defineProps<{
|
|
compact?: boolean;
|
|
variant?: 'default' | 'workspace';
|
|
}>(),
|
|
{
|
|
compact: false,
|
|
variant: 'default',
|
|
},
|
|
);
|
|
|
|
const emit = defineEmits<{
|
|
open: [];
|
|
close: [];
|
|
}>();
|
|
|
|
const auth = useAuthStore();
|
|
|
|
const orgs = ref<OrganizationMembershipDto[]>([]);
|
|
const open = ref(false);
|
|
const loading = ref(false);
|
|
const error = ref('');
|
|
const rootEl = ref<HTMLElement | null>(null);
|
|
const menuEl = ref<HTMLElement | null>(null);
|
|
const menuStyle = ref<Record<string, string>>({});
|
|
let ignoreDocClickUntil = 0;
|
|
|
|
const currentName = computed(
|
|
() =>
|
|
auth.user?.organizationName ||
|
|
orgs.value.find((o) => o.id === auth.user?.organizationId)?.name ||
|
|
'محیط کاری',
|
|
);
|
|
|
|
const currentSubtitle = computed(() => {
|
|
const current = orgs.value.find((o) => o.id === auth.user?.organizationId);
|
|
if (current?.orgRoleName) return current.orgRoleName;
|
|
if (auth.user?.role) return USER_ROLE_LABELS[auth.user.role];
|
|
return 'محیط کاری';
|
|
});
|
|
|
|
const currentInitial = computed(() => {
|
|
const name = currentName.value.trim();
|
|
return name ? name.charAt(0) : 'م';
|
|
});
|
|
|
|
const canSwitch = computed(() => orgs.value.length > 1);
|
|
const isWorkspace = computed(() => props.variant === 'workspace');
|
|
/** فقط وقتی بیش از یک محیط باشد نمایش و تعویض فعال است */
|
|
const menuEnabled = computed(() => canSwitch.value);
|
|
|
|
function apiError(e: unknown, fallback: string) {
|
|
if (axios.isAxiosError(e)) {
|
|
const msg = (e.response?.data as { message?: string | string[] })?.message;
|
|
return Array.isArray(msg) ? msg.join('، ') : (msg ?? fallback);
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
async function loadOrgs() {
|
|
try {
|
|
orgs.value = await auth.listOrganizations();
|
|
} catch {
|
|
orgs.value = [];
|
|
}
|
|
}
|
|
|
|
function closeMenu() {
|
|
if (!open.value) return;
|
|
open.value = false;
|
|
emit('close');
|
|
}
|
|
|
|
async function toggle() {
|
|
if (!menuEnabled.value) return;
|
|
error.value = '';
|
|
if (open.value) {
|
|
closeMenu();
|
|
return;
|
|
}
|
|
ignoreDocClickUntil = Date.now() + 400;
|
|
open.value = true;
|
|
emit('open');
|
|
await nextTick();
|
|
placeMenu();
|
|
await loadOrgs();
|
|
await nextTick();
|
|
placeMenu();
|
|
}
|
|
|
|
function placeMenu() {
|
|
const btn = rootEl.value?.querySelector('button');
|
|
if (!(btn instanceof HTMLElement)) return;
|
|
const box = btn.getBoundingClientRect();
|
|
const minWidth = Math.round(Math.max(box.width, isWorkspace.value ? 240 : 200));
|
|
const sidebar = document.getElementById('app-sidebar');
|
|
const rail = sidebar?.getBoundingClientRect();
|
|
// لبهٔ راست منو = لبهٔ چپ سایدبار − ۴px
|
|
const edge = rail?.left ?? box.left;
|
|
const beside = {
|
|
right: 'auto',
|
|
left: `${Math.round(edge - 4)}px`,
|
|
transform: 'translateX(-100%)',
|
|
minWidth: `${minWidth}px`,
|
|
};
|
|
if (isWorkspace.value) {
|
|
menuStyle.value = {
|
|
...beside,
|
|
top: `${Math.round(box.top)}px`,
|
|
bottom: 'auto',
|
|
};
|
|
return;
|
|
}
|
|
menuStyle.value = {
|
|
...beside,
|
|
top: 'auto',
|
|
bottom: `${Math.round(window.innerHeight - box.bottom)}px`,
|
|
};
|
|
}
|
|
|
|
async function choose(org: OrganizationMembershipDto) {
|
|
if (org.id === auth.user?.organizationId) {
|
|
closeMenu();
|
|
return;
|
|
}
|
|
loading.value = true;
|
|
error.value = '';
|
|
try {
|
|
await auth.switchOrganization(org.id);
|
|
window.location.assign('/');
|
|
} catch (e) {
|
|
error.value = apiError(e, 'تغییر محیط ناموفق بود');
|
|
loading.value = false;
|
|
}
|
|
}
|
|
|
|
function onDocClick(event: MouseEvent) {
|
|
if (!open.value || Date.now() < ignoreDocClickUntil) return;
|
|
const target = event.target;
|
|
if (!(target instanceof Node)) return;
|
|
if (rootEl.value?.contains(target) || menuEl.value?.contains(target)) return;
|
|
closeMenu();
|
|
}
|
|
|
|
defineExpose({ close: closeMenu });
|
|
|
|
onMounted(() => {
|
|
void loadOrgs();
|
|
document.addEventListener('click', onDocClick);
|
|
});
|
|
|
|
onUnmounted(() => {
|
|
document.removeEventListener('click', onDocClick);
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<div
|
|
v-if="canSwitch"
|
|
ref="rootEl"
|
|
class="org-switch"
|
|
:class="{ 'org-ws': isWorkspace, compact }"
|
|
>
|
|
<button
|
|
class="org-btn"
|
|
:class="{
|
|
compact,
|
|
'org-ws': isWorkspace,
|
|
open,
|
|
}"
|
|
type="button"
|
|
:title="currentName"
|
|
:aria-expanded="open"
|
|
aria-haspopup="listbox"
|
|
@click.stop="toggle"
|
|
>
|
|
<template v-if="isWorkspace">
|
|
<span class="org-mark" aria-hidden="true">{{ currentInitial }}</span>
|
|
<span class="org-copy">
|
|
<strong>{{ currentName }}</strong>
|
|
<small>{{ currentSubtitle }}</small>
|
|
</span>
|
|
<i class="org-chev" :class="{ open }" aria-hidden="true" />
|
|
</template>
|
|
<template v-else>
|
|
<UiIcon name="building" :size="18" />
|
|
<span>{{ currentName }}</span>
|
|
</template>
|
|
</button>
|
|
<Teleport to="body">
|
|
<div
|
|
v-if="open && menuEnabled"
|
|
ref="menuEl"
|
|
class="menu"
|
|
:class="{ 'org-ws': isWorkspace }"
|
|
:style="menuStyle"
|
|
role="listbox"
|
|
aria-label="محیطهای کاری"
|
|
>
|
|
<p class="hint">{{ canSwitch ? 'تعویض محیط کاری' : 'محیط کاری' }}</p>
|
|
<button
|
|
v-for="org in orgs"
|
|
:key="org.id"
|
|
type="button"
|
|
class="org-item"
|
|
:class="{ on: org.id === auth.user?.organizationId }"
|
|
:disabled="loading"
|
|
role="option"
|
|
:aria-selected="org.id === auth.user?.organizationId"
|
|
@click="choose(org)"
|
|
>
|
|
<span class="org-item-mark" aria-hidden="true">{{ org.name.trim().charAt(0) || 'م' }}</span>
|
|
<span class="org-item-copy">
|
|
<strong>{{ org.name }}</strong>
|
|
<span>{{ org.orgRoleName || USER_ROLE_LABELS[org.role] }}</span>
|
|
</span>
|
|
<UiIcon
|
|
v-if="org.id === auth.user?.organizationId"
|
|
class="org-check"
|
|
name="check"
|
|
:size="16"
|
|
/>
|
|
</button>
|
|
<p v-if="error" class="err">{{ error }}</p>
|
|
</div>
|
|
</Teleport>
|
|
</div>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.org-switch {
|
|
position: relative;
|
|
min-width: 0;
|
|
width: 100%;
|
|
}
|
|
|
|
.org-btn {
|
|
display: inline-flex;
|
|
align-items: flex-start;
|
|
gap: 0.35rem;
|
|
width: 100%;
|
|
max-width: 100%;
|
|
min-width: 0;
|
|
border: 0;
|
|
background: transparent;
|
|
color: #94a3b8;
|
|
font: inherit;
|
|
font-size: 0.78rem;
|
|
font-weight: 600;
|
|
cursor: pointer;
|
|
text-align: right;
|
|
padding: 0 0.15rem;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.org-btn :deep(.ico) {
|
|
flex-shrink: 0;
|
|
margin-top: 0.05rem;
|
|
}
|
|
|
|
.org-btn > span {
|
|
min-width: 0;
|
|
white-space: normal;
|
|
overflow-wrap: anywhere;
|
|
line-height: 1.35;
|
|
}
|
|
|
|
.org-btn:hover {
|
|
color: #0f172a;
|
|
}
|
|
|
|
.org-btn.static {
|
|
cursor: default;
|
|
}
|
|
|
|
.org-btn.static:hover {
|
|
color: #94a3b8;
|
|
}
|
|
|
|
.org-btn.compact:not(.org-ws) {
|
|
display: grid;
|
|
place-items: center;
|
|
width: 100%;
|
|
min-height: var(--control-h);
|
|
padding: 0;
|
|
border-radius: var(--radius);
|
|
}
|
|
|
|
.org-btn.compact:not(.org-ws) > span {
|
|
display: none;
|
|
}
|
|
|
|
.org-btn.compact:not(.org-ws):hover {
|
|
background: #f8fafc;
|
|
}
|
|
|
|
.org-btn.compact.static:not(.org-ws):hover {
|
|
background: transparent;
|
|
}
|
|
|
|
.org-btn.org-ws {
|
|
align-items: center;
|
|
gap: 0.7rem;
|
|
padding: 0.5rem 0.65rem;
|
|
border-radius: 12px;
|
|
border: 1px solid transparent;
|
|
color: #0f172a;
|
|
text-align: start;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
.org-btn.org-ws:hover,
|
|
.org-btn.org-ws.open {
|
|
background: #f3f4f6;
|
|
border-color: #eef2f7;
|
|
color: #0f172a;
|
|
}
|
|
|
|
.org-btn.org-ws.static:hover {
|
|
background: #f3f4f6;
|
|
border-color: #eef2f7;
|
|
color: #0f172a;
|
|
}
|
|
|
|
.org-mark {
|
|
width: 36px;
|
|
height: 36px;
|
|
border-radius: 999px;
|
|
display: grid;
|
|
place-items: center;
|
|
flex-shrink: 0;
|
|
background: color-mix(in srgb, var(--accent-soft) 80%, white);
|
|
color: var(--accent-deep);
|
|
font-size: 0.92rem;
|
|
font-weight: 800;
|
|
}
|
|
|
|
.org-copy {
|
|
min-width: 0;
|
|
flex: 1;
|
|
display: grid;
|
|
gap: 0.08rem;
|
|
}
|
|
|
|
.org-copy strong {
|
|
font-size: 0.92rem;
|
|
font-weight: 800;
|
|
line-height: 1.25;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.org-copy small {
|
|
font-size: 0.72rem;
|
|
font-weight: 600;
|
|
color: #94a3b8;
|
|
line-height: 1.2;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.org-chev {
|
|
width: 0.42rem;
|
|
height: 0.42rem;
|
|
border-right: 1.6px solid #94a3b8;
|
|
border-bottom: 1.6px solid #94a3b8;
|
|
/* RTL: اشاره به بیرون (چپ) */
|
|
transform: rotate(135deg);
|
|
transition: transform 0.15s ease;
|
|
flex-shrink: 0;
|
|
margin-inline-start: 0.15rem;
|
|
}
|
|
|
|
.org-chev.open {
|
|
transform: rotate(45deg);
|
|
}
|
|
|
|
.org-btn.compact.org-ws {
|
|
display: grid;
|
|
place-items: center;
|
|
width: 100%;
|
|
min-height: var(--control-h);
|
|
padding: 0;
|
|
border-radius: 14px;
|
|
}
|
|
|
|
.org-btn.compact.org-ws .org-copy,
|
|
.org-btn.compact.org-ws .org-chev {
|
|
display: none;
|
|
}
|
|
|
|
.org-btn.compact.org-ws .org-mark {
|
|
width: 32px;
|
|
height: 32px;
|
|
font-size: 0.82rem;
|
|
}
|
|
|
|
.menu {
|
|
position: fixed;
|
|
z-index: 90;
|
|
padding: 0.45rem;
|
|
border-radius: 12px;
|
|
background: #fff;
|
|
border: 1px solid var(--line);
|
|
box-shadow: 0 10px 28px rgba(15, 23, 42, 0.12);
|
|
display: grid;
|
|
gap: 0.2rem;
|
|
}
|
|
|
|
.menu.org-ws {
|
|
padding: 0.5rem;
|
|
border-radius: 16px;
|
|
border: 1px solid #e8eef3;
|
|
box-shadow: 0 16px 40px rgba(15, 23, 42, 0.14);
|
|
gap: 4px;
|
|
}
|
|
|
|
.hint {
|
|
margin: 0;
|
|
padding: 0.35rem 0.65rem 0.45rem;
|
|
font-size: 0.7rem;
|
|
font-weight: 700;
|
|
letter-spacing: 0.02em;
|
|
color: #94a3b8;
|
|
}
|
|
|
|
.org-item,
|
|
.create {
|
|
display: grid;
|
|
gap: 0.1rem;
|
|
text-align: start;
|
|
border: 0;
|
|
background: transparent;
|
|
border-radius: 9px;
|
|
padding: 0.45rem 0.5rem;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.menu.org-ws .org-item {
|
|
grid-template-columns: auto 1fr auto;
|
|
align-items: center;
|
|
gap: 0.7rem;
|
|
border-radius: 12px;
|
|
padding: 0.55rem 0.65rem;
|
|
color: #0f172a;
|
|
}
|
|
|
|
.org-item-mark {
|
|
width: 36px;
|
|
height: 36px;
|
|
border-radius: 999px;
|
|
display: grid;
|
|
place-items: center;
|
|
background: #eef2f7;
|
|
color: #475569;
|
|
font-size: 0.85rem;
|
|
font-weight: 800;
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.org-item-copy {
|
|
min-width: 0;
|
|
display: grid;
|
|
gap: 0.12rem;
|
|
}
|
|
|
|
.org-item strong {
|
|
font-size: 0.88rem;
|
|
font-weight: 700;
|
|
color: #0f172a;
|
|
line-height: 1.25;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.org-item span,
|
|
.org-item-copy span {
|
|
font-size: 0.74rem;
|
|
font-weight: 500;
|
|
color: #94a3b8;
|
|
line-height: 1.2;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.org-check {
|
|
color: #0f172a;
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.org-item:hover,
|
|
.create:hover {
|
|
background: #f3f4f6;
|
|
}
|
|
|
|
.menu.org-ws .org-item:hover {
|
|
background: #f3f4f6;
|
|
}
|
|
|
|
.menu.org-ws .org-item:hover .org-item-mark {
|
|
background: #e2e8f0;
|
|
}
|
|
|
|
.org-item.on {
|
|
background: #f3f4f6;
|
|
}
|
|
|
|
.menu.org-ws .org-item.on {
|
|
background: #f3f4f6;
|
|
}
|
|
|
|
.menu.org-ws .org-item.on .org-item-mark {
|
|
background: color-mix(in srgb, var(--accent-soft) 85%, white);
|
|
color: var(--accent-deep);
|
|
}
|
|
|
|
.menu.org-ws .org-item.on strong {
|
|
font-weight: 800;
|
|
color: #0f172a;
|
|
}
|
|
|
|
.err {
|
|
margin: 0.2rem 0 0;
|
|
color: #b91c1c;
|
|
font-size: 0.75rem;
|
|
}
|
|
</style>
|