MicProtocol / web /src /components /RateLimitPill.vue
Benard John
feat: implement full-stack architecture with database models, authentication services, and web dashboard components
37b5223
Raw History Blame Contribute Delete
6.71 kB
<template>
<n-tooltip
placement="bottom"
:show-arrow="true"
class="rate-pill-tooltip"
>
<template #trigger>
<button
class="rate-pill"
:data-state="authStore.rateState"
@click="onClick"
:aria-label="ariaLabel"
>
<span class="icon">
<n-icon size="14">
<flash-outline v-if="authStore.rateState === 'ok'" />
<warning-outline v-else-if="authStore.rateState === 'warning'" />
<alert-outline v-else-if="authStore.rateState === 'critical'" />
<lock-closed-outline v-else />
</n-icon>
</span>
<span class="label">
<strong>{{ authStore.remainingDisplay }}</strong>
<span class="of">/</span>
<span class="limit">{{ authStore.limitDisplay }}</span>
</span>
<span v-if="showProgress" class="progress-track" :data-state="authStore.rateState">
<span
class="progress-fill"
:style="{ width: `${authStore.percentUsed}%` }"
/>
</span>
</button>
</template>
<div class="tooltip-body">
<div class="tooltip-head">
<span class="tier-badge" :data-tier="authStore.tier">{{ tierLabel }}</span>
<span class="reset">Resets in {{ resetLabel }}</span>
</div>
<div class="tooltip-stat">
<span class="stat-num">{{ authStore.usedDisplay }}</span>
<span class="stat-label">used today</span>
</div>
<p v-if="authStore.rateState === 'exhausted'" class="tooltip-cta">
Sign up free for 25× more queries.
</p>
<p v-else-if="authStore.tier === 'public'" class="tooltip-cta">
{{ authStore.remainingDisplay }} of {{ authStore.limitDisplay }} left today.
</p>
<p v-else class="tooltip-cta">
Registered tier — generate an API key for 10,000/day.
</p>
</div>
</n-tooltip>
</template>
<script setup lang="ts">
import { computed } from 'vue'
import { useAuthStore } from '@/stores/auth'
import {
FlashOutline,
WarningOutline,
AlertOutline,
LockClosedOutline,
} from '@vicons/ionicons5'
const authStore = useAuthStore()
const showProgress = computed(() => authStore.rate.limit !== -1)
const tierLabel = computed(() => {
switch (authStore.tier) {
case 'public': return 'Public'
case 'registered': return 'Registered'
case 'api': return 'API key'
case 'enterprise': return 'Enterprise'
}
return 'Public'
})
const resetLabel = computed(() => {
if (!authStore.rate.reset_at) return '—'
const ms = new Date(authStore.rate.reset_at).getTime() - Date.now()
if (ms <= 0) return 'now'
const hours = Math.floor(ms / (1000 * 60 * 60))
const mins = Math.floor((ms % (1000 * 60 * 60)) / (1000 * 60))
if (hours > 0) return `${hours}h ${mins}m`
return `${mins}m`
})
const ariaLabel = computed(() =>
`${authStore.usedDisplay} of ${authStore.limitDisplay} daily queries used`
)
function onClick() {
if (authStore.tier === 'public' && authStore.rateState === 'exhausted') {
authStore.openAuthModal()
} else if (authStore.tier === 'public') {
authStore.openAuthModal()
}
}
</script>
<style scoped lang="scss">
.rate-pill {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 5px 12px 5px 8px;
background: var(--bg-elevated);
border: 1px solid var(--border);
border-radius: 999px;
font-size: 12px;
font-weight: 700;
color: var(--text-secondary);
cursor: pointer;
position: relative;
overflow: hidden;
transition: all 0.25s var(--ease-out);
font-family: var(--font-mono);
.icon {
display: inline-flex;
align-items: center;
justify-content: center;
width: 20px;
height: 20px;
border-radius: 50%;
background: var(--bg-card);
color: var(--text-secondary);
}
.label {
display: inline-flex;
align-items: baseline;
gap: 1px;
strong {
color: var(--text-primary);
font-size: 13px;
}
.of {
color: var(--text-muted);
font-weight: 400;
margin: 0 1px;
}
.limit {
color: var(--text-muted);
font-weight: 500;
}
}
.progress-track {
position: absolute;
bottom: 0;
left: 0;
height: 2px;
width: 100%;
background: transparent;
.progress-fill {
display: block;
height: 100%;
background: var(--text-muted);
transition: width 0.4s var(--ease-out), background 0.25s var(--ease-out);
}
}
&:hover {
transform: translateY(-1px);
box-shadow: var(--shadow-sm);
}
// --- States ---
&[data-state='ok'] {
.icon { color: var(--primary); background: var(--primary-soft); }
.progress-fill { background: var(--primary); }
}
&[data-state='warning'] {
border-color: rgba(181, 116, 14, 0.35);
.icon { color: var(--warning); background: rgba(181, 116, 14, 0.12); }
.progress-fill { background: var(--warning); }
}
&[data-state='critical'] {
border-color: rgba(196, 132, 26, 0.5);
background: var(--accent-soft);
.icon { color: var(--accent); background: var(--bg-card); }
.progress-fill { background: var(--accent); }
}
&[data-state='exhausted'] {
border-color: rgba(181, 28, 28, 0.5);
background: rgba(181, 28, 28, 0.10);
color: var(--error);
.icon { color: white; background: var(--error); }
strong { color: var(--error); }
.progress-fill { background: var(--error); }
}
}
// --- Tooltip body ---
.tooltip-body {
min-width: 220px;
padding: 4px 0;
font-family: var(--font-body);
.tooltip-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}
.tier-badge {
display: inline-block;
padding: 2px 8px;
background: var(--primary-soft);
color: var(--primary);
border-radius: 999px;
font-size: 10px;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.08em;
&[data-tier='api'] { background: var(--accent-soft); color: var(--accent); }
&[data-tier='enterprise'] { background: rgba(196, 132, 26, 0.18); color: var(--accent-hover); }
}
.reset {
font-size: 11px;
color: var(--text-muted);
font-family: var(--font-mono);
}
.tooltip-stat {
display: flex;
align-items: baseline;
gap: 6px;
margin-bottom: 8px;
.stat-num {
font-family: var(--font-display);
font-size: 28px;
font-weight: 700;
color: var(--text-primary);
}
.stat-label {
font-size: 12px;
color: var(--text-muted);
}
}
.tooltip-cta {
font-size: 12px;
color: var(--text-secondary);
line-height: 1.5;
margin: 0;
}
}
</style>