2026-06-17 17:06:30 -06:00
|
|
|
<script lang="ts">
|
2026-06-17 22:30:56 -06:00
|
|
|
import { errorMessage } from '$lib/utils/errors';
|
2026-06-17 22:07:18 -06:00
|
|
|
import { goto } from '$app/navigation';
|
2026-06-17 17:06:30 -06:00
|
|
|
import { onMount } from 'svelte';
|
2026-06-20 02:27:24 +02:00
|
|
|
import { dateBucket, resolveOwnerName, typeLabel } from '$lib/api/endpoints/favorites';
|
2026-06-17 17:06:30 -06:00
|
|
|
import { fetchSharedWithMe, type IncomingGrantItem } from '$lib/api/endpoints/grants';
|
2026-06-17 22:07:18 -06:00
|
|
|
import type { FileItem } from '$lib/api/types';
|
2026-06-19 22:56:00 +00:00
|
|
|
import { lazyComponent } from '$lib/composables/lazyComponent.svelte';
|
2026-06-20 02:27:24 +02:00
|
|
|
import { useOwnerCache } from '$lib/composables/useOwnerCache.svelte';
|
|
|
|
|
import ResourceList, {
|
|
|
|
|
type GroupByDef,
|
|
|
|
|
type ResourceEntry
|
|
|
|
|
} from '$lib/components/ResourceList.svelte';
|
2026-06-17 17:06:30 -06:00
|
|
|
import { t } from '$lib/i18n/index.svelte';
|
|
|
|
|
|
2026-06-17 22:07:18 -06:00
|
|
|
let raw = $state<IncomingGrantItem[]>([]);
|
2026-06-17 17:06:30 -06:00
|
|
|
let cursor = $state<string | undefined>(undefined);
|
|
|
|
|
let loading = $state(false);
|
|
|
|
|
let error = $state<string | null>(null);
|
2026-06-20 02:27:24 +02:00
|
|
|
let groupBy = $state<string>('');
|
|
|
|
|
let reversed = $state(false);
|
|
|
|
|
|
|
|
|
|
const sharers = useOwnerCache(resolveOwnerName);
|
2026-06-17 17:06:30 -06:00
|
|
|
|
2026-06-17 22:07:18 -06:00
|
|
|
const byId = $derived(new Map(raw.map((it) => [it.resource.id, it])));
|
|
|
|
|
|
|
|
|
|
const entries = $derived(
|
2026-06-20 02:27:24 +02:00
|
|
|
raw.map((it): ResourceEntry => {
|
|
|
|
|
const isFile = it.resource_type === 'file';
|
|
|
|
|
return {
|
2026-06-17 22:07:18 -06:00
|
|
|
id: it.resource.id,
|
|
|
|
|
name: it.resource.name,
|
|
|
|
|
kind: it.resource_type,
|
|
|
|
|
iconClass: it.resource.icon_class,
|
2026-06-20 02:13:23 +02:00
|
|
|
// The sharer becomes the "owner" surface — ResourceList renders
|
|
|
|
|
// `<UserVignette userId>` (avatar / name / external badge),
|
|
|
|
|
// resolved lazily via `/api/users/{id}`. `path` keeps the
|
|
|
|
|
// resource's real location so the row still shows where it
|
|
|
|
|
// lives, not a translated string.
|
|
|
|
|
ownerId: it.granted_by ?? null,
|
2026-06-20 02:27:24 +02:00
|
|
|
ownerName: sharers.name(it.granted_by),
|
2026-06-20 02:13:23 +02:00
|
|
|
path: it.resource.path,
|
2026-06-20 02:27:24 +02:00
|
|
|
size: isFile ? (it.resource as FileItem).size : null,
|
|
|
|
|
date: it.granted_at,
|
|
|
|
|
category: isFile ? it.resource.category : 'Folder'
|
|
|
|
|
};
|
|
|
|
|
})
|
2026-06-17 22:07:18 -06:00
|
|
|
);
|
|
|
|
|
|
2026-06-20 02:27:24 +02:00
|
|
|
// Server-supported sort_by values (see grant_handler.rs:615):
|
|
|
|
|
// granted_at, granted_by, name, type
|
|
|
|
|
// The first entry (no `bucketOf`) renders a flat list sorted by name —
|
|
|
|
|
// the A-Z icon flags it as "sort, not group" so users don't read it as
|
|
|
|
|
// a real bucket dimension. The remaining three are honest groupings and
|
|
|
|
|
// get the default layer-group icon.
|
|
|
|
|
const groupBys: GroupByDef[] = [
|
|
|
|
|
{ key: '', label: t('files.name', 'Name'), orderBy: 'name', icon: 'arrow-up-a-z' },
|
|
|
|
|
{
|
|
|
|
|
key: 'sharedBy',
|
|
|
|
|
label: t('groupby.sharedBy', 'Shared by'),
|
|
|
|
|
orderBy: 'granted_by',
|
|
|
|
|
bucketOf: (e) => e.ownerId ?? null,
|
|
|
|
|
labelOf: (id) => sharers.label(id)
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: 'type',
|
|
|
|
|
label: t('groupby.type', 'Type'),
|
|
|
|
|
orderBy: 'type',
|
|
|
|
|
bucketOf: (e) => e.category ?? 'other',
|
|
|
|
|
labelOf: (k) => typeLabel(k)
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
key: 'sharedAt',
|
|
|
|
|
label: t('groupby.sharedAt', 'Shared date'),
|
|
|
|
|
orderBy: 'granted_at',
|
|
|
|
|
bucketOf: (e) => dateBucket(e.date)
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
function orderByForGroup(): string {
|
|
|
|
|
return groupBys.find((g) => g.key === groupBy)?.orderBy ?? 'granted_at';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function load(reset = false, orderBy = 'granted_at', rev = reversed) {
|
2026-06-17 17:06:30 -06:00
|
|
|
loading = true;
|
|
|
|
|
error = null;
|
|
|
|
|
try {
|
2026-06-20 02:27:24 +02:00
|
|
|
const page = await fetchSharedWithMe({
|
|
|
|
|
cursor: reset ? undefined : cursor,
|
|
|
|
|
orderBy,
|
|
|
|
|
reverse: rev
|
|
|
|
|
});
|
2026-06-17 22:07:18 -06:00
|
|
|
raw = reset ? page.items : [...raw, ...page.items];
|
2026-06-17 17:06:30 -06:00
|
|
|
cursor = page.next_cursor;
|
2026-06-20 02:27:24 +02:00
|
|
|
// Warm the sharer-name cache so the "Shared by" group headers
|
|
|
|
|
// show real names instead of UUIDs.
|
|
|
|
|
void sharers.resolve(page.items.map((i) => i.granted_by).filter((id): id is string => !!id));
|
2026-06-17 17:06:30 -06:00
|
|
|
} catch (e) {
|
2026-06-17 22:30:56 -06:00
|
|
|
error = errorMessage(e);
|
2026-06-17 17:06:30 -06:00
|
|
|
} finally {
|
|
|
|
|
loading = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-17 22:07:18 -06:00
|
|
|
let viewerOpen = $state(false);
|
|
|
|
|
let viewerFile = $state<FileItem | null>(null);
|
|
|
|
|
|
2026-06-19 22:56:00 +00:00
|
|
|
// The file preview is loaded the first time a file is opened, keeping its
|
|
|
|
|
// module out of this route's initial chunk.
|
|
|
|
|
const fileViewer = lazyComponent(() => import('$lib/components/FileViewer.svelte'));
|
|
|
|
|
$effect(() => {
|
|
|
|
|
if (viewerOpen) void fileViewer.load();
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-17 22:07:18 -06:00
|
|
|
function open(entry: ResourceEntry) {
|
|
|
|
|
if (entry.kind === 'folder') {
|
|
|
|
|
goto(`/files/${entry.id}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const item = byId.get(entry.id);
|
|
|
|
|
if (item) {
|
|
|
|
|
viewerFile = item.resource as FileItem;
|
|
|
|
|
viewerOpen = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-17 17:06:30 -06:00
|
|
|
onMount(() => load(true));
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<svelte:head><title>{t('nav.shared_with_me', 'Shared with me')} · OxiCloud</title></svelte:head>
|
|
|
|
|
|
2026-06-17 22:07:18 -06:00
|
|
|
<ResourceList
|
|
|
|
|
title={t('nav.shared_with_me', 'Shared with me')}
|
|
|
|
|
items={entries}
|
2026-06-17 17:06:30 -06:00
|
|
|
{loading}
|
|
|
|
|
{error}
|
|
|
|
|
emptyText={t('shared_with_me.empty', 'Nothing has been shared with you yet.')}
|
|
|
|
|
hasMore={!!cursor}
|
2026-06-20 02:13:23 +02:00
|
|
|
showOwner={true}
|
2026-06-20 02:27:24 +02:00
|
|
|
{groupBys}
|
|
|
|
|
bind:groupBy
|
|
|
|
|
bind:reversed
|
|
|
|
|
onloadmore={() => load(false, orderByForGroup())}
|
2026-06-17 22:07:18 -06:00
|
|
|
onopen={open}
|
2026-06-20 02:27:24 +02:00
|
|
|
onreload={(orderBy, rev) => {
|
|
|
|
|
cursor = undefined;
|
|
|
|
|
load(true, orderBy, rev);
|
|
|
|
|
}}
|
2026-06-17 22:07:18 -06:00
|
|
|
/>
|
|
|
|
|
|
2026-06-19 22:56:00 +00:00
|
|
|
{#if fileViewer.component}
|
|
|
|
|
{@const FileViewer = fileViewer.component}
|
|
|
|
|
<FileViewer bind:open={viewerOpen} file={viewerFile} />
|
|
|
|
|
{/if}
|