mirror of
https://github.com/immich-app/immich.git
synced 2025-12-22 09:15:34 +03:00
refactor(web): tree data structure for folder and tag views (#18980)
* refactor folder view inline link * improved tree collapsing * handle tags * linting * formatting * simplify * .from is faster * simplify * add key
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
import { handleError } from '$lib/utils/handle-error';
|
||||
import { getMetadataSearchQuery } from '$lib/utils/metadata-search';
|
||||
import { fromISODateTime, fromISODateTimeUTC } from '$lib/utils/timeline-util';
|
||||
import { getParentPath } from '$lib/utils/tree-utils';
|
||||
import {
|
||||
AssetMediaSize,
|
||||
getAssetInfo,
|
||||
@@ -137,7 +138,7 @@
|
||||
const getAssetFolderHref = (asset: AssetResponseDto) => {
|
||||
const folderUrl = new URL(AppRoute.FOLDERS, globalThis.location.href);
|
||||
// Remove the last part of the path to get the parent path
|
||||
const assetParentPath = asset.originalPath.split('/').slice(0, -1).join('/');
|
||||
const assetParentPath = getParentPath(asset.originalPath);
|
||||
folderUrl.searchParams.set(QueryParameter.PATH, assetParentPath);
|
||||
return folderUrl.href;
|
||||
};
|
||||
|
||||
@@ -1,23 +1,27 @@
|
||||
<script lang="ts">
|
||||
import Icon from '$lib/components/elements/icon.svelte';
|
||||
import { TreeNode } from '$lib/utils/tree-utils';
|
||||
import { IconButton } from '@immich/ui';
|
||||
import { mdiArrowUpLeft, mdiChevronRight } from '@mdi/js';
|
||||
import { t } from 'svelte-i18n';
|
||||
|
||||
interface Props {
|
||||
pathSegments?: string[];
|
||||
node: TreeNode;
|
||||
getLink: (path: string) => string;
|
||||
title: string;
|
||||
icon: string;
|
||||
}
|
||||
|
||||
let { pathSegments = [], getLink, title, icon }: Props = $props();
|
||||
const { node, getLink, title, icon }: Props = $props();
|
||||
|
||||
let isRoot = $derived(pathSegments.length === 0);
|
||||
const rootLink = getLink('');
|
||||
const isRoot = $derived(node.parent === null);
|
||||
const parentLink = $derived(getLink(node.parent ? node.parent.path : ''));
|
||||
const parents = $derived(node.parents);
|
||||
</script>
|
||||
|
||||
<nav class="flex items-center py-2">
|
||||
{#if !isRoot}
|
||||
{#if parentLink}
|
||||
<div>
|
||||
<IconButton
|
||||
shape="round"
|
||||
@@ -25,9 +29,8 @@
|
||||
variant="ghost"
|
||||
icon={mdiArrowUpLeft}
|
||||
aria-label={$t('to_parent')}
|
||||
href={getLink(pathSegments.slice(0, -1).join('/'))}
|
||||
href={parentLink}
|
||||
class="me-2"
|
||||
onclick={() => {}}
|
||||
/>
|
||||
</div>
|
||||
{/if}
|
||||
@@ -42,31 +45,29 @@
|
||||
color="secondary"
|
||||
variant="ghost"
|
||||
{icon}
|
||||
href={getLink('')}
|
||||
href={rootLink}
|
||||
aria-label={title}
|
||||
size="medium"
|
||||
aria-current={isRoot ? 'page' : undefined}
|
||||
onclick={() => {}}
|
||||
/>
|
||||
</li>
|
||||
{#each pathSegments as segment, index (index)}
|
||||
{@const isLastSegment = index === pathSegments.length - 1}
|
||||
{#each parents as parent (parent)}
|
||||
<li
|
||||
class="flex gap-2 items-center font-mono text-sm text-nowrap text-immich-primary dark:text-immich-dark-primary"
|
||||
>
|
||||
<Icon path={mdiChevronRight} class="text-gray-500 dark:text-gray-300" size={16} ariaHidden />
|
||||
{#if isLastSegment}
|
||||
<p class="cursor-default whitespace-pre-wrap">{segment}</p>
|
||||
{:else}
|
||||
<a
|
||||
class="underline hover:font-semibold whitespace-pre-wrap"
|
||||
href={getLink(pathSegments.slice(0, index + 1).join('/'))}
|
||||
>
|
||||
{segment}
|
||||
</a>
|
||||
{/if}
|
||||
<a class="underline hover:font-semibold whitespace-pre-wrap" href={getLink(parent.path)}>
|
||||
{parent.value}
|
||||
</a>
|
||||
</li>
|
||||
{/each}
|
||||
|
||||
<li
|
||||
class="flex gap-2 items-center font-mono text-sm text-nowrap text-immich-primary dark:text-immich-dark-primary"
|
||||
>
|
||||
<Icon path={mdiChevronRight} class="text-gray-500 dark:text-gray-300" size={16} ariaHidden />
|
||||
<p class="cursor-default whitespace-pre-wrap">{node.value}</p>
|
||||
</li>
|
||||
</ol>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
@@ -1,29 +1,31 @@
|
||||
<script lang="ts">
|
||||
import Icon from '$lib/components/elements/icon.svelte';
|
||||
import type { TreeNode } from '$lib/utils/tree-utils';
|
||||
|
||||
interface Props {
|
||||
items?: string[];
|
||||
items: TreeNode[];
|
||||
icon: string;
|
||||
onClick: (path: string) => void;
|
||||
}
|
||||
|
||||
let { items = [], icon, onClick }: Props = $props();
|
||||
let { items, icon, onClick }: Props = $props();
|
||||
</script>
|
||||
|
||||
{#if items.length > 0}
|
||||
<div
|
||||
class="w-full grid grid-cols-2 sm:grid-cols-4 lg:grid-cols-6 2xl:grid-cols-8 gap-2 bg-gray-50 dark:bg-immich-dark-gray/50 rounded-2xl border border-gray-100 dark:border-gray-900"
|
||||
>
|
||||
{#each items as item (item)}
|
||||
<!-- eslint-disable-next-line svelte/require-each-key -->
|
||||
{#each items as item}
|
||||
<button
|
||||
class="flex flex-col place-items-center gap-2 py-2 px-4 hover:bg-immich-primary/10 dark:hover:bg-immich-primary/40 rounded-xl"
|
||||
onclick={() => onClick(item)}
|
||||
title={item}
|
||||
onclick={() => onClick(item.value)}
|
||||
title={item.value}
|
||||
type="button"
|
||||
>
|
||||
<Icon path={icon} class="text-immich-primary dark:text-immich-dark-primary" size={64} />
|
||||
<p class="text-sm dark:text-gray-200 text-nowrap text-ellipsis overflow-clip w-full whitespace-pre-wrap">
|
||||
{item}
|
||||
{item.value}
|
||||
</p>
|
||||
</button>
|
||||
{/each}
|
||||
|
||||
@@ -1,28 +1,21 @@
|
||||
<script lang="ts">
|
||||
import Tree from '$lib/components/shared-components/tree/tree.svelte';
|
||||
import { normalizeTreePath, type RecursiveObject } from '$lib/utils/tree-utils';
|
||||
import { type TreeNode } from '$lib/utils/tree-utils';
|
||||
|
||||
interface Props {
|
||||
items: RecursiveObject;
|
||||
parent?: string;
|
||||
active?: string;
|
||||
tree: TreeNode;
|
||||
active: string;
|
||||
icons: { default: string; active: string };
|
||||
getLink: (path: string) => string;
|
||||
getColor?: (path: string) => string | undefined;
|
||||
}
|
||||
|
||||
let { items, parent = '', active = '', icons, getLink, getColor = () => undefined }: Props = $props();
|
||||
let { tree, active, icons, getLink }: Props = $props();
|
||||
</script>
|
||||
|
||||
<ul class="list-none ms-2">
|
||||
<!-- eslint-disable-next-line svelte/require-each-key -->
|
||||
{#each Object.entries(items).sort() as [path, tree]}
|
||||
{@const value = normalizeTreePath(`${parent}/${path}`)}
|
||||
{@const key = value + getColor(value)}
|
||||
{#key key}
|
||||
<li>
|
||||
<Tree {parent} value={path} {tree} {icons} {active} {getLink} {getColor} />
|
||||
</li>
|
||||
{/key}
|
||||
{#each tree.children as node (node.color ? node.path + node.color : node.path)}
|
||||
<li>
|
||||
<Tree {node} {icons} {active} {getLink} />
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
|
||||
@@ -1,25 +1,20 @@
|
||||
<script lang="ts">
|
||||
import Icon from '$lib/components/elements/icon.svelte';
|
||||
import TreeItems from '$lib/components/shared-components/tree/tree-items.svelte';
|
||||
import { normalizeTreePath, type RecursiveObject } from '$lib/utils/tree-utils';
|
||||
import { TreeNode } from '$lib/utils/tree-utils';
|
||||
import { mdiChevronDown, mdiChevronRight } from '@mdi/js';
|
||||
|
||||
interface Props {
|
||||
tree: RecursiveObject;
|
||||
parent: string;
|
||||
value: string;
|
||||
active?: string;
|
||||
node: TreeNode;
|
||||
active: string;
|
||||
icons: { default: string; active: string };
|
||||
getLink: (path: string) => string;
|
||||
getColor: (path: string) => string | undefined;
|
||||
}
|
||||
|
||||
let { tree, parent, value, active = '', icons, getLink, getColor }: Props = $props();
|
||||
let { node, active, icons, getLink }: Props = $props();
|
||||
|
||||
const path = $derived(normalizeTreePath(`${parent}/${value}`));
|
||||
const isActive = $derived(active === path || active.startsWith(`${path}/`));
|
||||
const isTarget = $derived(active === path);
|
||||
const color = $derived(getColor(path));
|
||||
const isTarget = $derived(active === node.path);
|
||||
const isActive = $derived(active === node.path || active.startsWith(node.value === '/' ? '/' : `${node.path}/`));
|
||||
let isOpen = $derived(isActive);
|
||||
|
||||
const onclick = (event: MouseEvent) => {
|
||||
@@ -29,25 +24,27 @@
|
||||
</script>
|
||||
|
||||
<a
|
||||
href={getLink(path)}
|
||||
title={value}
|
||||
href={getLink(node.path)}
|
||||
title={node.value}
|
||||
class={`flex grow place-items-center ps-2 py-1 text-sm rounded-lg hover:bg-slate-200 dark:hover:bg-slate-800 hover:font-semibold ${isTarget ? 'bg-slate-100 dark:bg-slate-700 font-semibold text-immich-primary dark:text-immich-dark-primary' : 'dark:text-gray-200'}`}
|
||||
data-sveltekit-keepfocus
|
||||
>
|
||||
<button type="button" {onclick} class={Object.values(tree).length === 0 ? 'invisible' : ''}>
|
||||
<Icon path={isOpen ? mdiChevronDown : mdiChevronRight} class="text-gray-400" size={20} />
|
||||
</button>
|
||||
<div>
|
||||
{#if node.size > 0}
|
||||
<button type="button" {onclick}>
|
||||
<Icon path={isOpen ? mdiChevronDown : mdiChevronRight} class="text-gray-400" size={20} />
|
||||
</button>
|
||||
{/if}
|
||||
<div class={node.size === 0 ? 'ml-[1.5em] ' : ''}>
|
||||
<Icon
|
||||
path={isActive ? icons.active : icons.default}
|
||||
class={isActive ? 'text-immich-primary dark:text-immich-dark-primary' : 'text-gray-400'}
|
||||
{color}
|
||||
color={node.color}
|
||||
size={20}
|
||||
/>
|
||||
</div>
|
||||
<span class="text-nowrap overflow-hidden text-ellipsis font-mono ps-1 pt-1 whitespace-pre-wrap">{value}</span>
|
||||
<span class="text-nowrap overflow-hidden text-ellipsis font-mono ps-1 pt-1 whitespace-pre-wrap">{node.value}</span>
|
||||
</a>
|
||||
|
||||
{#if isOpen}
|
||||
<TreeItems parent={path} items={tree} {icons} {active} {getLink} {getColor} />
|
||||
<TreeItems tree={node} {icons} {active} {getLink} />
|
||||
{/if}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { eventManager } from '$lib/managers/event-manager.svelte';
|
||||
import { TreeNode } from '$lib/utils/tree-utils';
|
||||
import {
|
||||
getAssetsByOriginalPath,
|
||||
getUniqueOriginalPaths,
|
||||
@@ -13,47 +14,41 @@ type AssetCache = {
|
||||
};
|
||||
|
||||
class FoldersStore {
|
||||
folders = $state.raw<TreeNode | null>(null);
|
||||
private initialized = false;
|
||||
uniquePaths = $state<string[]>([]);
|
||||
assets = $state<AssetCache>({});
|
||||
private assets = $state<AssetCache>({});
|
||||
|
||||
constructor() {
|
||||
eventManager.on('auth.logout', () => this.clearCache());
|
||||
}
|
||||
|
||||
async fetchUniquePaths() {
|
||||
async fetchTree(): Promise<TreeNode> {
|
||||
if (this.initialized) {
|
||||
return;
|
||||
return this.folders!;
|
||||
}
|
||||
this.initialized = true;
|
||||
|
||||
const uniquePaths = await getUniqueOriginalPaths();
|
||||
this.uniquePaths.push(...uniquePaths);
|
||||
this.folders = TreeNode.fromPaths(await getUniqueOriginalPaths());
|
||||
this.folders.collapse();
|
||||
return this.folders;
|
||||
}
|
||||
|
||||
bustAssetCache() {
|
||||
this.assets = {};
|
||||
}
|
||||
|
||||
async refreshAssetsByPath(path: string | null) {
|
||||
if (!path) {
|
||||
return;
|
||||
}
|
||||
this.assets[path] = await getAssetsByOriginalPath({ path });
|
||||
async refreshAssetsByPath(path: string) {
|
||||
return (this.assets[path] = await getAssetsByOriginalPath({ path }));
|
||||
}
|
||||
|
||||
async fetchAssetsByPath(path: string) {
|
||||
if (this.assets[path]) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.assets[path] = await getAssetsByOriginalPath({ path });
|
||||
return (this.assets[path] ??= await getAssetsByOriginalPath({ path }));
|
||||
}
|
||||
|
||||
clearCache() {
|
||||
this.initialized = false;
|
||||
this.uniquePaths = [];
|
||||
this.assets = {};
|
||||
this.folders = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ export const isAssetViewerRoute = (target?: NavigationTarget | null) =>
|
||||
!!(target?.route.id?.endsWith('/[[assetId=id]]') && 'assetId' in (target?.params || {}));
|
||||
|
||||
export function getAssetInfoFromParam({ assetId, key }: { assetId?: string; key?: string }) {
|
||||
return assetId && getAssetInfo({ id: assetId, key });
|
||||
return assetId ? getAssetInfo({ id: assetId, key }) : undefined;
|
||||
}
|
||||
|
||||
function currentUrlWithoutAsset() {
|
||||
|
||||
@@ -1,21 +1,144 @@
|
||||
export interface RecursiveObject {
|
||||
[key: string]: RecursiveObject;
|
||||
}
|
||||
/* eslint-disable @typescript-eslint/no-this-alias */
|
||||
/* eslint-disable unicorn/no-this-assignment */
|
||||
/* eslint-disable unicorn/prefer-at */
|
||||
import type { TagResponseDto } from '@immich/sdk';
|
||||
|
||||
export const normalizeTreePath = (path: string) => path.replace(/^\//, '').replace(/\/$/, '');
|
||||
export class TreeNode extends Map<string, TreeNode> {
|
||||
value: string;
|
||||
path: string;
|
||||
parent: TreeNode | null;
|
||||
hasAssets: boolean;
|
||||
id: string | undefined;
|
||||
color: string | undefined;
|
||||
private _parents: TreeNode[] | undefined;
|
||||
private _children: TreeNode[] | undefined;
|
||||
|
||||
export function buildTree(paths: string[]) {
|
||||
const root: RecursiveObject = {};
|
||||
private constructor(value: string, path: string, parent: TreeNode | null) {
|
||||
super();
|
||||
this.value = value;
|
||||
this.parent = parent;
|
||||
this.path = path;
|
||||
this.hasAssets = false;
|
||||
}
|
||||
|
||||
for (const path of paths) {
|
||||
const parts = path.split('/');
|
||||
let current = root;
|
||||
static fromPaths(paths: string[]) {
|
||||
const root = new TreeNode('', '', null);
|
||||
for (const path of paths) {
|
||||
const current = root.add(path);
|
||||
current.hasAssets = true;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
static fromTags(tags: TagResponseDto[]) {
|
||||
const root = new TreeNode('', '', null);
|
||||
for (const tag of tags) {
|
||||
const current = root.add(tag.value);
|
||||
current.hasAssets = true;
|
||||
current.id = tag.id;
|
||||
current.color = tag.color;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
traverse(path: string) {
|
||||
const parts = getPathParts(path);
|
||||
let current: TreeNode = this;
|
||||
let curPart = null;
|
||||
for (const part of parts) {
|
||||
if (!current[part]) {
|
||||
current[part] = {};
|
||||
// segments common to all subtrees can be collapsed together
|
||||
curPart = curPart === null ? part : joinPaths(curPart, part);
|
||||
const next = current.get(curPart);
|
||||
if (next) {
|
||||
current = next;
|
||||
curPart = null;
|
||||
}
|
||||
current = current[part];
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
collapse() {
|
||||
if (this.size === 1 && !this.hasAssets && this.parent !== null) {
|
||||
const child = this.values().next().value!;
|
||||
child.value = joinPaths(this.value, child.value);
|
||||
child.parent = this.parent;
|
||||
this.parent.delete(this.value);
|
||||
this.parent.set(child.value, child);
|
||||
}
|
||||
|
||||
for (const child of this.values()) {
|
||||
child.collapse();
|
||||
}
|
||||
}
|
||||
return root;
|
||||
|
||||
private add(path: string) {
|
||||
let current: TreeNode = this;
|
||||
for (const part of getPathParts(path)) {
|
||||
let next = current.get(part);
|
||||
if (next === undefined) {
|
||||
next = new TreeNode(part, joinPaths(current.path, part), current);
|
||||
current.set(part, next);
|
||||
}
|
||||
current = next;
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
get parents(): TreeNode[] {
|
||||
if (this._parents) {
|
||||
return this._parents;
|
||||
}
|
||||
const parents: TreeNode[] = [];
|
||||
let current: TreeNode | null = this.parent;
|
||||
while (current !== null && current.parent !== null) {
|
||||
parents.push(current);
|
||||
current = current.parent;
|
||||
}
|
||||
return (this._parents = parents.reverse());
|
||||
}
|
||||
|
||||
get children(): TreeNode[] {
|
||||
return (this._children ??= Array.from(this.values()));
|
||||
}
|
||||
}
|
||||
|
||||
export const normalizeTreePath = (path: string) =>
|
||||
path.length > 1 && path[path.length - 1] === '/' ? path.slice(0, -1) : path;
|
||||
|
||||
export function getPathParts(path: string) {
|
||||
const parts = path.split('/');
|
||||
if (path[0] === '/') {
|
||||
parts[0] = '/';
|
||||
}
|
||||
|
||||
if (path[path.length - 1] === '/') {
|
||||
parts.pop();
|
||||
}
|
||||
|
||||
return parts;
|
||||
}
|
||||
|
||||
export function joinPaths(path1: string, path2: string) {
|
||||
if (!path1) {
|
||||
return path2;
|
||||
}
|
||||
|
||||
if (!path2) {
|
||||
return path1;
|
||||
}
|
||||
|
||||
if (path1[path1.length - 1] === '/') {
|
||||
return path1 + path2;
|
||||
}
|
||||
|
||||
return path1 + '/' + path2;
|
||||
}
|
||||
|
||||
export function getParentPath(path: string) {
|
||||
const normalized = normalizeTreePath(path);
|
||||
const last = normalized.lastIndexOf('/');
|
||||
if (last > 0) {
|
||||
return normalized.slice(0, last);
|
||||
}
|
||||
return last === 0 ? '/' : normalized;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user