This allows to configure which view should be the default ("start view") in the files app, currently either "all files" or "personal files". But it might be extended to the new home view in the future. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>pull/53798/head
parent
927beefae2
commit
275c4404d4
@ -0,0 +1,75 @@ |
||||
/** |
||||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
import { beforeEach, describe, expect, test } from 'vitest' |
||||
import { defaultView, hasPersonalFilesView } from './filesViews.ts' |
||||
|
||||
describe('hasPersonalFilesView', () => { |
||||
beforeEach(() => removeInitialState()) |
||||
|
||||
test('enabled if user has unlimited quota', () => { |
||||
mockInitialState('files', 'storageStats', { quota: -1 }) |
||||
expect(hasPersonalFilesView()).toBe(true) |
||||
}) |
||||
|
||||
test('enabled if user has limited quota', () => { |
||||
mockInitialState('files', 'storageStats', { quota: 1234 }) |
||||
expect(hasPersonalFilesView()).toBe(true) |
||||
}) |
||||
|
||||
test('disabled if user has no quota', () => { |
||||
mockInitialState('files', 'storageStats', { quota: 0 }) |
||||
expect(hasPersonalFilesView()).toBe(false) |
||||
}) |
||||
}) |
||||
|
||||
describe('defaultView', () => { |
||||
beforeEach(() => { |
||||
document.querySelectorAll('input[type="hidden"]').forEach((el) => { |
||||
el.remove() |
||||
}) |
||||
}) |
||||
|
||||
test('Returns files view if set', () => { |
||||
mockInitialState('files', 'config', { default_view: 'files' }) |
||||
expect(defaultView()).toBe('files') |
||||
}) |
||||
|
||||
test('Returns personal view if set and enabled', () => { |
||||
mockInitialState('files', 'config', { default_view: 'personal' }) |
||||
mockInitialState('files', 'storageStats', { quota: -1 }) |
||||
expect(defaultView()).toBe('personal') |
||||
}) |
||||
|
||||
test('Falls back to files if personal view is disabled', () => { |
||||
mockInitialState('files', 'config', { default_view: 'personal' }) |
||||
mockInitialState('files', 'storageStats', { quota: 0 }) |
||||
expect(defaultView()).toBe('files') |
||||
}) |
||||
}) |
||||
|
||||
/** |
||||
* Remove the mocked initial state |
||||
*/ |
||||
function removeInitialState(): void { |
||||
document.querySelectorAll('input[type="hidden"]').forEach((el) => { |
||||
el.remove() |
||||
}) |
||||
} |
||||
|
||||
/** |
||||
* Helper to mock an initial state value |
||||
* @param app - The app |
||||
* @param key - The key |
||||
* @param value - The value |
||||
*/ |
||||
function mockInitialState(app: string, key: string, value: unknown): void { |
||||
const el = document.createElement('input') |
||||
el.value = btoa(JSON.stringify(value)) |
||||
el.id = `initial-state-${app}-${key}` |
||||
el.type = 'hidden' |
||||
|
||||
document.head.appendChild(el) |
||||
} |
@ -0,0 +1,30 @@ |
||||
/** |
||||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
||||
* SPDX-License-Identifier: AGPL-3.0-or-later |
||||
*/ |
||||
|
||||
import type { UserConfig } from '../types.ts' |
||||
|
||||
import { loadState } from '@nextcloud/initial-state' |
||||
|
||||
/** |
||||
* Check whether the personal files view can be shown |
||||
*/ |
||||
export function hasPersonalFilesView(): boolean { |
||||
const storageStats = loadState('files', 'storageStats', { quota: -1 }) |
||||
// Don't show this view if the user has no storage quota
|
||||
return storageStats.quota !== 0 |
||||
} |
||||
|
||||
/** |
||||
* Get the default files view |
||||
*/ |
||||
export function defaultView() { |
||||
const { default_view: defaultView } = loadState<Partial<UserConfig>>('files', 'config', { default_view: 'files' }) |
||||
|
||||
// the default view - only use the personal one if it is enabled
|
||||
if (defaultView !== 'personal' || hasPersonalFilesView()) { |
||||
return defaultView |
||||
} |
||||
return 'files' |
||||
} |
Loading…
Reference in new issue