Skip to content

Commit

Permalink
fix site previews, update primo
Browse files Browse the repository at this point in the history
  • Loading branch information
mateomorris committed Aug 26, 2023
1 parent 329f008 commit 419f30f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 36 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"dependencies": {
"@fontsource/fira-code": "^5.0.5",
"@iconify/svelte": "^2.2.1",
"@primocms/builder": "^0.1.13",
"@primocms/builder": "^0.1.14",
"@rollup/browser": "^3.28.0",
"@supabase/auth-helpers-sveltekit": "^0.10.2",
"@supabase/supabase-js": "^2.31.0",
Expand Down
62 changes: 39 additions & 23 deletions src/routes/+layout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { PUBLIC_SUPABASE_PUBLIC_KEY, PUBLIC_SUPABASE_URL } from '$env/static/public'
import {
PUBLIC_SUPABASE_PUBLIC_KEY,
PUBLIC_SUPABASE_URL,
} from '$env/static/public'
import { createSupabaseLoadClient } from '@supabase/auth-helpers-sveltekit'
import { redirect } from '@sveltejs/kit'

Expand All @@ -13,49 +16,62 @@ export async function load(event) {
event: { fetch },
serverSession: event?.data?.session,
})
const { data: { session } } = await supabase.auth.getSession()
const {
data: { session },
} = await supabase.auth.getSession()

if (!session && !event.url.pathname.startsWith('/auth')) {
throw redirect(303, '/auth')
} else if (session) {
// const site = event.params['site']
// const site = event.params['site']
const { sites, user } = await Promise.all([
supabase.from('sites').select('id, name, url, active_deployment, collaborators (*)').order('created_at', { ascending: true }),
supabase.from('users').select('*, server_members (admin, role), collaborators (role)').eq('id', session.user.id).single()
supabase
.from('sites')
.select('id, name, url, active_deployment, collaborators (*)')
.order('created_at', { ascending: true }),
supabase
.from('users')
.select('*, server_members (admin, role), collaborators (role)')
.eq('id', session.user.id)
.single(),
]).then(([{ data: sites }, { data: user }]) => {

const [server_member] = user.server_members
const [collaborator] = user.collaborators

const user_final = server_member ? {
...user,
server_member: true,
admin: server_member.admin,
role: server_member.role,
} : {
...user,
server_member: false,
admin: false,
role: collaborator.role,
}
const user_final = server_member
? {
...user,
server_member: true,
admin: server_member.admin,
role: server_member.role,
}
: {
...user,
server_member: false,
admin: false,
role: collaborator.role,
}

return {
sites: sites || [],
user: user_final
user: user_final,
}
})

// TODO: do this w/ sql
const user_sites = sites?.filter(site =>
/*user is server member*/ user.server_member ||
/*user is site collaborator*/ site.collaborators.some(collaborator => collaborator.user === user.id)
const user_sites = sites?.filter(
(site) =>
/*user is server member*/ user.server_member ||
/*user is site collaborator*/ site.collaborators.some(
(collaborator) => collaborator.user === user.id
)
)

return {
supabase,
session,
user,
sites: user_sites
sites: user_sites,
}
}
}
}
10 changes: 5 additions & 5 deletions src/routes/[site]/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@
storage_subscribe(async ({ bucket, action, key, file, options }) => {
if (action === 'upload') {
const { data } = await supabase.storage
.from(bucket)
.upload(key, file, options)
const { data: res } = supabase.storage.from('images').getPublicUrl(key)
return res.publicUrl
await supabase.storage.from(bucket).upload(key, file, options)
if (bucket === 'images') {
const { data: res } = supabase.storage.from(bucket).getPublicUrl(key)
return res.publicUrl
}
}
})
</script>
Expand Down

0 comments on commit 419f30f

Please sign in to comment.