Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ const config: ConfigType = {

export type Config = typeof config;
export default config;

export const FEATURES = {
DAILY_FREE_CREDITS: false
} as const;
71 changes: 38 additions & 33 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PUBLIC_SUPABASE_URL } from '$env/static/public';
import { PRIVATE_SUPABASE_SERVICE_ROLE } from '$env/static/private';
import type { Database } from '$lib/supabase-types';
import { isBefore, startOfDay, parseISO, formatISO } from 'date-fns';
import { FEATURES } from '$lib/config';

const supabaseAdmin = createClient<Database>(PUBLIC_SUPABASE_URL, PRIVATE_SUPABASE_SERVICE_ROLE);

Expand All @@ -17,40 +18,42 @@ export async function load({ locals: { session }, depends }) {
})
]);

// If no payment record exists or if remaining generations is 0 and last update was before today,
// If daily free credits are enabled and no payment record exists or if remaining generations is 0 and last update was before today,
// create/update record with 1 free generation
if (
!paymentData ||
(paymentData.remaining_generations === 0 &&
(!paymentData.updated_at ||
isBefore(parseISO(paymentData.updated_at), startOfDay(new Date()))))
) {
if (!paymentData) {
// Insert new record
const { data: newPaymentData } = await supabaseAdmin
.from('user_payments')
.insert({
user_id: session.user.id,
remaining_generations: 1,
updated_at: formatISO(new Date())
})
.select()
.single();
if (FEATURES.DAILY_FREE_CREDITS) {
if (
!paymentData ||
(paymentData.remaining_generations === 0 &&
(!paymentData.updated_at ||
isBefore(parseISO(paymentData.updated_at), startOfDay(new Date()))))
) {
if (!paymentData) {
// Insert new record
const { data: newPaymentData } = await supabaseAdmin
.from('user_payments')
.insert({
user_id: session.user.id,
remaining_generations: 1,
updated_at: formatISO(new Date())
})
.select()
.single();

paymentData = newPaymentData;
} else {
// Update existing record
const { data: updatedPaymentData } = await supabaseAdmin
.from('user_payments')
.update({
remaining_generations: 1,
updated_at: formatISO(new Date())
})
.eq('user_id', session.user.id)
.select()
.single();
paymentData = newPaymentData;
} else {
// Update existing record
const { data: updatedPaymentData } = await supabaseAdmin
.from('user_payments')
.update({
remaining_generations: 1,
updated_at: formatISO(new Date())
})
.eq('user_id', session.user.id)
.select()
.single();

paymentData = updatedPaymentData;
paymentData = updatedPaymentData;
}
}
}

Expand All @@ -73,11 +76,13 @@ export async function load({ locals: { session }, depends }) {

return {
remainingGenerations: paymentData?.remaining_generations || 0,
images
images,
features: FEATURES
};
}
return {
remainingGenerations: 0,
images: []
images: [],
features: FEATURES
};
}
4 changes: 2 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

let { data } = $props();

const { images, remainingGenerations } = $derived(data);
const { images, remainingGenerations, features } = $derived(data);
</script>

<Meta
Expand All @@ -31,4 +31,4 @@
author="EmaDev"
/>
<Board user={data.user} {images} />
<Sidebar user={data.user} {remainingGenerations} />
<Sidebar user={data.user} {remainingGenerations} {features} />
12 changes: 10 additions & 2 deletions src/routes/sidebar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@

let {
user,
remainingGenerations
remainingGenerations,
features
}: {
user: User | null;
remainingGenerations: number;
features: { DAILY_FREE_CREDITS: boolean };
} = $props();

let dropZone: DropZone | undefined = $state();
Expand Down Expand Up @@ -463,7 +465,13 @@
>
<div class="flex w-full flex-col gap-4">
{#if !user}
<p class="text-center text-sm text-error">Login to get daily free credits or buy more</p>
<p class="text-center text-sm text-error">
{#if features.DAILY_FREE_CREDITS}
Login to get daily free credits or buy more
{:else}
Login to generate avatars or buy credits
{/if}
</p>
{/if}
<div class="flex w-full gap-2">
<DaisyButton
Expand Down