-
Notifications
You must be signed in to change notification settings - Fork 0
Add ImpersonationAlert component and improve styling consistency #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| <script setup lang="ts"> | ||
| import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; | ||
| import { cn } from '@/lib/utils'; | ||
| import type { User } from '@/types'; | ||
| import { router, usePage } from '@inertiajs/vue3'; | ||
| import { onClickOutside, onKeyStroke } from '@vueuse/core'; | ||
| import { Drama, HistoryIcon, X } from 'lucide-vue-next'; | ||
| import { computed, ref } from 'vue'; | ||
|
|
||
| interface Impersonation { | ||
| user: User; | ||
| route: string; | ||
| label: string; | ||
| recent: User[]; | ||
| } | ||
|
|
||
| const page = usePage(); | ||
|
|
||
| const impersonation = computed<Impersonation | null>( | ||
| () => (page.props?.impersonation as Impersonation) || null, | ||
| ); | ||
|
|
||
| // User initials helper | ||
| const getUserInitials = (name: string) => { | ||
| return name | ||
| .split(' ') | ||
| .map((word) => word.charAt(0).toUpperCase()) | ||
| .slice(0, 2) | ||
| .join(''); | ||
| }; | ||
|
|
||
| // Role badge helper | ||
| const getRoleBadgeClasses = (role: string) => { | ||
| const baseClasses = | ||
| 'shrink-0 rounded-xl px-1 py-0.5 text-[9px] text-white uppercase'; | ||
| const colorClasses = role === 'admin' ? 'bg-red-800' : 'bg-cyan-700'; | ||
|
|
||
| return cn(baseClasses, colorClasses); | ||
| }; | ||
|
|
||
| // State management | ||
| const isExpanded = ref(false); | ||
| const alertRef = ref<HTMLElement | null>(null); | ||
|
|
||
| // Actions | ||
| const toggleExpanded = () => { | ||
| isExpanded.value = !isExpanded.value; | ||
| }; | ||
| const collapse = () => { | ||
| isExpanded.value = false; | ||
| }; | ||
| const reimpersonate = (userId: number) => { | ||
| router.post( | ||
| route('auth.impersonate.reimpersonate', { userId }), | ||
| {}, | ||
| { | ||
| preserveScroll: true, | ||
| onSuccess: () => { | ||
| collapse(); | ||
| }, | ||
| }, | ||
| ); | ||
| }; | ||
|
|
||
| // Interaction handlers | ||
| onClickOutside(alertRef, () => { | ||
| if (isExpanded.value) collapse(); | ||
| }); | ||
|
|
||
| onKeyStroke('Escape', () => { | ||
| if (isExpanded.value) collapse(); | ||
| }); | ||
|
|
||
| // Positioning classes | ||
| const containerClasses = computed(() => cn('fixed bottom-3 right-3 z-50')); | ||
| </script> | ||
|
|
||
| <template> | ||
| <div | ||
| v-if="impersonation" | ||
| ref="alertRef" | ||
| :class="containerClasses" | ||
| data-testid="impersonation-alert" | ||
| > | ||
| <!-- Collapsed state: Avatar only with orange border --> | ||
| <button | ||
| v-if="!isExpanded" | ||
| @click="toggleExpanded" | ||
| :title="`Impersonating ${impersonation.user.name}`" | ||
| class="animate-in fade-in zoom-in-95 relative cursor-pointer rounded-xl shadow-lg ring-2 ring-orange-500 transition-all duration-300 hover:shadow-xl hover:ring-orange-600" | ||
| :aria-label="$t('Show impersonation details')" | ||
| :aria-expanded="false" | ||
| > | ||
| <Avatar class="size-10 bg-gray-100"> | ||
| <AvatarImage | ||
| :src="impersonation.user.avatar" | ||
| :alt="impersonation.user.name" | ||
| /> | ||
| <AvatarFallback class="bg-yellow-600 text-sm text-white"> | ||
| {{ getUserInitials(impersonation.user.name) }} | ||
| </AvatarFallback> | ||
| </Avatar> | ||
| <!-- Impersonation icon badge --> | ||
| <div | ||
| class="absolute -top-3 -left-3 flex size-7 items-center justify-center rounded-xl bg-orange-500 shadow-lg" | ||
| > | ||
| <Drama class="size-5 text-white" /> | ||
| </div> | ||
| </button> | ||
|
|
||
| <!-- Expanded state: Full card with user info and action button --> | ||
| <div | ||
| v-else | ||
| class="bg-foreground animate-in fade-in slide-in-from-right-5 relative flex w-80 flex-col gap-3 rounded-xl p-3 shadow-2xl duration-300" | ||
| role="region" | ||
| :aria-label="$t('Impersonation alert')" | ||
| > | ||
| <!-- Close button --> | ||
| <button | ||
| @click="collapse" | ||
| class="text-background/60 hover:bg-background/10 hover:text-background absolute top-3 right-3 rounded-xl p-1 transition-colors" | ||
| :aria-label="$t('Close')" | ||
| > | ||
| <X class="size-5" /> | ||
| </button> | ||
| <div class="flex items-center gap-3"> | ||
| <Avatar class="size-11 border-2 border-amber-500"> | ||
| <AvatarImage | ||
| :src="impersonation.user.avatar" | ||
| :alt="impersonation.user.name" | ||
| /> | ||
| <AvatarFallback class="bg-yellow-600 text-sm text-white"> | ||
| {{ getUserInitials(impersonation.user.name) }} | ||
| </AvatarFallback> | ||
| </Avatar> | ||
| <div class="min-w-0 flex-1"> | ||
| <div class="flex items-center gap-2"> | ||
| <p | ||
| class="text-background truncate text-sm font-semibold" | ||
| > | ||
| {{ impersonation.user.name }} | ||
| </p> | ||
| <span | ||
| v-if="impersonation.user.role" | ||
| :class=" | ||
| getRoleBadgeClasses(impersonation.user.role) | ||
| " | ||
| > | ||
| {{ impersonation.user.role }} | ||
| </span> | ||
| </div> | ||
| <p class="text-background/80 truncate text-xs"> | ||
| {{ impersonation.user.email }} | ||
| </p> | ||
| </div> | ||
| </div> | ||
| <a | ||
| :href="impersonation.route" | ||
| class="bg-background text-foreground hover:bg-background/90 w-full rounded-xl px-3 py-2 text-center text-sm font-medium transition-colors" | ||
| > | ||
| {{ impersonation.label }} | ||
| </a> | ||
|
|
||
| <!-- Recent History Section --> | ||
| <div | ||
| v-if="impersonation.recent && impersonation.recent.length > 0" | ||
| class="border-background/10 mt-1 border-t pt-2" | ||
| > | ||
| <p | ||
| class="text-background/50 mb-3 text-center text-sm font-medium tracking-wide" | ||
| > | ||
| <HistoryIcon class="inline-block size-4" /> | ||
| {{ $t('Recent impersonated users') }} | ||
| </p> | ||
|
|
||
| <div class="space-y-0"> | ||
| <button | ||
| v-for="user in impersonation.recent" | ||
| :key="user.id" | ||
| @click="reimpersonate(user.id)" | ||
| class="hover:bg-background/20 flex w-full items-center rounded-xl text-left transition-colors" | ||
| > | ||
| <Avatar | ||
| class="border-background/20 m-1 size-10 border-2" | ||
| > | ||
| <AvatarImage :src="user.avatar" :alt="user.name" /> | ||
| <AvatarFallback | ||
| class="bg-yellow-600/80 text-xs text-white" | ||
| > | ||
| {{ getUserInitials(user.name) }} | ||
| </AvatarFallback> | ||
| </Avatar> | ||
| <div class="min-w-0 flex-1 p-2 pl-1"> | ||
| <div class="flex items-center gap-1.5"> | ||
| <p | ||
| class="text-background truncate text-xs font-medium" | ||
| > | ||
| {{ user.name }} | ||
| </p> | ||
| <span | ||
| v-if="user.role" | ||
| :class="getRoleBadgeClasses(user.role)" | ||
| > | ||
| {{ user.role }} | ||
| </span> | ||
| </div> | ||
| <p class="text-background/70 truncate text-xs"> | ||
| {{ user.email }} | ||
| </p> | ||
| </div> | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </template> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.