|
| 1 | +import React from 'react' |
| 2 | +import type { Preview, Decorator } from '@storybook/react-vite' |
| 3 | +import { createStore, createIndexes, Store, Indexes } from 'tinybase' |
| 4 | +import { Provider } from 'tinybase/ui-react' |
| 5 | +import { FOAF, VCARD } from '@inrupt/vocab-common-rdf' |
| 6 | + |
| 7 | +// Create sample store with test data for stories |
| 8 | +function createSampleStore(): { store: Store; indexes: Indexes } { |
| 9 | + const store = createStore() |
| 10 | + const indexes = createIndexes(store) |
| 11 | + |
| 12 | + // Initialize tables |
| 13 | + store.setTables({ |
| 14 | + resources: {}, |
| 15 | + personas: {}, |
| 16 | + contacts: {}, |
| 17 | + groups: {}, |
| 18 | + typeIndexes: {}, |
| 19 | + }) |
| 20 | + |
| 21 | + // Add sample personas |
| 22 | + store.setRow('personas', 'persona-1', { |
| 23 | + '@id': 'https://pod.example.com/profiles/alice#me', |
| 24 | + '@type': FOAF.Person, |
| 25 | + [FOAF.name]: 'Alice Smith', |
| 26 | + [FOAF.nick]: 'alice', |
| 27 | + [VCARD.hasEmail]: 'mailto:alice@example.com', |
| 28 | + }) |
| 29 | + |
| 30 | + store.setRow('personas', 'persona-2', { |
| 31 | + '@id': 'https://pod.example.com/profiles/bob#me', |
| 32 | + '@type': FOAF.Person, |
| 33 | + [FOAF.name]: 'Bob Developer', |
| 34 | + [VCARD.hasEmail]: 'mailto:bob@work.com', |
| 35 | + }) |
| 36 | + |
| 37 | + // Add sample contacts |
| 38 | + store.setRow('contacts', 'contact-1', { |
| 39 | + '@id': 'https://pod.example.com/contacts/john#', |
| 40 | + '@type': VCARD.Individual, |
| 41 | + [VCARD.fn]: 'John Doe', |
| 42 | + [VCARD.nickname]: 'johnny', |
| 43 | + [VCARD.hasEmail]: 'mailto:john@example.com', |
| 44 | + [VCARD.hasOrganizationName]: 'Acme Corp', |
| 45 | + }) |
| 46 | + |
| 47 | + store.setRow('contacts', 'contact-2', { |
| 48 | + '@id': 'https://pod.example.com/contacts/jane#', |
| 49 | + '@type': VCARD.Individual, |
| 50 | + [VCARD.fn]: 'Jane Smith', |
| 51 | + [VCARD.hasEmail]: 'mailto:jane@example.com', |
| 52 | + }) |
| 53 | + |
| 54 | + store.setRow('contacts', 'contact-3', { |
| 55 | + '@id': 'https://pod.example.com/contacts/ai-bot#', |
| 56 | + '@type': 'https://schema.org/SoftwareApplication', |
| 57 | + [VCARD.fn]: 'AI Assistant', |
| 58 | + [VCARD.note]: 'A helpful AI chatbot', |
| 59 | + }) |
| 60 | + |
| 61 | + // Add sample groups |
| 62 | + store.setRow('groups', 'group-1', { |
| 63 | + '@id': 'https://pod.example.com/groups/acme#org', |
| 64 | + '@type': 'http://www.w3.org/ns/org#Organization', |
| 65 | + [VCARD.fn]: 'Acme Corporation', |
| 66 | + 'http://purl.org/dc/terms/description': 'A sample organization', |
| 67 | + }) |
| 68 | + |
| 69 | + store.setRow('groups', 'group-2', { |
| 70 | + '@id': 'https://pod.example.com/groups/team-alpha#team', |
| 71 | + '@type': VCARD.Group, |
| 72 | + [VCARD.fn]: 'Team Alpha', |
| 73 | + 'http://purl.org/dc/terms/description': 'The best team', |
| 74 | + }) |
| 75 | + |
| 76 | + // Set default persona |
| 77 | + store.setValue('defaultPersonaId', 'persona-1') |
| 78 | + |
| 79 | + return { store, indexes } |
| 80 | +} |
| 81 | + |
| 82 | +// Global decorator that wraps all stories in TinyBase Provider |
| 83 | +const withTinyBase: Decorator = (Story, context) => { |
| 84 | + // Allow stories to provide their own store via parameters |
| 85 | + const customStore = context.parameters.store |
| 86 | + const { store, indexes } = customStore || createSampleStore() |
| 87 | + |
| 88 | + return ( |
| 89 | + <Provider store={store} indexes={indexes}> |
| 90 | + <Story /> |
| 91 | + </Provider> |
| 92 | + ) |
| 93 | +} |
| 94 | + |
| 95 | +const preview: Preview = { |
| 96 | + parameters: { |
| 97 | + controls: { |
| 98 | + matchers: { |
| 99 | + color: /(background|color)$/i, |
| 100 | + date: /Date$/i, |
| 101 | + }, |
| 102 | + }, |
| 103 | + // Default background |
| 104 | + backgrounds: { |
| 105 | + default: 'light', |
| 106 | + values: [ |
| 107 | + { name: 'light', value: '#f5f5f5' }, |
| 108 | + { name: 'dark', value: '#1a1a1a' }, |
| 109 | + ], |
| 110 | + }, |
| 111 | + }, |
| 112 | + decorators: [withTinyBase], |
| 113 | +} |
| 114 | + |
| 115 | +export default preview |
0 commit comments