-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
279 additions
and
38 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
src/lib/server/prisma/migrations/20231010021216_add_settings_to_user/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "settings" JSONB NOT NULL DEFAULT '{}'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export function secondsToDHM(seconds: number) { | ||
const days = Math.floor(seconds / (3600 * 24)); | ||
const hours = Math.floor((seconds % (3600 * 24)) / 3600); | ||
const minutes = Math.floor((seconds % 3600) / 60); | ||
return { days, hours, minutes }; | ||
} | ||
|
||
export function DHMToSeconds({ | ||
days, | ||
hours, | ||
minutes | ||
}: { | ||
days?: number; | ||
hours?: number; | ||
minutes?: number; | ||
}) { | ||
return (days ?? 0) * 3600 * 24 + (hours ?? 0) * 3600 + (minutes ?? 0) * 60; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,19 @@ | ||
import { getUserIdFromCookie } from '$lib/server/auth'; | ||
import type { UserSettings } from '$lib/types'; | ||
import prisma from '@db'; | ||
|
||
export async function load({ cookies }) { | ||
const userId = await getUserIdFromCookie(cookies); | ||
return { loggedIn: !!userId }; | ||
|
||
let settings: UserSettings | undefined; | ||
|
||
if (userId) { | ||
const user = await prisma.user.findUnique({ | ||
where: { id: userId }, | ||
select: { settings: true } | ||
}); | ||
settings = user?.settings as UserSettings; | ||
} | ||
|
||
return { loggedIn: !!userId, settings }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<script lang="ts"> | ||
import { goto } from '$app/navigation'; | ||
import { onMount } from 'svelte'; | ||
let cmdKey = 'Ctrl'; | ||
onMount(() => { | ||
const isMac = | ||
(navigator as any).userAgentData?.platform?.toLowerCase() === 'macos' || | ||
navigator.platform?.toLowerCase().startsWith('mac'); | ||
cmdKey = isMac ? '⌘' : 'Ctrl'; | ||
document.addEventListener('keydown', (e) => { | ||
if (e.key === 'n' && (e.ctrlKey || e.metaKey)) { | ||
e.preventDefault(); | ||
goto('/'); | ||
} | ||
if (e.key === 'i' && (e.ctrlKey || e.metaKey)) { | ||
e.preventDefault(); | ||
goto('/info'); | ||
} | ||
}); | ||
}); | ||
</script> | ||
|
||
<div class="p-2 min-h-screen w-screen flex flex-col text-primary"> | ||
<div class="pb-4"> | ||
<div class="flex flex-row items-center gap-4"> | ||
<h1 class="mr-auto text-2xl"><a href="/">YABin</a></h1> | ||
|
||
<!-- <a class="underline underline-offset-4 px-2 py-1" href="/dashboard/pastes">Pastes</a> --> | ||
<a class="underline underline-offset-4 px-2 py-1" href="/dashboard/settings">Settings</a> | ||
|
||
<button | ||
class="underline underline-offset-4 px-2 py-1" | ||
title="{cmdKey}+I" | ||
on:click={() => goto('/info')} | ||
> | ||
Info | ||
</button> | ||
|
||
<button | ||
class="bg-amber-500 text-black text-lg px-4 py-1" | ||
title="{cmdKey}+N" | ||
on:click={() => goto('/')} | ||
> | ||
New | ||
</button> | ||
</div> | ||
</div> | ||
|
||
<div class="px-24 py-4"> | ||
<slot /> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { getUserIdFromCookie } from '$lib/server/auth'; | ||
import { redirect, type Actions } from '@sveltejs/kit'; | ||
import type { PageServerLoad } from './$types'; | ||
import prisma from '@db'; | ||
import type { UserSettings } from '$lib/types'; | ||
|
||
export const load: PageServerLoad = async ({ cookies }) => { | ||
const userId = await getUserIdFromCookie(cookies); | ||
if (!userId) throw redirect(303, '/login'); | ||
|
||
const user = await prisma.user.findUnique({ | ||
where: { id: userId }, | ||
select: { settings: true } | ||
}); | ||
|
||
return { settings: user?.settings as UserSettings }; | ||
}; | ||
|
||
export const actions: Actions = { | ||
defaults: async ({ cookies, request }) => { | ||
const userId = await getUserIdFromCookie(cookies); | ||
if (!userId) throw redirect(303, '/login'); | ||
|
||
const formData = await request.formData(); | ||
const expiresAfterSeconds = parseInt(formData.get('expires-after-seconds')?.toString() ?? '0'); | ||
const data = { | ||
encrypted: formData.get('encrypted') === 'on', | ||
burnAfterRead: formData.get('burn-after-read') === 'on', | ||
expiresAfterSeconds: Math.max(0, Math.min(365 * 24 * 3600, expiresAfterSeconds)) | ||
}; | ||
|
||
const user = await prisma.user.update({ | ||
where: { id: userId }, | ||
data: { settings: { defaults: data } }, | ||
select: { settings: true } | ||
}); | ||
|
||
return { defaultsForm: { success: true, settings: user.settings as UserSettings } }; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<script lang="ts"> | ||
import { applyAction, enhance } from '$app/forms'; | ||
import { goto } from '$app/navigation'; | ||
import type { UserSettings } from '$lib/types'; | ||
import { DHMToSeconds, secondsToDHM } from '$lib/utils/time'; | ||
import type { ActionData, PageData } from './$types'; | ||
export let data: PageData; | ||
export let form: ActionData; | ||
let expiresAfter: { | ||
days?: number; | ||
hours?: number; | ||
minutes?: number; | ||
} = {}; | ||
let expiresAfterSeconds: number = 0; | ||
$: settings = form?.defaultsForm?.settings || data?.settings; | ||
const updateInitialConfig = (defaults: UserSettings['defaults']) => { | ||
if (!defaults) return; | ||
if (defaults?.expiresAfterSeconds) { | ||
expiresAfterSeconds = defaults.expiresAfterSeconds; | ||
expiresAfter = secondsToDHM(expiresAfterSeconds); | ||
} | ||
}; | ||
$: updateInitialConfig(settings?.defaults); | ||
$: { | ||
expiresAfterSeconds = DHMToSeconds(expiresAfter); | ||
// Don't allow pastes to be saved for more than a year | ||
expiresAfterSeconds = Math.min(expiresAfterSeconds, 365 * 24 * 60 * 60); | ||
// Don't allow pastes to be saved for less than 5 minutes | ||
if (expiresAfterSeconds > 0) { | ||
expiresAfterSeconds = Math.max(expiresAfterSeconds, 5 * 60); | ||
expiresAfter = secondsToDHM(expiresAfterSeconds); | ||
} else { | ||
expiresAfter = {}; | ||
} | ||
} | ||
</script> | ||
|
||
<h1 class="text-5xl">Settings</h1> | ||
|
||
<div class="px-4"> | ||
<h4 class="text-2xl mt-6 mb-4">Defaults</h4> | ||
|
||
<form | ||
method="post" | ||
action="?/defaults" | ||
class="mt-2 flex flex-col gap-4" | ||
use:enhance={() => { | ||
return async ({ result }) => { | ||
if (result.type === 'redirect') await goto(result.location); | ||
else await applyAction(result); | ||
}; | ||
}} | ||
> | ||
<div> | ||
<label for="encrypted" class="py-2">Encrypted</label> | ||
<input | ||
id="encrypted" | ||
class="bg-dark px-2 py-1" | ||
type="checkbox" | ||
name="encrypted" | ||
checked={settings?.defaults?.encrypted} | ||
/> | ||
</div> | ||
|
||
<div> | ||
<label for="burn-after-read" class="py-2">Burn after read</label> | ||
<input | ||
id="burn-after-read" | ||
class="bg-dark px-2 py-1" | ||
type="checkbox" | ||
name="burn-after-read" | ||
checked={settings?.defaults?.burnAfterRead} | ||
/> | ||
</div> | ||
|
||
<div> | ||
<span>Expires in:</span> | ||
<div class="grid grid-cols-3 gap-2 justify-center items-center"> | ||
<input | ||
type="number" | ||
class="bg-dark py-1 text-center" | ||
placeholder="DD" | ||
bind:value={expiresAfter.days} | ||
/> | ||
<input | ||
type="number" | ||
class="bg-dark py-1 text-center" | ||
placeholder="HH" | ||
bind:value={expiresAfter.hours} | ||
/> | ||
<input | ||
type="number" | ||
class="bg-dark py-1 text-center" | ||
placeholder="MM" | ||
bind:value={expiresAfter.minutes} | ||
/> | ||
<input type="hidden" name="expires-after-seconds" bind:value={expiresAfterSeconds} /> | ||
</div> | ||
</div> | ||
|
||
<div class="mt-2"> | ||
<button class="bg-amber-500 text-black text-lg px-4 py-1">Save</button> | ||
{#if form?.defaultsForm.success} | ||
<span class="text-green-500">Saved</span> | ||
<!-- {:else if form?.defaultsForm.error} | ||
<span class="text-red-500">Error</span> --> | ||
{/if} | ||
</div> | ||
</form> | ||
</div> |