parent
43486d7c39
commit
9eede674b6
@ -0,0 +1,122 @@
|
|||||||
|
import { supabase } from '../supabase.js';
|
||||||
|
|
||||||
|
export async function fetchMyDecks(userId) {
|
||||||
|
const { data, error } = await supabase
|
||||||
|
.from('decks')
|
||||||
|
.select('id, title, description, config, published, created_at, updated_at')
|
||||||
|
.eq('owner_id', userId)
|
||||||
|
.order('updated_at', { ascending: false });
|
||||||
|
if (error) throw error;
|
||||||
|
const decks = data ?? [];
|
||||||
|
const questionCounts = await Promise.all(
|
||||||
|
decks.map(async (d) => {
|
||||||
|
const { count, error: e } = await supabase
|
||||||
|
.from('questions')
|
||||||
|
.select('*', { count: 'exact', head: true })
|
||||||
|
.eq('deck_id', d.id);
|
||||||
|
if (e) return 0;
|
||||||
|
return count ?? 0;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
return decks.map((d, i) => ({ ...d, question_count: questionCounts[i] }));
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function fetchPublishedDecks() {
|
||||||
|
const { data, error } = await supabase
|
||||||
|
.from('decks')
|
||||||
|
.select('id, title, description, config, published, created_at, updated_at')
|
||||||
|
.eq('published', true)
|
||||||
|
.order('updated_at', { ascending: false });
|
||||||
|
if (error) throw error;
|
||||||
|
const decks = data ?? [];
|
||||||
|
const questionCounts = await Promise.all(
|
||||||
|
decks.map(async (d) => {
|
||||||
|
const { count, error: e } = await supabase
|
||||||
|
.from('questions')
|
||||||
|
.select('*', { count: 'exact', head: true })
|
||||||
|
.eq('deck_id', d.id);
|
||||||
|
if (e) return 0;
|
||||||
|
return count ?? 0;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
return decks.map((d, i) => ({ ...d, question_count: questionCounts[i] }));
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function fetchDeckWithQuestions(deckId) {
|
||||||
|
const { data: deck, error: deckError } = await supabase
|
||||||
|
.from('decks')
|
||||||
|
.select('*')
|
||||||
|
.eq('id', deckId)
|
||||||
|
.single();
|
||||||
|
if (deckError || !deck) throw deckError || new Error('Deck not found');
|
||||||
|
const { data: questions, error: qError } = await supabase
|
||||||
|
.from('questions')
|
||||||
|
.select('*')
|
||||||
|
.eq('deck_id', deckId)
|
||||||
|
.order('sort_order', { ascending: true });
|
||||||
|
if (qError) throw qError;
|
||||||
|
return { ...deck, questions: questions ?? [] };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createDeck(ownerId, { title, description, config, questions }) {
|
||||||
|
const { data: deck, error: deckError } = await supabase
|
||||||
|
.from('decks')
|
||||||
|
.insert({
|
||||||
|
owner_id: ownerId,
|
||||||
|
title: title.trim(),
|
||||||
|
description: (description ?? '').trim(),
|
||||||
|
config: config ?? {},
|
||||||
|
})
|
||||||
|
.select('id')
|
||||||
|
.single();
|
||||||
|
if (deckError || !deck) throw deckError || new Error('Failed to create deck');
|
||||||
|
if (questions && questions.length > 0) {
|
||||||
|
const rows = questions.map((q, i) => ({
|
||||||
|
deck_id: deck.id,
|
||||||
|
sort_order: i,
|
||||||
|
prompt: (q.prompt ?? '').trim(),
|
||||||
|
explanation: (q.explanation ?? '').trim() || null,
|
||||||
|
answers: Array.isArray(q.answers) ? q.answers : [],
|
||||||
|
correct_answer_indices: Array.isArray(q.correct_answer_indices) ? q.correct_answer_indices : [],
|
||||||
|
}));
|
||||||
|
const { error: qError } = await supabase.from('questions').insert(rows);
|
||||||
|
if (qError) throw qError;
|
||||||
|
}
|
||||||
|
return deck.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateDeck(deckId, { title, description, config, questions }) {
|
||||||
|
const { error: deckError } = await supabase
|
||||||
|
.from('decks')
|
||||||
|
.update({
|
||||||
|
title: title.trim(),
|
||||||
|
description: (description ?? '').trim(),
|
||||||
|
config: config ?? {},
|
||||||
|
})
|
||||||
|
.eq('id', deckId);
|
||||||
|
if (deckError) throw deckError;
|
||||||
|
const { error: delError } = await supabase.from('questions').delete().eq('deck_id', deckId);
|
||||||
|
if (delError) throw delError;
|
||||||
|
if (questions && questions.length > 0) {
|
||||||
|
const rows = questions.map((q, i) => ({
|
||||||
|
deck_id: deckId,
|
||||||
|
sort_order: i,
|
||||||
|
prompt: (q.prompt ?? '').trim(),
|
||||||
|
explanation: (q.explanation ?? '').trim() || null,
|
||||||
|
answers: Array.isArray(q.answers) ? q.answers : [],
|
||||||
|
correct_answer_indices: Array.isArray(q.correct_answer_indices) ? q.correct_answer_indices : [],
|
||||||
|
}));
|
||||||
|
const { error: qError } = await supabase.from('questions').insert(rows);
|
||||||
|
if (qError) throw qError;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function togglePublished(deckId, published) {
|
||||||
|
const { error } = await supabase.from('decks').update({ published }).eq('id', deckId);
|
||||||
|
if (error) throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteDeck(deckId) {
|
||||||
|
const { error } = await supabase.from('decks').delete().eq('id', deckId);
|
||||||
|
if (error) throw error;
|
||||||
|
}
|
||||||
@ -1,50 +0,0 @@
|
|||||||
export const cards = [
|
|
||||||
{
|
|
||||||
id: 1,
|
|
||||||
title: 'Alpha',
|
|
||||||
description: 'First card with a short description for the grid.',
|
|
||||||
imageUrl: 'https://picsum.photos/seed/alpha/400/240',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 2,
|
|
||||||
title: 'Beta',
|
|
||||||
description: 'Second card. Another brief description here.',
|
|
||||||
imageUrl: 'https://picsum.photos/seed/beta/400/240',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 3,
|
|
||||||
title: 'Gamma',
|
|
||||||
description: 'Third card. Keep descriptions short and clear.',
|
|
||||||
imageUrl: 'https://picsum.photos/seed/gamma/400/240',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 4,
|
|
||||||
title: 'Delta',
|
|
||||||
description: 'Fourth card in the responsive grid layout.',
|
|
||||||
imageUrl: 'https://picsum.photos/seed/delta/400/240',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 5,
|
|
||||||
title: 'Epsilon',
|
|
||||||
description: 'Fifth card. Placeholder image from Picsum.',
|
|
||||||
imageUrl: 'https://picsum.photos/seed/epsilon/400/240',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 6,
|
|
||||||
title: 'Zeta',
|
|
||||||
description: 'Sixth card with a subtle shadow and dark theme.',
|
|
||||||
imageUrl: 'https://picsum.photos/seed/zeta/400/240',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 7,
|
|
||||||
title: 'Eta',
|
|
||||||
description: 'Seventh card. Full-width grid, 4+ columns on desktop.',
|
|
||||||
imageUrl: 'https://picsum.photos/seed/eta/400/240',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 8,
|
|
||||||
title: 'Theta',
|
|
||||||
description: 'Eighth card. Click Use to log the card name.',
|
|
||||||
imageUrl: 'https://picsum.photos/seed/theta/400/240',
|
|
||||||
},
|
|
||||||
];
|
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
// Defaults matching Decky practice_engine DeckConfig
|
||||||
|
export const DEFAULT_DECK_CONFIG = {
|
||||||
|
requiredConsecutiveCorrect: 3,
|
||||||
|
defaultAttemptSize: 10,
|
||||||
|
priorityIncreaseOnIncorrect: 5,
|
||||||
|
priorityDecreaseOnCorrect: 2,
|
||||||
|
immediateFeedbackEnabled: true,
|
||||||
|
includeKnownInAttempts: false,
|
||||||
|
shuffleAnswerOrder: true,
|
||||||
|
excludeFlaggedQuestions: false,
|
||||||
|
timeLimitSeconds: null,
|
||||||
|
};
|
||||||
@ -0,0 +1,135 @@
|
|||||||
|
<script>
|
||||||
|
import { onMount } from 'svelte';
|
||||||
|
import { fetchPublishedDecks } from '../lib/api/decks.js';
|
||||||
|
|
||||||
|
let decks = [];
|
||||||
|
let loading = true;
|
||||||
|
let error = null;
|
||||||
|
|
||||||
|
onMount(load);
|
||||||
|
|
||||||
|
async function load() {
|
||||||
|
loading = true;
|
||||||
|
error = null;
|
||||||
|
try {
|
||||||
|
decks = await fetchPublishedDecks();
|
||||||
|
} catch (e) {
|
||||||
|
error = e?.message ?? 'Failed to load community decks';
|
||||||
|
decks = [];
|
||||||
|
} finally {
|
||||||
|
loading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="page">
|
||||||
|
<header class="page-header">
|
||||||
|
<h1>Community</h1>
|
||||||
|
<p class="subtitle">Published decks you can use in the Decky app.</p>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
{#if loading}
|
||||||
|
<p class="muted">Loading…</p>
|
||||||
|
{:else if error}
|
||||||
|
<p class="error">{error}</p>
|
||||||
|
{:else if decks.length === 0}
|
||||||
|
<p class="muted">No published decks yet. Create and publish a deck from My decks to see it here.</p>
|
||||||
|
{:else}
|
||||||
|
<ul class="deck-list">
|
||||||
|
{#each decks as deck (deck.id)}
|
||||||
|
<li class="deck-card">
|
||||||
|
<h3 class="deck-title">{deck.title}</h3>
|
||||||
|
{#if deck.description}
|
||||||
|
<p class="deck-description">{deck.description}</p>
|
||||||
|
{/if}
|
||||||
|
<div class="deck-meta">
|
||||||
|
<span class="deck-count">{deck.question_count ?? 0} questions</span>
|
||||||
|
</div>
|
||||||
|
<p class="use-hint">Use the Decky app to discover and import this deck from the same Supabase project.</p>
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</ul>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.page {
|
||||||
|
padding: 1.5rem;
|
||||||
|
max-width: 900px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-header {
|
||||||
|
margin-bottom: 1.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-header h1 {
|
||||||
|
margin: 0 0 0.25rem 0;
|
||||||
|
font-size: 1.5rem;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.subtitle {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.muted,
|
||||||
|
.error {
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
color: #ef4444;
|
||||||
|
}
|
||||||
|
|
||||||
|
.muted {
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.deck-list {
|
||||||
|
list-style: none;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.deck-card {
|
||||||
|
padding: 1rem 1.25rem;
|
||||||
|
background: var(--card-bg, #1a1a1a);
|
||||||
|
border: 1px solid var(--border, #333);
|
||||||
|
border-radius: 10px;
|
||||||
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.deck-title {
|
||||||
|
margin: 0 0 0.25rem 0;
|
||||||
|
font-size: 1.1rem;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.deck-description {
|
||||||
|
margin: 0 0 0.5rem 0;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
line-height: 1.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.deck-meta {
|
||||||
|
font-size: 0.85rem;
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
margin-bottom: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.use-hint {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 0.8rem;
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@ -0,0 +1,337 @@
|
|||||||
|
<script>
|
||||||
|
import { push } from 'svelte-spa-router';
|
||||||
|
import { auth } from '../lib/stores/auth.js';
|
||||||
|
import { createDeck } from '../lib/api/decks.js';
|
||||||
|
import { DEFAULT_DECK_CONFIG } from '../lib/deckConfig.js';
|
||||||
|
import QuestionEditor from '../lib/QuestionEditor.svelte';
|
||||||
|
|
||||||
|
let title = '';
|
||||||
|
let description = '';
|
||||||
|
let config = { ...DEFAULT_DECK_CONFIG };
|
||||||
|
let questions = [
|
||||||
|
{ prompt: '', explanation: '', answers: [''], correct_answer_indices: [0] },
|
||||||
|
];
|
||||||
|
let saving = false;
|
||||||
|
let error = null;
|
||||||
|
let jsonInput = '';
|
||||||
|
let jsonError = null;
|
||||||
|
$: userId = $auth.user?.id;
|
||||||
|
$: view = $auth.loading ? 'loading' : (userId ? 'form' : 'signin');
|
||||||
|
|
||||||
|
const CONFIG_KEYS = Object.keys(DEFAULT_DECK_CONFIG);
|
||||||
|
|
||||||
|
function loadFromJson() {
|
||||||
|
jsonError = null;
|
||||||
|
let parsed;
|
||||||
|
try {
|
||||||
|
parsed = JSON.parse(jsonInput);
|
||||||
|
} catch (e) {
|
||||||
|
jsonError = 'Invalid JSON: ' + (e?.message ?? 'parse error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!parsed || typeof parsed !== 'object') {
|
||||||
|
jsonError = 'JSON must be an object';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const t = (parsed.title ?? '').toString().trim();
|
||||||
|
if (!t) {
|
||||||
|
jsonError = 'JSON must include a "title"';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const rawQuestions = Array.isArray(parsed.questions) ? parsed.questions : [];
|
||||||
|
if (rawQuestions.length === 0) {
|
||||||
|
jsonError = 'JSON must include a non-empty "questions" array';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
title = t;
|
||||||
|
description = (parsed.description ?? '').toString().trim();
|
||||||
|
const rawConfig = parsed.config && typeof parsed.config === 'object' ? parsed.config : {};
|
||||||
|
config = CONFIG_KEYS.reduce((acc, key) => {
|
||||||
|
if (Object.prototype.hasOwnProperty.call(rawConfig, key)) acc[key] = rawConfig[key];
|
||||||
|
else acc[key] = DEFAULT_DECK_CONFIG[key];
|
||||||
|
return acc;
|
||||||
|
}, { ...DEFAULT_DECK_CONFIG });
|
||||||
|
questions = rawQuestions.map((q) => {
|
||||||
|
const answers = Array.isArray(q.answers) ? q.answers.map((a) => String(a).trim()) : [''];
|
||||||
|
const indices = Array.isArray(q.correctAnswerIndices)
|
||||||
|
? q.correctAnswerIndices
|
||||||
|
: Array.isArray(q.correct_answer_indices)
|
||||||
|
? q.correct_answer_indices
|
||||||
|
: [0];
|
||||||
|
return {
|
||||||
|
prompt: (q.prompt ?? '').toString().trim(),
|
||||||
|
explanation: (q.explanation ?? '').toString().trim(),
|
||||||
|
answers: answers.length ? answers : [''],
|
||||||
|
correct_answer_indices: indices.filter((i) => i >= 0 && i < (answers.length || 1)).length ? indices : [0],
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function addQuestion() {
|
||||||
|
questions = [...questions, { prompt: '', explanation: '', answers: [''], correct_answer_indices: [0] }];
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeQuestion(i) {
|
||||||
|
questions = questions.filter((_, idx) => idx !== i);
|
||||||
|
if (questions.length === 0) {
|
||||||
|
questions = [{ prompt: '', explanation: '', answers: [''], correct_answer_indices: [0] }];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeQuestion(q) {
|
||||||
|
const answers = (q.answers || []).map((a) => String(a).trim()).filter(Boolean);
|
||||||
|
if (answers.length === 0) return null;
|
||||||
|
const indices = (q.correct_answer_indices ?? [0]).filter((i) => i >= 0 && i < answers.length);
|
||||||
|
return {
|
||||||
|
prompt: (q.prompt ?? '').trim(),
|
||||||
|
explanation: (q.explanation ?? '').trim() || null,
|
||||||
|
answers,
|
||||||
|
correct_answer_indices: indices.length ? indices : [0],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function save() {
|
||||||
|
const t = title.trim();
|
||||||
|
if (!t) {
|
||||||
|
error = 'Title is required';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!userId) {
|
||||||
|
error = 'You must be signed in to create a deck';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const normalized = questions.map(normalizeQuestion).filter(Boolean);
|
||||||
|
if (normalized.length === 0) {
|
||||||
|
error = 'Add at least one question with at least one answer';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
saving = true;
|
||||||
|
error = null;
|
||||||
|
try {
|
||||||
|
const id = await createDeck(userId, {
|
||||||
|
title: t,
|
||||||
|
description: description.trim(),
|
||||||
|
config,
|
||||||
|
questions: normalized,
|
||||||
|
});
|
||||||
|
push('/');
|
||||||
|
} catch (e) {
|
||||||
|
error = e?.message ?? 'Failed to create deck';
|
||||||
|
} finally {
|
||||||
|
saving = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function cancel() {
|
||||||
|
push('/');
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="page">
|
||||||
|
<header class="page-header">
|
||||||
|
<h1>Create deck</h1>
|
||||||
|
<div class="header-actions">
|
||||||
|
<button type="button" class="btn btn-secondary" onclick={cancel} disabled={saving}>Cancel</button>
|
||||||
|
<button type="button" class="btn btn-primary" onclick={save} disabled={saving}>
|
||||||
|
{saving ? 'Saving…' : 'Save deck'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
{#if view === 'loading'}
|
||||||
|
<p class="auth-required">Loading…</p>
|
||||||
|
{:else if view === 'signin'}
|
||||||
|
<p class="auth-required">Sign in to create a deck.</p>
|
||||||
|
{:else}
|
||||||
|
<form class="deck-form" onsubmit={(e) => { e.preventDefault(); save(); }}>
|
||||||
|
<details class="import-json">
|
||||||
|
<summary>Import from JSON</summary>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="json-input">Paste deck JSON</label>
|
||||||
|
<textarea
|
||||||
|
id="json-input"
|
||||||
|
class="input textarea json-textarea"
|
||||||
|
bind:value={jsonInput}
|
||||||
|
placeholder="Paste deck JSON (title, description, config, questions)"
|
||||||
|
rows="8"
|
||||||
|
></textarea>
|
||||||
|
<button type="button" class="btn btn-secondary" onclick={loadFromJson}>Load from JSON</button>
|
||||||
|
{#if jsonError}
|
||||||
|
<p class="error">{jsonError}</p>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="title">Title</label>
|
||||||
|
<input id="title" type="text" class="input" bind:value={title} placeholder="Deck title" required />
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="description">Description</label>
|
||||||
|
<textarea id="description" class="input textarea" bind:value={description} placeholder="Optional description" rows="2"></textarea>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h2 class="section-title">Questions</h2>
|
||||||
|
{#each questions as question, i}
|
||||||
|
<QuestionEditor bind:question index={i} onRemove={() => removeQuestion(i)} />
|
||||||
|
{/each}
|
||||||
|
<button type="button" class="btn btn-add-q" onclick={addQuestion}>+ Add question</button>
|
||||||
|
|
||||||
|
{#if error}
|
||||||
|
<p class="error">{error}</p>
|
||||||
|
{/if}
|
||||||
|
</form>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.page {
|
||||||
|
padding: 1.5rem;
|
||||||
|
max-width: 700px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 1rem;
|
||||||
|
margin-bottom: 1.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-header h1 {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 1.5rem;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.header-actions {
|
||||||
|
display: flex;
|
||||||
|
gap: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.auth-required {
|
||||||
|
margin: 0;
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.deck-form {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.import-json {
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
padding: 0.75rem;
|
||||||
|
border: 1px solid var(--border, #333);
|
||||||
|
border-radius: 6px;
|
||||||
|
background: var(--card-bg, #1e1e1e);
|
||||||
|
}
|
||||||
|
|
||||||
|
.import-json summary {
|
||||||
|
cursor: pointer;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.import-json summary:hover {
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.json-textarea {
|
||||||
|
font-family: ui-monospace, monospace;
|
||||||
|
font-size: 0.85rem;
|
||||||
|
margin-bottom: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-group {
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-group label {
|
||||||
|
display: block;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
margin-bottom: 0.35rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.input {
|
||||||
|
width: 100%;
|
||||||
|
padding: 0.6rem 0.75rem;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
border: 1px solid var(--border, #333);
|
||||||
|
border-radius: 6px;
|
||||||
|
background: var(--card-bg, #1e1e1e);
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.textarea {
|
||||||
|
resize: vertical;
|
||||||
|
min-height: 2.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section-title {
|
||||||
|
margin: 1.5rem 0 0.75rem 0;
|
||||||
|
font-size: 1.1rem;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-add-q {
|
||||||
|
padding: 0.5rem 1rem;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
border: 1px dashed var(--border, #333);
|
||||||
|
border-radius: 6px;
|
||||||
|
background: transparent;
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
cursor: pointer;
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-add-q:hover {
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
border-color: var(--border-hover, #444);
|
||||||
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
margin: 0 0 1rem 0;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
color: #ef4444;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn {
|
||||||
|
padding: 0.5rem 1rem;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
font-weight: 500;
|
||||||
|
border: 1px solid var(--border, #333);
|
||||||
|
border-radius: 6px;
|
||||||
|
background: var(--card-bg, #1e1e1e);
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn:hover:not(:disabled) {
|
||||||
|
background: var(--hover-bg, #2a2a2a);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn:disabled {
|
||||||
|
opacity: 0.6;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary {
|
||||||
|
background: var(--accent, #3b82f6);
|
||||||
|
border-color: var(--accent, #3b82f6);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary:hover:not(:disabled) {
|
||||||
|
background: #2563eb;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-secondary {
|
||||||
|
background: transparent;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@ -0,0 +1,341 @@
|
|||||||
|
<script>
|
||||||
|
import { push } from 'svelte-spa-router';
|
||||||
|
import { auth } from '../lib/stores/auth.js';
|
||||||
|
import { fetchDeckWithQuestions, updateDeck, togglePublished, deleteDeck } from '../lib/api/decks.js';
|
||||||
|
import { DEFAULT_DECK_CONFIG } from '../lib/deckConfig.js';
|
||||||
|
import QuestionEditor from '../lib/QuestionEditor.svelte';
|
||||||
|
|
||||||
|
export let params = {};
|
||||||
|
$: deckId = params?.id;
|
||||||
|
|
||||||
|
let title = '';
|
||||||
|
let description = '';
|
||||||
|
let config = { ...DEFAULT_DECK_CONFIG };
|
||||||
|
let published = false;
|
||||||
|
let questions = [];
|
||||||
|
let loading = true;
|
||||||
|
let saving = false;
|
||||||
|
let error = null;
|
||||||
|
|
||||||
|
$: userId = $auth.user?.id;
|
||||||
|
|
||||||
|
let prevDeckId = '';
|
||||||
|
$: if (deckId && userId && deckId !== prevDeckId) {
|
||||||
|
prevDeckId = deckId;
|
||||||
|
load();
|
||||||
|
} else if (!deckId || !userId) {
|
||||||
|
loading = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function load() {
|
||||||
|
if (!deckId) return;
|
||||||
|
loading = true;
|
||||||
|
error = null;
|
||||||
|
try {
|
||||||
|
const deck = await fetchDeckWithQuestions(deckId);
|
||||||
|
if (deck.owner_id !== userId) {
|
||||||
|
push('/');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
title = deck.title ?? '';
|
||||||
|
description = deck.description ?? '';
|
||||||
|
config = { ...DEFAULT_DECK_CONFIG, ...(deck.config || {}) };
|
||||||
|
published = !!deck.published;
|
||||||
|
questions = (deck.questions || []).map((q) => ({
|
||||||
|
prompt: q.prompt ?? '',
|
||||||
|
explanation: q.explanation ?? '',
|
||||||
|
answers: Array.isArray(q.answers) ? [...q.answers] : [''],
|
||||||
|
correct_answer_indices: Array.isArray(q.correct_answer_indices) ? [...q.correct_answer_indices] : [0],
|
||||||
|
}));
|
||||||
|
if (questions.length === 0) {
|
||||||
|
questions = [{ prompt: '', explanation: '', answers: [''], correct_answer_indices: [0] }];
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
error = e?.message ?? 'Failed to load deck';
|
||||||
|
questions = [{ prompt: '', explanation: '', answers: [''], correct_answer_indices: [0] }];
|
||||||
|
} finally {
|
||||||
|
loading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function addQuestion() {
|
||||||
|
questions = [...questions, { prompt: '', explanation: '', answers: [''], correct_answer_indices: [0] }];
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeQuestion(i) {
|
||||||
|
questions = questions.filter((_, idx) => idx !== i);
|
||||||
|
if (questions.length === 0) {
|
||||||
|
questions = [{ prompt: '', explanation: '', answers: [''], correct_answer_indices: [0] }];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeQuestion(q) {
|
||||||
|
const answers = (q.answers || []).map((a) => String(a).trim()).filter(Boolean);
|
||||||
|
if (answers.length === 0) return null;
|
||||||
|
const indices = (q.correct_answer_indices ?? [0]).filter((i) => i >= 0 && i < answers.length);
|
||||||
|
return {
|
||||||
|
prompt: (q.prompt ?? '').trim(),
|
||||||
|
explanation: (q.explanation ?? '').trim() || null,
|
||||||
|
answers,
|
||||||
|
correct_answer_indices: indices.length ? indices : [0],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function save() {
|
||||||
|
const t = title.trim();
|
||||||
|
if (!t) {
|
||||||
|
error = 'Title is required';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!userId || !deckId) return;
|
||||||
|
const normalized = questions.map(normalizeQuestion).filter(Boolean);
|
||||||
|
if (normalized.length === 0) {
|
||||||
|
error = 'Add at least one question with at least one answer';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
saving = true;
|
||||||
|
error = null;
|
||||||
|
try {
|
||||||
|
await updateDeck(deckId, {
|
||||||
|
title: t,
|
||||||
|
description: description.trim(),
|
||||||
|
config,
|
||||||
|
questions: normalized,
|
||||||
|
});
|
||||||
|
push('/');
|
||||||
|
} catch (e) {
|
||||||
|
error = e?.message ?? 'Failed to save deck';
|
||||||
|
} finally {
|
||||||
|
saving = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function togglePublish() {
|
||||||
|
if (!deckId) return;
|
||||||
|
try {
|
||||||
|
await togglePublished(deckId, !published);
|
||||||
|
published = !published;
|
||||||
|
} catch (e) {
|
||||||
|
error = e?.message ?? 'Failed to update publish status';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function remove() {
|
||||||
|
if (!deckId || !confirm('Delete this deck? This cannot be undone.')) return;
|
||||||
|
try {
|
||||||
|
await deleteDeck(deckId);
|
||||||
|
push('/');
|
||||||
|
} catch (e) {
|
||||||
|
error = e?.message ?? 'Failed to delete deck';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function cancel() {
|
||||||
|
push('/');
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="page">
|
||||||
|
<header class="page-header">
|
||||||
|
<h1>Edit deck</h1>
|
||||||
|
<div class="header-actions">
|
||||||
|
<button type="button" class="btn btn-danger" onclick={remove} disabled={saving}>Delete</button>
|
||||||
|
<button type="button" class="btn btn-secondary" onclick={cancel} disabled={saving}>Cancel</button>
|
||||||
|
<button type="button" class="btn btn-primary" onclick={save} disabled={saving}>
|
||||||
|
{saving ? 'Saving…' : 'Save'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
{#if $auth.loading}
|
||||||
|
<p class="muted">Loading…</p>
|
||||||
|
{:else if !userId}
|
||||||
|
<p class="auth-required">Sign in to edit decks.</p>
|
||||||
|
{:else if loading}
|
||||||
|
<p class="muted">Loading…</p>
|
||||||
|
{:else}
|
||||||
|
<form class="deck-form" onsubmit={(e) => { e.preventDefault(); save(); }}>
|
||||||
|
<div class="publish-row">
|
||||||
|
<label class="toggle-label">
|
||||||
|
<input type="checkbox" checked={published} onchange={togglePublish} />
|
||||||
|
Published (visible in Community)
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="title">Title</label>
|
||||||
|
<input id="title" type="text" class="input" bind:value={title} placeholder="Deck title" required />
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="description">Description</label>
|
||||||
|
<textarea id="description" class="input textarea" bind:value={description} placeholder="Optional description" rows="2"></textarea>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h2 class="section-title">Questions</h2>
|
||||||
|
{#each questions as question, i}
|
||||||
|
<QuestionEditor bind:question index={i} onRemove={() => removeQuestion(i)} />
|
||||||
|
{/each}
|
||||||
|
<button type="button" class="btn btn-add-q" onclick={addQuestion}>+ Add question</button>
|
||||||
|
|
||||||
|
{#if error}
|
||||||
|
<p class="error">{error}</p>
|
||||||
|
{/if}
|
||||||
|
</form>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.page {
|
||||||
|
padding: 1.5rem;
|
||||||
|
max-width: 700px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 1rem;
|
||||||
|
margin-bottom: 1.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-header h1 {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 1.5rem;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.header-actions {
|
||||||
|
display: flex;
|
||||||
|
gap: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.publish-row {
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.toggle-label {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.toggle-label input {
|
||||||
|
width: 1rem;
|
||||||
|
height: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.auth-required,
|
||||||
|
.muted {
|
||||||
|
margin: 0;
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.deck-form {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-group {
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-group label {
|
||||||
|
display: block;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
margin-bottom: 0.35rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.input {
|
||||||
|
width: 100%;
|
||||||
|
padding: 0.6rem 0.75rem;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
border: 1px solid var(--border, #333);
|
||||||
|
border-radius: 6px;
|
||||||
|
background: var(--card-bg, #1e1e1e);
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.textarea {
|
||||||
|
resize: vertical;
|
||||||
|
min-height: 2.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section-title {
|
||||||
|
margin: 1.5rem 0 0.75rem 0;
|
||||||
|
font-size: 1.1rem;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-add-q {
|
||||||
|
padding: 0.5rem 1rem;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
border: 1px dashed var(--border, #333);
|
||||||
|
border-radius: 6px;
|
||||||
|
background: transparent;
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
cursor: pointer;
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-add-q:hover {
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
border-color: var(--border-hover, #444);
|
||||||
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
margin: 0 0 1rem 0;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
color: #ef4444;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn {
|
||||||
|
padding: 0.5rem 1rem;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
font-weight: 500;
|
||||||
|
border: 1px solid var(--border, #333);
|
||||||
|
border-radius: 6px;
|
||||||
|
background: var(--card-bg, #1e1e1e);
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn:hover:not(:disabled) {
|
||||||
|
background: var(--hover-bg, #2a2a2a);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn:disabled {
|
||||||
|
opacity: 0.6;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary {
|
||||||
|
background: var(--accent, #3b82f6);
|
||||||
|
border-color: var(--accent, #3b82f6);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary:hover:not(:disabled) {
|
||||||
|
background: #2563eb;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-secondary {
|
||||||
|
background: transparent;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-danger {
|
||||||
|
background: transparent;
|
||||||
|
color: #ef4444;
|
||||||
|
border-color: #ef4444;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-danger:hover:not(:disabled) {
|
||||||
|
background: rgba(239, 68, 68, 0.15);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@ -0,0 +1,211 @@
|
|||||||
|
<script>
|
||||||
|
import { push } from 'svelte-spa-router';
|
||||||
|
import { auth } from '../lib/stores/auth.js';
|
||||||
|
import { fetchMyDecks } from '../lib/api/decks.js';
|
||||||
|
|
||||||
|
let decks = [];
|
||||||
|
let loading = true;
|
||||||
|
let error = null;
|
||||||
|
|
||||||
|
$: userId = $auth.user?.id;
|
||||||
|
|
||||||
|
let prevUserId = null;
|
||||||
|
$: if (!$auth.loading && userId && userId !== prevUserId) {
|
||||||
|
prevUserId = userId;
|
||||||
|
load();
|
||||||
|
} else if (!$auth.loading && !userId) {
|
||||||
|
loading = false;
|
||||||
|
prevUserId = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function load() {
|
||||||
|
if (!userId) return;
|
||||||
|
loading = true;
|
||||||
|
error = null;
|
||||||
|
try {
|
||||||
|
decks = await fetchMyDecks(userId);
|
||||||
|
} catch (e) {
|
||||||
|
error = e?.message ?? 'Failed to load decks';
|
||||||
|
decks = [];
|
||||||
|
} finally {
|
||||||
|
loading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function goCreate() {
|
||||||
|
push('/decks/new');
|
||||||
|
}
|
||||||
|
|
||||||
|
function goEdit(id) {
|
||||||
|
push(`/decks/${id}/edit`);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="page">
|
||||||
|
<header class="page-header">
|
||||||
|
<h1>My decks</h1>
|
||||||
|
<button type="button" class="btn btn-primary" onclick={goCreate}>Create deck</button>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
{#if $auth.loading}
|
||||||
|
<p class="muted">Loading…</p>
|
||||||
|
{:else if !userId}
|
||||||
|
<p class="auth-required">Sign in to create and manage your decks.</p>
|
||||||
|
{:else if loading}
|
||||||
|
<p class="muted">Loading…</p>
|
||||||
|
{:else if error}
|
||||||
|
<p class="error">{error}</p>
|
||||||
|
{:else if decks.length === 0}
|
||||||
|
<p class="muted">No decks yet. Create your first deck to get started.</p>
|
||||||
|
{:else}
|
||||||
|
<ul class="deck-list">
|
||||||
|
{#each decks as deck (deck.id)}
|
||||||
|
<li class="deck-card">
|
||||||
|
<div class="deck-card-main" onclick={() => goEdit(deck.id)} onkeydown={(e) => e.key === 'Enter' && goEdit(deck.id)} role="button" tabindex="0">
|
||||||
|
<h3 class="deck-title">{deck.title}</h3>
|
||||||
|
{#if deck.description}
|
||||||
|
<p class="deck-description">{deck.description}</p>
|
||||||
|
{/if}
|
||||||
|
<div class="deck-meta">
|
||||||
|
<span class="deck-count">{deck.question_count ?? 0} questions</span>
|
||||||
|
{#if deck.published}
|
||||||
|
<span class="badge published">Published</span>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="deck-card-actions">
|
||||||
|
<button type="button" class="btn btn-small" onclick={() => goEdit(deck.id)}>Edit</button>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</ul>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.page {
|
||||||
|
padding: 1.5rem;
|
||||||
|
max-width: 900px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-header {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 1rem;
|
||||||
|
margin-bottom: 1.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-header h1 {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 1.5rem;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.auth-required,
|
||||||
|
.muted,
|
||||||
|
.error {
|
||||||
|
margin: 0;
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
color: #ef4444;
|
||||||
|
}
|
||||||
|
|
||||||
|
.deck-list {
|
||||||
|
list-style: none;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.75rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.deck-card {
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-start;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 1rem;
|
||||||
|
padding: 1rem 1.25rem;
|
||||||
|
background: var(--card-bg, #1a1a1a);
|
||||||
|
border: 1px solid var(--border, #333);
|
||||||
|
border-radius: 10px;
|
||||||
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.deck-card-main {
|
||||||
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.deck-title {
|
||||||
|
margin: 0 0 0.25rem 0;
|
||||||
|
font-size: 1.1rem;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.deck-description {
|
||||||
|
margin: 0 0 0.5rem 0;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
line-height: 1.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.deck-meta {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
font-size: 0.85rem;
|
||||||
|
color: var(--text-muted, #a0a0a0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.badge {
|
||||||
|
padding: 0.2rem 0.5rem;
|
||||||
|
border-radius: 4px;
|
||||||
|
font-size: 0.75rem;
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
.badge.published {
|
||||||
|
background: rgba(34, 197, 94, 0.2);
|
||||||
|
color: #22c55e;
|
||||||
|
}
|
||||||
|
|
||||||
|
.deck-card-actions {
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn {
|
||||||
|
padding: 0.5rem 1rem;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
font-weight: 500;
|
||||||
|
border: 1px solid var(--border, #333);
|
||||||
|
border-radius: 6px;
|
||||||
|
background: var(--card-bg, #1e1e1e);
|
||||||
|
color: var(--text-primary, #f0f0f0);
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn:hover {
|
||||||
|
background: var(--hover-bg, #2a2a2a);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary {
|
||||||
|
background: var(--accent, #3b82f6);
|
||||||
|
border-color: var(--accent, #3b82f6);
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary:hover {
|
||||||
|
background: #2563eb;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-small {
|
||||||
|
padding: 0.35rem 0.75rem;
|
||||||
|
font-size: 0.85rem;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@ -0,0 +1,117 @@
|
|||||||
|
-- Decks table (metadata + config for Decky quiz/flashcard app)
|
||||||
|
CREATE TABLE IF NOT EXISTS public.decks (
|
||||||
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
owner_id uuid NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
|
||||||
|
title text NOT NULL,
|
||||||
|
description text NOT NULL DEFAULT '',
|
||||||
|
config jsonb NOT NULL DEFAULT '{
|
||||||
|
"requiredConsecutiveCorrect": 3,
|
||||||
|
"defaultAttemptSize": 10,
|
||||||
|
"priorityIncreaseOnIncorrect": 5,
|
||||||
|
"priorityDecreaseOnCorrect": 2,
|
||||||
|
"immediateFeedbackEnabled": true,
|
||||||
|
"includeKnownInAttempts": false,
|
||||||
|
"shuffleAnswerOrder": true,
|
||||||
|
"excludeFlaggedQuestions": false,
|
||||||
|
"timeLimitSeconds": null
|
||||||
|
}'::jsonb,
|
||||||
|
published boolean NOT NULL DEFAULT false,
|
||||||
|
created_at timestamptz NOT NULL DEFAULT now(),
|
||||||
|
updated_at timestamptz NOT NULL DEFAULT now()
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Questions table (deck content)
|
||||||
|
CREATE TABLE IF NOT EXISTS public.questions (
|
||||||
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
deck_id uuid NOT NULL REFERENCES public.decks(id) ON DELETE CASCADE,
|
||||||
|
sort_order int NOT NULL DEFAULT 0,
|
||||||
|
prompt text NOT NULL,
|
||||||
|
explanation text,
|
||||||
|
answers jsonb NOT NULL DEFAULT '[]'::jsonb,
|
||||||
|
correct_answer_indices jsonb NOT NULL DEFAULT '[]'::jsonb
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_questions_deck_id ON public.questions(deck_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_decks_owner_id ON public.decks(owner_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_decks_published ON public.decks(published) WHERE published = true;
|
||||||
|
|
||||||
|
-- updated_at trigger for decks
|
||||||
|
CREATE OR REPLACE FUNCTION public.set_decks_updated_at()
|
||||||
|
RETURNS TRIGGER AS $$
|
||||||
|
BEGIN
|
||||||
|
NEW.updated_at = now();
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
DROP TRIGGER IF EXISTS decks_updated_at ON public.decks;
|
||||||
|
CREATE TRIGGER decks_updated_at
|
||||||
|
BEFORE UPDATE ON public.decks
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE PROCEDURE public.set_decks_updated_at();
|
||||||
|
|
||||||
|
-- RLS
|
||||||
|
ALTER TABLE public.decks ENABLE ROW LEVEL SECURITY;
|
||||||
|
ALTER TABLE public.questions ENABLE ROW LEVEL SECURITY;
|
||||||
|
|
||||||
|
-- decks: SELECT if owner or published; mutate only if owner
|
||||||
|
CREATE POLICY decks_select ON public.decks
|
||||||
|
FOR SELECT
|
||||||
|
USING (owner_id = auth.uid() OR published = true);
|
||||||
|
|
||||||
|
CREATE POLICY decks_insert ON public.decks
|
||||||
|
FOR INSERT
|
||||||
|
WITH CHECK (owner_id = auth.uid());
|
||||||
|
|
||||||
|
CREATE POLICY decks_update ON public.decks
|
||||||
|
FOR UPDATE
|
||||||
|
USING (owner_id = auth.uid())
|
||||||
|
WITH CHECK (owner_id = auth.uid());
|
||||||
|
|
||||||
|
CREATE POLICY decks_delete ON public.decks
|
||||||
|
FOR DELETE
|
||||||
|
USING (owner_id = auth.uid());
|
||||||
|
|
||||||
|
-- questions: SELECT if deck is visible; mutate only if deck is owned
|
||||||
|
CREATE POLICY questions_select ON public.questions
|
||||||
|
FOR SELECT
|
||||||
|
USING (
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1 FROM public.decks d
|
||||||
|
WHERE d.id = questions.deck_id
|
||||||
|
AND (d.owner_id = auth.uid() OR d.published = true)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE POLICY questions_insert ON public.questions
|
||||||
|
FOR INSERT
|
||||||
|
WITH CHECK (
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1 FROM public.decks d
|
||||||
|
WHERE d.id = questions.deck_id AND d.owner_id = auth.uid()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE POLICY questions_update ON public.questions
|
||||||
|
FOR UPDATE
|
||||||
|
USING (
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1 FROM public.decks d
|
||||||
|
WHERE d.id = questions.deck_id AND d.owner_id = auth.uid()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
WITH CHECK (
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1 FROM public.decks d
|
||||||
|
WHERE d.id = questions.deck_id AND d.owner_id = auth.uid()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE POLICY questions_delete ON public.questions
|
||||||
|
FOR DELETE
|
||||||
|
USING (
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1 FROM public.decks d
|
||||||
|
WHERE d.id = questions.deck_id AND d.owner_id = auth.uid()
|
||||||
|
)
|
||||||
|
);
|
||||||
Loading…
Reference in new issue