Skip to content

High performance self-hosted photo and video management solution.

4.0 KB
Raw Blame
<script lang="ts">
  import { goto } from '$app/navigation';
  import UserPageLayout from '#lib/components/layouts/UserPageLayout.svelte';
  import MapTimelinePanel from './MapTimelinePanel.svelte';
  import type { SelectionBBox } from '#lib/components/shared-components/map/types.ts';
  import { timeToLoadTheMap } from '#lib/constants.ts';
  import Portal from '#lib/elements/Portal.svelte';
  import { assetViewerManager } from '#lib/managers/asset-viewer-manager.svelte.ts';
  import { Route } from '#lib/route.ts';
  import { handlePromiseError } from '#lib/utils.ts';
  import { delay } from '#lib/utils/asset-utils.ts';
  import { navigate } from '#lib/utils/navigation.ts';
  import { mapSettings } from '#lib/stores/preferences.store.ts';
  import { LoadingSpinner } from '@immich/ui';
  import { onDestroy } from 'svelte';
  import type { PageData } from './$types';
  import { createPreloadedQuery } from '#lib/queries/client.ts';
  import { getUserConfigQuery } from '#lib/queries/config-user.query.ts';

  const query = createPreloadedQuery(getUserConfigQuery);
  const config = $derived(query.data);

  interface Props {
    data: PageData;
  }

  let { data }: Props = $props();
  let selectedClusterIds = $state.raw(new Set<string>());
  let selectedClusterBBox = $state.raw<SelectionBBox>();
  let isTimelinePanelVisible = $state(false);

  function closeTimelinePanel() {
    isTimelinePanelVisible = false;
    selectedClusterBBox = undefined;
    selectedClusterIds = new Set();
  }

  onDestroy(() => {
    assetViewerManager.showAssetViewer(false);
  });

  $effect(() => {
    if (!config.map.enabled) {
      handlePromiseError(goto(Route.photos()));
    }
  });

  async function onViewAssets(assetIds: string[]) {
    await assetViewerManager.setAssetId(assetIds[0]);
    closeTimelinePanel();
  }

  function onClusterSelect(assetIds: string[], bbox: SelectionBBox) {
    selectedClusterIds = new Set(assetIds);
    selectedClusterBBox = bbox;
    isTimelinePanelVisible = true;
    assetViewerManager.showAssetViewer(false);
    handlePromiseError(navigate({ targetRoute: 'current', assetId: null }));
  }
</script>

{#if config.map.enabled}
  <UserPageLayout title={data.meta.title}>
    <div class="isolate flex size-full flex-col sm:flex-row">
      <div
        class={[
          'min-h-0',
          isTimelinePanelVisible ? 'h-1/2 w-full pb-2 sm:h-full sm:w-2/3 sm:pe-2 sm:pb-0' : 'size-full',
        ]}
      >
        {#await import('#lib/components/shared-components/map/Map.svelte')}
          {#await delay(timeToLoadTheMap) then}
            <!-- show the loading spinner only if loading the map takes too much time -->
            <div class="flex size-full items-center justify-center">
              <LoadingSpinner />
            </div>
          {/await}
        {:then { default: Map }}
          <Map
            hash
            onSelect={onViewAssets}
            {onClusterSelect}
            onViewportClose={closeTimelinePanel}
            viewportGridActive={isTimelinePanelVisible}
            autoOpenPanel={$mapSettings.showAssetPanel}
          />
        {/await}
      </div>

      {#if isTimelinePanelVisible && selectedClusterBBox}
        <div class="h-1/2 min-h-0 w-full pt-2 sm:h-full sm:w-1/3 sm:ps-2 sm:pt-0">
          <MapTimelinePanel
            bbox={selectedClusterBBox}
            {selectedClusterIds}
            assetCount={selectedClusterIds.size}
            onClose={closeTimelinePanel}
          />
        </div>
      {/if}
    </div>
  </UserPageLayout>
  <Portal target="body">
    {#if assetViewerManager.isViewing && !isTimelinePanelVisible}
      {#await import('#lib/components/asset-viewer/AssetViewer.svelte') then { default: AssetViewer }}
        <AssetViewer
          cursor={{ current: assetViewerManager.asset! }}
          showNavigation={false}
          onClose={() => {
            assetViewerManager.showAssetViewer(false);
            handlePromiseError(navigate({ targetRoute: 'current', assetId: null }));
          }}
          isShared={false}
        />
      {/await}
    {/if}
  </Portal>
{/if}