Skip to content

πŸ“š The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

2.9 KB
Raw Blame
<template>
  <div class="view-selection">
    <button v-for="option in viewOptions" :key="option.value" :class="{ active: view === option.value }" @click="selectView(option.value)">
      <Icon :name="option.icon" />
      <p class="hint-tooltip">{{ option.label }}</p>
    </button>
  </div>
</template>

<script setup lang="ts">
import localForage from 'localforage';
export type ViewMode = 'kanban' | 'list' | 'table' | 'advanced';

const props = withDefaults(
  defineProps<{
    showKanban?: boolean;
  }>(),
  {
    showKanban: false,
  },
);

const { t } = useI18nT();
const preferences = usePreferencesStore();
const view = defineModel<ViewMode>();

const viewOptions = computed(() => {
  const options = [
    { icon: 'table', label: t('components.viewSelection.list'), value: 'list' as ViewMode },
    { icon: 'list', label: t('components.viewSelection.table'), value: 'table' as ViewMode },
  ];
  if (preferences.get('advancedView').value) {
    options.push({ icon: 'grid', label: 'Advanced', value: 'advanced' as ViewMode });
  }
  if (props.showKanban) {
    options.push({ icon: 'kanban', label: t('components.viewSelection.kanban'), value: 'kanban' as ViewMode });
  }
  return options;
});

// Keep the user's saved choice even when this container cannot display it.
const preferredView = ref<ViewMode>('table');
let userSelectedView = false;
const effectiveView = computed<ViewMode>(() =>
  viewOptions.value.some(option => option.value === preferredView.value) ? preferredView.value : 'table',
);

watch(effectiveView, newView => (view.value = newView), { immediate: true });

function selectView(newView: ViewMode) {
  userSelectedView = true;
  preferredView.value = newView;
  localForage.setItem('viewSelection', newView);
}

onMounted(async () => {
  const storedView = await localForage.getItem<unknown>('viewSelection');
  const knownViews: ViewMode[] = ['table', 'list', 'advanced', 'kanban'];
  if (!userSelectedView && typeof storedView === 'string' && knownViews.includes(storedView as ViewMode)) {
    preferredView.value = storedView as ViewMode;
  }
});
</script>

<style scoped lang="scss">
.view-selection {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  border-radius: var(--radius-sm);
  background: var(--surface-raised);
}

button {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  background: transparent;
  cursor: pointer;
  transition:
    color $transition-fast,
    background $transition-fast;

  &:hover {
    color: var(--text-body);
    background: var(--surface-base);

    .hint-tooltip {
      opacity: 1;
      visibility: visible;
    }
  }

  &.active > svg {
    color: var(--primary) !important;
  }

  :deep(svg) {
    width: 18px;
    height: 18px;
    fill: currentcolor;
  }
}
</style>