Files
collabry-components/src/components/Comment/Comment.stories.tsx
T
2026-11-14 08:49:19 +01:00

114 lines
3.3 KiB
TypeScript

import type { Meta, StoryObj } from '@storybook/react';
import { SendHorizontal } from 'lucide-react';
import { useState } from 'react';
import { me, people } from '../../stories/fixtures';
import { PersonAvatar, type Person } from '../Avatar/Avatar';
import { Button } from '../Button/Button';
import {
Comment,
CommentAuthor,
CommentBody,
CommentBubble,
CommentComposer,
CommentComposerHint,
CommentComposerRow,
CommentHeader,
CommentInput,
CommentList,
CommentTime,
} from './Comment';
const meta: Meta<typeof Comment> = {
title: 'Tasks/Comment',
component: Comment,
};
export default meta;
type Story = StoryObj<typeof Comment>;
interface CommentData {
id: string;
author: Person;
when: string;
text: string;
mine?: boolean;
}
const initial: CommentData[] = [
{
id: 'c1',
author: people.PM,
when: 'vor 1 Std.',
text: 'Kannst du die n:m-Beziehung zwischen Gruppe und Mitglied noch auflösen?',
},
{
id: 'c2',
author: me,
when: 'vor 40 Min.',
text: 'Ja, ich lege eine Zwischentabelle „Mitgliedschaft“ an.',
mine: true,
},
];
export const Thread: Story = {
render: function Render() {
const [comments, setComments] = useState(initial);
const [draft, setDraft] = useState('');
const empty = !draft.trim();
const send = () => {
if (empty) return;
setComments((cs) => [
...cs,
{ id: `c${cs.length + 1}`, author: me, when: 'gerade eben', text: draft, mine: true },
]);
setDraft('');
};
return (
<div className="flex max-w-[460px] flex-col gap-4">
<CommentList aria-label="Kommentarverlauf">
{comments.map((c) => (
<Comment key={c.id}>
<PersonAvatar person={c.author} size={30} aria-hidden="true" />
<CommentBody>
<CommentHeader>
<CommentAuthor>{c.author.name}</CommentAuthor>
<CommentTime>{c.when}</CommentTime>
{c.mine && (
<Button
variant="ghost"
size="xs"
className="ml-auto h-6 px-1.5 text-xs text-fg-subtle"
aria-label="Kommentar löschen"
onClick={() => setComments((cs) => cs.filter((x) => x.id !== c.id))}
>
Löschen
</Button>
)}
</CommentHeader>
<CommentBubble>{c.text}</CommentBubble>
</CommentBody>
</Comment>
))}
</CommentList>
<CommentComposer>
<CommentComposerRow>
<PersonAvatar person={me} size={30} aria-hidden="true" />
<CommentInput value={draft} onChange={(e) => setDraft(e.target.value)} onSubmit={send} />
<Button
variant="primary"
iconOnly
aria-label="Kommentar senden"
aria-disabled={empty}
className={empty ? 'h-[38px] w-[38px] opacity-50' : 'h-[38px] w-[38px]'}
onClick={send}
>
<SendHorizontal size={16} strokeWidth={2} />
</Button>
</CommentComposerRow>
<CommentComposerHint />
</CommentComposer>
</div>
);
},
};