The minimalistic document archiving platform.
fix(mobile): format select property names correctly (#1485)
Corentin Thomasset588a1a1c12c8
diff --git a/.changeset/cyan-candles-pay.md b/.changeset/cyan-candles-pay.mdnew file mode 100644index 00000000..ba7f4cac--- /dev/null+++ b/.changeset/cyan-candles-pay.md@@ -0,0 +1,5 @@+---+'@papra/mobile': patch+---++Fixed select and multi-select custom properties to display option names correctly (previously displayed as JSON).diff --git a/apps/mobile/src/modules/documents/documents.models.test.ts b/apps/mobile/src/modules/documents/documents.models.test.tsnew file mode 100644index 00000000..b9b2a4bf--- /dev/null+++ b/apps/mobile/src/modules/documents/documents.models.test.ts@@ -0,0 +1,48 @@+import { describe, expect, test } from 'vitest';+import { formatCustomPropertyValue } from './documents.models';++describe('formatCustomPropertyValue', () => {+ test.each([+ {+ type: 'select',+ value: { optionId: 'cpso_1', name: 'Invoice' },+ expected: 'Invoice',+ },+ {+ type: 'multi_select',+ value: [+ { optionId: 'cpso_1', name: 'Invoice' },+ { optionId: 'cpso_2', name: 'Paid' },+ ],+ expected: 'Invoice, Paid',+ },+ {+ type: 'multi_select',+ value: [{ optionId: 'cpso_1', name: 'Invoice' }],+ expected: 'Invoice',+ },+ { type: 'select', value: null, expected: '—' },+ { type: 'select', value: undefined, expected: '—' },+ { type: 'select', value: {}, expected: '—' },+ { type: 'multi_select', value: [], expected: '—' },+ { type: 'multi_select', value: null, expected: '—' },+ { type: 'multi_select', value: [null, {}], expected: '—' },+ { type: 'text', value: 'Notes', expected: 'Notes' },+ { type: 'text', value: '', expected: '—' },+ { type: 'number', value: 0, expected: '0' },+ { type: 'boolean', value: true, expected: 'Yes' },+ { type: 'boolean', value: false, expected: 'No' },+ { type: 'date', value: '2025-01-15T12:00:00', expected: 'Jan 15, 2025' },+ { type: 'date', value: 'invalid', expected: 'invalid' },+ ])('formats $type value $value as $expected', ({ type, value, expected }) => {+ expect(+ formatCustomPropertyValue({+ key: 'property',+ name: 'Property',+ type,+ displayOrder: 0,+ value,+ }),+ ).toBe(expected);+ });+});diff --git a/apps/mobile/src/modules/documents/documents.models.ts b/apps/mobile/src/modules/documents/documents.models.tsnew file mode 100644index 00000000..74cdcae2--- /dev/null+++ b/apps/mobile/src/modules/documents/documents.models.ts@@ -0,0 +1,64 @@+import type { DocumentCustomProperty } from './documents.types';++export function formatDate(date: Date): string {+ return new Intl.DateTimeFormat('en-US', {+ month: 'short',+ day: 'numeric',+ year: 'numeric',+ }).format(date);+}++function formatSelectOption(value: unknown): string {+ if (+ typeof value === 'object' &&+ value !== null &&+ 'name' in value &&+ typeof value.name === 'string'+ ) {+ return value.name;+ }++ return '';+}++export function formatCustomPropertyValue({ type, value }: DocumentCustomProperty): string {+ if (value == null || value === '' || (Array.isArray(value) && value.length === 0)) {+ return '—';+ }++ if (type === 'select' && typeof value === 'object') {+ return formatSelectOption(value) || '—';+ }++ if (type === 'multi_select' && Array.isArray(value)) {+ return value.map(formatSelectOption).filter(Boolean).join(', ') || '—';+ }++ if (typeof value === 'boolean') {+ return value ? 'Yes' : 'No';+ }++ if (Array.isArray(value)) {+ return value+ .filter((item) => typeof item === 'string' || typeof item === 'number')+ .map(String)+ .join(', ');+ }++ if (typeof value === 'number') {+ return String(value);+ }++ if (typeof value === 'string') {+ if (type === 'date') {+ const date = new Date(value);+ if (!Number.isNaN(date.getTime())) {+ return formatDate(date);+ }+ }++ return value;+ }++ return JSON.stringify(value);+}diff --git a/apps/mobile/src/modules/documents/screens/document-details.screen.tsx b/apps/mobile/src/modules/documents/screens/document-details.screen.tsxindex 9ea7df8e..d2d89a69 100644--- a/apps/mobile/src/modules/documents/screens/document-details.screen.tsx+++ b/apps/mobile/src/modules/documents/screens/document-details.screen.tsx@@ -1,4 +1,3 @@-import type { DocumentCustomProperty } from '@/modules/documents/documents.types';import type { IconName } from '@/modules/ui/components/icon';import type { ThemeColors } from '@/modules/ui/theme.constants';import { formatBytes } from '@corentinth/chisels';@@ -20,6 +19,7 @@ import * as Sharing from 'expo-sharing';import { useAppTranslations } from '@/modules/i18n/hooks/use-app-translations';import { useApiClient, useAuthClient } from '@/modules/api/providers/api.provider';import { DocumentActionSheet } from '@/modules/documents/components/document-action-sheet';+import { formatCustomPropertyValue, formatDate } from '@/modules/documents/documents.models';import { fetchDocument, fetchDocumentFile } from '@/modules/documents/documents.services';import { Tag } from '@/modules/tags/components/tag';import { Icon } from '@/modules/ui/components/icon';@@ -32,53 +32,11 @@ const customPropertyTypeIcons: Record<string, IconName> = {'boolean': 'check-square','date': 'calendar','select': 'list',- 'multi-select': 'list',+ 'multi_select': 'list','document-relation': 'file','user-relation': 'user',};-function formatDate(date: Date): string {- return new Intl.DateTimeFormat('en-US', {- month: 'short',- day: 'numeric',- year: 'numeric',- }).format(date);-}--function formatCustomPropertyValue({ type, value }: DocumentCustomProperty): string {- if (value == null || value === '' || (Array.isArray(value) && value.length === 0)) {- return '—';- }-- if (typeof value === 'boolean') {- return value ? 'Yes' : 'No';- }-- if (Array.isArray(value)) {- return value- .filter((item) => typeof item === 'string' || typeof item === 'number')- .map(String)- .join(', ');- }-- if (typeof value === 'number') {- return String(value);- }-- if (typeof value === 'string') {- if (type === 'date') {- const date = new Date(value);- if (!Number.isNaN(date.getTime())) {- return formatDate(date);- }- }-- return value;- }-- return JSON.stringify(value);-}-// Extract MIME type subtype, fallback to full MIME type if subtype is missingfunction getDisplayMimeType(mimeType: string): string {const mimeSubtype = mimeType.split('/')[1];