feat(community): community feed — browse, publish, fork, mDNS discovery #78

Open
pyr0ball wants to merge 7 commits from feature/community into feature/meal-planner
5 changed files with 770 additions and 3 deletions
Showing only changes of commit 96d7fe0263 - Show all commits

View file

@ -0,0 +1,231 @@
<!-- frontend/src/components/CommunityFeedPanel.vue -->
<template>
<div class="community-feed-panel">
<!-- Filter bar -->
<div class="filter-bar" role="toolbar" aria-label="Filter community posts">
<button
v-for="f in FILTERS"
:key="f.value ?? 'all'"
class="filter-btn"
:class="{ active: activeFilter === f.value }"
:aria-pressed="activeFilter === f.value"
@click="setFilter(f.value)"
>{{ f.label }}</button>
</div>
<!-- Results count (aria-live so screen readers announce changes) -->
<p
class="results-summary"
aria-live="polite"
aria-atomic="true"
>
<template v-if="!loading">
{{ posts.length }} post{{ posts.length !== 1 ? 's' : '' }}
<template v-if="activeFilter"> · {{ activeFilterLabel }}</template>
</template>
</p>
<!-- Publish button (visible when plan is active) -->
<div class="publish-row" v-if="activePlanId">
<button class="publish-btn" @click="showPublish = true">
Share this week's plan
</button>
</div>
<!-- Error state -->
<div v-if="error" class="feed-error" role="alert">
<p>{{ error }}</p>
<button class="retry-btn" @click="communityStore.clearError(); communityStore.loadPosts(true)">
Try again
</button>
</div>
<!-- Feed -->
<div v-else-if="posts.length" class="feed-list">
<CommunityPostCard
v-for="post in posts"
:key="post.slug"
:post="post"
:forking="forkingSlug === post.slug"
@fork="onFork"
/>
<button
v-if="hasMore && !loading"
class="load-more-btn"
@click="communityStore.loadPosts()"
>
Load more
</button>
</div>
<!-- Loading skeleton -->
<div v-else-if="loading" class="feed-loading" aria-busy="true" aria-label="Loading posts">
<div v-for="i in 3" :key="i" class="skeleton-card"></div>
</div>
<!-- Empty state -->
<div v-else-if="isEmpty" class="feed-empty">
<p>No community posts yet.</p>
<p class="feed-empty-hint">Be the first to share a meal plan!</p>
</div>
<!-- Fork success toast -->
<div
v-if="forkSuccess"
class="fork-toast"
role="status"
aria-live="polite"
>
Plan forked into your week starting {{ forkSuccess.week_start }}
</div>
<!-- Publish modal -->
<PublishPlanModal
v-if="showPublish"
:plan-id="activePlanId"
@close="showPublish = false"
@published="onPublished"
/>
</div>
</template>
<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { storeToRefs } from 'pinia'
import { useCommunityStore } from '../stores/community'
import CommunityPostCard from './CommunityPostCard.vue'
import PublishPlanModal from './PublishPlanModal.vue'
const props = defineProps<{
activePlanId?: number | null
}>()
const communityStore = useCommunityStore()
const { posts, loading, error, hasMore, isEmpty } = storeToRefs(communityStore)
const activeFilter = ref<'plan' | 'recipe_success' | 'recipe_blooper' | null>(null)
const showPublish = ref(false)
const forkingSlug = ref<string | null>(null)
const forkSuccess = ref<{ plan_id: number; week_start: string } | null>(null)
const FILTERS = [
{ label: 'All', value: null },
{ label: 'Plans', value: 'plan' as const },
{ label: 'Wins', value: 'recipe_success' as const },
{ label: 'Bloopers', value: 'recipe_blooper' as const },
] as const
const activeFilterLabel = computed(
() => FILTERS.find(f => f.value === activeFilter.value)?.label ?? ''
)
onMounted(() => communityStore.loadPosts(true))
function setFilter(value: typeof activeFilter.value) {
activeFilter.value = value
communityStore.setFilter(value)
}
async function onFork(slug: string) {
forkingSlug.value = slug
forkSuccess.value = null
const result = await communityStore.forkPost(slug)
forkingSlug.value = null
if (result) {
forkSuccess.value = result
setTimeout(() => { forkSuccess.value = null }, 4000)
}
}
function onPublished() {
showPublish.value = false
communityStore.loadPosts(true)
}
</script>
<style scoped>
.community-feed-panel { display: flex; flex-direction: column; gap: 0.75rem; }
.filter-bar { display: flex; gap: 6px; flex-wrap: wrap; }
.filter-btn {
font-size: 0.78rem;
padding: 0.25rem 0.75rem;
border-radius: 16px;
border: 1px solid var(--color-border);
background: var(--color-surface);
color: var(--color-text-secondary);
cursor: pointer;
transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.filter-btn.active, .filter-btn:hover {
background: var(--color-accent-subtle);
color: var(--color-accent);
border-color: var(--color-accent);
}
.filter-btn:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
.results-summary { font-size: 0.75rem; color: var(--color-text-secondary); min-height: 1.1em; margin: 0; }
.publish-row { display: flex; }
.publish-btn {
font-size: 0.82rem;
padding: 0.4rem 1.1rem;
border-radius: 20px;
background: var(--color-accent);
color: white;
border: none;
cursor: pointer;
transition: opacity 0.15s;
}
.publish-btn:hover { opacity: 0.88; }
.publish-btn:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
.feed-list { display: flex; flex-direction: column; gap: 0.75rem; }
.load-more-btn {
align-self: center;
font-size: 0.8rem;
padding: 0.4rem 1.2rem;
border-radius: 16px;
border: 1px solid var(--color-border);
background: var(--color-surface);
color: var(--color-text-secondary);
cursor: pointer;
}
.load-more-btn:hover { border-color: var(--color-accent); color: var(--color-accent); }
.feed-loading { display: flex; flex-direction: column; gap: 0.75rem; }
.skeleton-card {
height: 110px;
border-radius: 10px;
background: linear-gradient(90deg, var(--color-surface) 25%, var(--color-border) 50%, var(--color-surface) 75%);
background-size: 200% 100%;
animation: shimmer 1.4s infinite;
}
@media (prefers-reduced-motion: reduce) { .skeleton-card { animation: none; background: var(--color-surface); } }
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }
.feed-empty { text-align: center; padding: 2rem 0; color: var(--color-text-secondary); }
.feed-empty p { margin: 0.25rem 0; }
.feed-empty-hint { font-size: 0.82rem; opacity: 0.7; }
.feed-error { padding: 0.75rem 1rem; border-radius: 8px; background: color-mix(in srgb, red 8%, transparent); border: 1px solid color-mix(in srgb, red 20%, transparent); }
.feed-error p { margin: 0 0 0.5rem; font-size: 0.85rem; color: var(--color-text); }
.retry-btn { font-size: 0.78rem; padding: 0.3rem 0.8rem; border-radius: 14px; border: 1px solid currentColor; background: none; cursor: pointer; color: var(--color-accent); }
.fork-toast {
position: fixed;
bottom: 1.5rem;
left: 50%;
transform: translateX(-50%);
background: var(--color-accent);
color: white;
padding: 0.6rem 1.2rem;
border-radius: 20px;
font-size: 0.85rem;
box-shadow: 0 4px 16px rgba(0,0,0,0.18);
z-index: 100;
pointer-events: none;
}
</style>

View file

@ -0,0 +1,171 @@
<!-- frontend/src/components/CommunityPostCard.vue -->
<template>
<article class="post-card" :class="`post-type-${post.post_type}`">
<header class="post-header">
<span class="post-type-badge" :aria-label="`Post type: ${typeLabel}`">{{ typeLabel }}</span>
<time class="post-date" :datetime="post.published">{{ formattedDate }}</time>
</header>
<h3 class="post-title">{{ post.title }}</h3>
<p v-if="post.description" class="post-description">{{ post.description }}</p>
<div v-if="post.dietary_tags.length || post.allergen_flags.length" class="post-tags">
<span
v-for="tag in post.dietary_tags"
:key="tag"
class="tag tag-dietary"
:aria-label="`Dietary: ${tag}`"
>{{ tag }}</span>
<span
v-for="flag in post.allergen_flags"
:key="flag"
class="tag tag-allergen"
:aria-label="`Contains: ${flag}`"
>{{ flag }}</span>
</div>
<div v-if="post.post_type === 'plan' && post.slots.length" class="post-slots">
<span class="slots-summary">
{{ post.slots.length }} meal{{ post.slots.length !== 1 ? 's' : '' }} planned
</span>
</div>
<div v-if="post.outcome_notes" class="outcome-notes">
<p class="outcome-label">Notes</p>
<p class="outcome-text">{{ post.outcome_notes }}</p>
</div>
<footer class="post-footer">
<span class="post-author">by {{ post.pseudonym }}</span>
<div class="post-actions">
<button
v-if="post.post_type === 'plan'"
class="action-btn fork-btn"
:disabled="forking"
:aria-busy="forking"
@click="$emit('fork', post.slug)"
>
{{ forking ? 'Forking…' : 'Fork plan' }}
</button>
</div>
</footer>
</article>
</template>
<script setup lang="ts">
import { computed } from 'vue'
import type { CommunityPost } from '../stores/community'
const props = defineProps<{
post: CommunityPost
forking?: boolean
}>()
defineEmits<{
fork: [slug: string]
}>()
const typeLabel = computed(() => ({
plan: 'Meal Plan',
recipe_success: 'Recipe Win',
recipe_blooper: 'Recipe Blooper',
}[props.post.post_type] ?? props.post.post_type))
const formattedDate = computed(() => {
try {
return new Date(props.post.published).toLocaleDateString(undefined, {
year: 'numeric', month: 'short', day: 'numeric',
})
} catch {
return props.post.published
}
})
</script>
<style scoped>
.post-card {
background: var(--color-surface);
border: 1px solid var(--color-border);
border-radius: 10px;
padding: 0.9rem 1rem;
display: flex;
flex-direction: column;
gap: 0.5rem;
transition: box-shadow 0.15s;
}
.post-card:hover { box-shadow: 0 2px 8px color-mix(in srgb, var(--color-accent) 12%, transparent); }
.post-header {
display: flex;
justify-content: space-between;
align-items: center;
gap: 0.5rem;
}
.post-type-badge {
font-size: 0.7rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.04em;
padding: 0.15rem 0.5rem;
border-radius: 20px;
background: var(--color-accent-subtle);
color: var(--color-accent);
}
.post-type-recipe_blooper .post-type-badge { background: color-mix(in srgb, orange 15%, transparent); color: #b36000; }
.post-type-recipe_success .post-type-badge { background: color-mix(in srgb, green 12%, transparent); color: #2a7a2a; }
.post-date { font-size: 0.75rem; color: var(--color-text-secondary); }
.post-title {
margin: 0;
font-size: 0.95rem;
font-weight: 600;
color: var(--color-text);
line-height: 1.3;
}
.post-description { margin: 0; font-size: 0.82rem; color: var(--color-text-secondary); line-height: 1.5; }
.post-tags { display: flex; flex-wrap: wrap; gap: 4px; }
.tag {
font-size: 0.68rem;
padding: 0.1rem 0.4rem;
border-radius: 10px;
border: 1px solid var(--color-border);
color: var(--color-text-secondary);
}
.tag-dietary { border-color: color-mix(in srgb, green 30%, transparent); color: #2a7a2a; }
.tag-allergen { border-color: color-mix(in srgb, orange 30%, transparent); color: #b36000; }
.slots-summary { font-size: 0.78rem; color: var(--color-text-secondary); }
.outcome-notes { background: var(--color-bg); border-radius: 6px; padding: 0.5rem 0.7rem; }
.outcome-label { margin: 0 0 0.2rem; font-size: 0.7rem; font-weight: 600; text-transform: uppercase; color: var(--color-text-secondary); }
.outcome-text { margin: 0; font-size: 0.82rem; color: var(--color-text); line-height: 1.5; }
.post-footer {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 0.25rem;
flex-wrap: wrap;
gap: 0.4rem;
}
.post-author { font-size: 0.75rem; color: var(--color-text-secondary); font-style: italic; }
.action-btn {
font-size: 0.78rem;
padding: 0.3rem 0.8rem;
border-radius: 16px;
border: 1px solid var(--color-accent);
background: var(--color-accent-subtle);
color: var(--color-accent);
cursor: pointer;
transition: background 0.15s;
}
.action-btn:hover:not(:disabled) { background: var(--color-accent); color: white; }
.action-btn:disabled { opacity: 0.5; cursor: default; }
.action-btn:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
</style>

View file

@ -26,7 +26,7 @@
@add-meal-type="onAddMealType"
/>
<!-- Panel tabs: Shopping List | Prep Schedule -->
<!-- Panel tabs: Shopping List | Prep Schedule | Community -->
<div class="panel-tabs" role="tablist" aria-label="Plan outputs">
<button
v-for="tab in TABS"
@ -60,6 +60,16 @@
>
<PrepSessionView @load="store.loadPrepSession()" />
</div>
<div
v-show="activeTab === 'community'"
id="tabpanel-community"
role="tabpanel"
aria-labelledby="tab-community"
class="tab-panel"
>
<CommunityFeedPanel :active-plan-id="activePlan?.id ?? null" />
</div>
</template>
<div v-else-if="!loading" class="empty-plan-state">
@ -76,10 +86,12 @@ import { useMealPlanStore } from '../stores/mealPlan'
import MealPlanGrid from './MealPlanGrid.vue'
import ShoppingListPanel from './ShoppingListPanel.vue'
import PrepSessionView from './PrepSessionView.vue'
import CommunityFeedPanel from './CommunityFeedPanel.vue'
const TABS = [
{ id: 'shopping', label: 'Shopping List' },
{ id: 'prep', label: 'Prep Schedule' },
{ id: 'shopping', label: 'Shopping List' },
{ id: 'prep', label: 'Prep Schedule' },
{ id: 'community', label: 'Community' },
] as const
type TabId = typeof TABS[number]['id']

View file

@ -0,0 +1,230 @@
<!-- frontend/src/components/PublishPlanModal.vue -->
<!-- Publish a meal plan to the community feed.
Pseudonym setup is inline (pre-populated, feels like invitation).
Focus-trapped per a11y audit. No countdown on delete-undo. -->
<template>
<div
class="modal-backdrop"
role="dialog"
aria-modal="true"
aria-labelledby="publish-modal-title"
@keydown.esc="$emit('close')"
ref="backdropEl"
@click.self="$emit('close')"
>
<div class="modal-card" ref="cardEl">
<header class="modal-header">
<h2 id="publish-modal-title" class="modal-title">Share this week's plan</h2>
<button class="close-btn" aria-label="Close" @click="$emit('close')">×</button>
</header>
<div class="modal-body">
<div class="field">
<label for="plan-title" class="field-label">Title</label>
<input
id="plan-title"
v-model="title"
class="field-input"
type="text"
placeholder="e.g. Pasta Week"
maxlength="120"
required
ref="firstFocusEl"
/>
</div>
<div class="field">
<label for="plan-description" class="field-label">Description <span class="optional">(optional)</span></label>
<textarea
id="plan-description"
v-model="description"
class="field-input field-textarea"
placeholder="What makes this week special?"
maxlength="400"
rows="3"
/>
</div>
<div class="field">
<label for="pseudonym" class="field-label">
Your community name
<span class="field-hint">How you appear on posts not your real name or email</span>
</label>
<input
id="pseudonym"
v-model="pseudonymName"
class="field-input"
type="text"
placeholder="e.g. PastaWitch"
maxlength="40"
/>
<p v-if="pseudonymError" class="field-error" role="alert">{{ pseudonymError }}</p>
</div>
<p v-if="error" class="submit-error" role="alert">{{ error }}</p>
</div>
<footer class="modal-footer">
<button class="cancel-btn" @click="$emit('close')">Cancel</button>
<button
class="publish-btn"
:disabled="submitting || !title.trim()"
:aria-busy="submitting"
@click="onSubmit"
>
{{ submitting ? 'Publishing…' : 'Publish' }}
</button>
</footer>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { useCommunityStore } from '../stores/community'
const props = defineProps<{
planId?: number | null
}>()
const emit = defineEmits<{
close: []
published: []
}>()
const communityStore = useCommunityStore()
const title = ref('')
const description = ref('')
const pseudonymName = ref('')
const pseudonymError = ref('')
const submitting = ref(false)
const error = ref('')
const backdropEl = ref<HTMLElement | null>(null)
const cardEl = ref<HTMLElement | null>(null)
const firstFocusEl = ref<HTMLInputElement | null>(null)
onMounted(() => {
firstFocusEl.value?.focus()
document.addEventListener('keydown', trapFocus)
})
onBeforeUnmount(() => {
document.removeEventListener('keydown', trapFocus)
})
function trapFocus(e: KeyboardEvent) {
if (e.key !== 'Tab' || !cardEl.value) return
const focusables = cardEl.value.querySelectorAll<HTMLElement>(
'button, input, textarea, [tabindex]:not([tabindex="-1"])'
)
const first = focusables[0]
const last = focusables[focusables.length - 1]
if (e.shiftKey) {
if (document.activeElement === first) { e.preventDefault(); last.focus() }
} else {
if (document.activeElement === last) { e.preventDefault(); first.focus() }
}
}
async function onSubmit() {
pseudonymError.value = ''
error.value = ''
if (pseudonymName.value && pseudonymName.value.includes('@')) {
pseudonymError.value = 'Community name must not contain "@" — use a display name, not an email.'
return
}
submitting.value = true
const result = await communityStore.publishPost({
post_type: 'plan',
title: title.value.trim(),
description: description.value.trim() || undefined,
pseudonym_name: pseudonymName.value.trim() || undefined,
})
submitting.value = false
if (result) {
emit('published')
} else {
error.value = communityStore.error ?? 'Publish failed. Please try again.'
}
}
</script>
<style scoped>
.modal-backdrop {
position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 200;
display: flex; align-items: center; justify-content: center; padding: 1rem;
}
.modal-card {
background: var(--color-surface);
border: 1px solid var(--color-border);
border-radius: 14px;
width: 100%;
max-width: 440px;
max-height: 90vh;
overflow-y: auto;
display: flex;
flex-direction: column;
box-shadow: 0 8px 32px rgba(0,0,0,0.22);
}
.modal-header {
display: flex; justify-content: space-between; align-items: center;
padding: 1rem 1.2rem 0.75rem;
border-bottom: 1px solid var(--color-border);
}
.modal-title { margin: 0; font-size: 1rem; font-weight: 600; }
.close-btn {
background: none; border: none; font-size: 1.4rem; cursor: pointer;
color: var(--color-text-secondary); line-height: 1; padding: 0 0.2rem;
}
.close-btn:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
.modal-body { padding: 1rem 1.2rem; display: flex; flex-direction: column; gap: 0.9rem; }
.field { display: flex; flex-direction: column; gap: 0.3rem; }
.field-label { font-size: 0.82rem; font-weight: 600; color: var(--color-text); }
.optional { font-weight: 400; color: var(--color-text-secondary); }
.field-hint { display: block; font-size: 0.72rem; color: var(--color-text-secondary); font-weight: 400; margin-top: 0.1rem; }
.field-input {
padding: 0.45rem 0.65rem;
border: 1px solid var(--color-border);
border-radius: 7px;
background: var(--color-bg);
color: var(--color-text);
font-size: 0.88rem;
width: 100%;
box-sizing: border-box;
}
.field-input:focus { outline: 2px solid var(--color-accent); border-color: transparent; }
.field-textarea { resize: vertical; font-family: inherit; min-height: 70px; }
.field-error { margin: 0; font-size: 0.78rem; color: #c0392b; }
.submit-error { margin: 0; font-size: 0.82rem; color: #c0392b; }
.modal-footer {
padding: 0.85rem 1.2rem;
border-top: 1px solid var(--color-border);
display: flex;
gap: 0.6rem;
justify-content: flex-end;
}
.cancel-btn {
padding: 0.4rem 1rem; border-radius: 8px; border: 1px solid var(--color-border);
background: none; color: var(--color-text-secondary); cursor: pointer; font-size: 0.85rem;
}
.cancel-btn:hover { border-color: var(--color-text-secondary); }
.publish-btn {
padding: 0.4rem 1.2rem; border-radius: 8px; border: none;
background: var(--color-accent); color: white; cursor: pointer; font-size: 0.85rem;
font-weight: 600;
}
.publish-btn:hover:not(:disabled) { opacity: 0.88; }
.publish-btn:disabled { opacity: 0.5; cursor: default; }
.publish-btn:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
</style>

View file

@ -0,0 +1,123 @@
// frontend/src/stores/community.ts
import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { api } from '../services/api'
export interface CommunityPost {
slug: string
pseudonym: string
post_type: 'plan' | 'recipe_success' | 'recipe_blooper'
published: string
title: string
description: string | null
photo_url: string | null
slots: Array<{ day: string; meal_type: string; recipe_id: number; recipe_name: string }>
recipe_id: number | null
recipe_name: string | null
level: number | null
outcome_notes: string | null
element_profiles: {
seasoning_score: number
richness_score: number
brightness_score: number
depth_score: number
aroma_score: number
structure_score: number
texture_profile: string
}
dietary_tags: string[]
allergen_flags: string[]
fat_pct: number | null
protein_pct: number | null
moisture_pct: number | null
}
export const useCommunityStore = defineStore('community', () => {
const posts = ref<CommunityPost[]>([])
const loading = ref(false)
const error = ref<string | null>(null)
const page = ref(1)
const hasMore = ref(true)
const pseudonym = ref<string | null>(null)
// Filters
const filterDietaryTags = ref<string[]>([])
const filterPostType = ref<'plan' | 'recipe_success' | 'recipe_blooper' | null>(null)
const isEmpty = computed(() => !loading.value && posts.value.length === 0)
async function loadPosts(reset = false) {
if (loading.value) return
if (reset) {
posts.value = []
page.value = 1
hasMore.value = true
}
if (!hasMore.value) return
loading.value = true
error.value = null
try {
const params: Record<string, string | number> = {
page: page.value,
page_size: 20,
}
if (filterPostType.value) params.post_type = filterPostType.value
if (filterDietaryTags.value.length) params.dietary_tags = filterDietaryTags.value.join(',')
const res = await api.get('/community/posts', { params })
const newPosts: CommunityPost[] = res.data.posts ?? []
posts.value = reset ? newPosts : [...posts.value, ...newPosts]
hasMore.value = newPosts.length === 20
page.value += 1
} catch (err: any) {
error.value = err?.response?.data?.detail ?? 'Could not load community posts.'
} finally {
loading.value = false
}
}
async function forkPost(slug: string): Promise<{ plan_id: number; week_start: string } | null> {
try {
const res = await api.post(`/community/posts/${slug}/fork`)
return res.data
} catch (err: any) {
error.value = err?.response?.data?.detail ?? 'Fork failed.'
return null
}
}
async function publishPost(payload: {
post_type: string
title: string
description?: string
pseudonym_name?: string
slots?: unknown[]
recipe_id?: number
recipe_name?: string
outcome_notes?: string
}): Promise<CommunityPost | null> {
try {
const res = await api.post('/community/posts', payload)
return res.data
} catch (err: any) {
error.value = err?.response?.data?.detail ?? 'Publish failed.'
return null
}
}
function setFilter(type: typeof filterPostType.value) {
filterPostType.value = type
loadPosts(true)
}
function clearError() {
error.value = null
}
return {
posts, loading, error, page, hasMore, pseudonym,
filterDietaryTags, filterPostType, isEmpty,
loadPosts, forkPost, publishPost, setFilter, clearError,
}
})