File size: 5,884 Bytes
07bbbbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { useState, useEffect, useCallback, useRef } from 'react';
import {
  IonPage,
  IonHeader,
  IonToolbar,
  IonTitle,
  IonContent,
  IonButton,
  IonButtons,
  IonIcon,
  IonSegment,
  IonSegmentButton,
  IonLabel,
  IonAlert,
  IonSpinner,
} from '@ionic/react';
import {
  refreshOutline,
  addOutline,
} from 'ionicons/icons';
import { useTranslation } from 'react-i18next';
import { LibraryViewMode, MdxProfile } from '../../types';
import { ProfileListView } from './ProfileListView';
import { GroupListView } from './GroupListView';
import { GroupEditView } from './GroupEditView';
import { useLibraryStore } from '../../store';

/**
 * LibraryView - Combined Component
 * 
 * Library management page with databases and groups views.
 * Handles view mode switching, group management, and library refresh.
 */

export interface LibraryViewProps {
  className?: string;
  onSelectLibrary?: (profileId: number) => void;
}

interface SegmentControlProps {
  options: Array<{ value: LibraryViewMode; label: string }>; 
  value: LibraryViewMode;
  onChange: (value: LibraryViewMode) => void;
}

const SegmentControl: React.FC<SegmentControlProps> = ({ options, value, onChange }) => {
  return (
    <IonSegment 
      value={value} 
      onIonChange={(e) => onChange(e.detail.value as LibraryViewMode)}
    >
      {options.map((option) => (
        <IonSegmentButton key={option.value} value={option.value}>
          <IonLabel>{option.label}</IonLabel>
        </IonSegmentButton>
      ))}
    </IonSegment>
  );
};

export const LibraryView: React.FC<LibraryViewProps> = ({
  className,
  onSelectLibrary
}) => {
  const { t } = useTranslation();
  
  // State
  const [viewMode, setViewMode] = useState<LibraryViewMode>('databases');
  const {loading} = useLibraryStore();
  const {refreshLibrary, createGroup} = useLibraryStore();
  const {getGroup} = useLibraryStore();

  const [error, setError] = useState<string | null>(null);
  const [selectedGroup, setSelectedGroup] = useState<MdxProfile | null>(null);
  const errorTimeoutRef = useRef<number | null>(null);
  
  // Set error with auto-clear
  const setErrorWithTimeout = useCallback((errorMessage: string) => {
    if (errorTimeoutRef.current) {
      window.clearTimeout(errorTimeoutRef.current);
    }
    
    setError(errorMessage);
    
    const timeout = window.setTimeout(() => {
      setError(null);
      errorTimeoutRef.current = null;
    }, 5000);
    
    errorTimeoutRef.current = timeout;
  }, []);
      
  
  // Handle group edit
  const handleGroupEdit = useCallback(async (profileId: number) => {
    try {
      let group = getGroup(profileId);  
      if (group) {
        setSelectedGroup(group);
      } else {
        setErrorWithTimeout(t('Dictionary group does not exist'));
      }
    } catch (err) {
      console.error('Failed to get group:', err);
      setErrorWithTimeout(t('Failed to get dictionary group') + (err as Error).message);
    }
  }, [getGroup, setErrorWithTimeout, setSelectedGroup]);
  
  // Handle back to groups
  const handleBackToGroups = useCallback(() => {
    setSelectedGroup(null);
  }, []);
      
  // Handle create group
  const handleCreateGroup = useCallback(async () => {
    try {
      await createGroup(t('New Dictionary Group'));
    } catch (err) {
      console.error('Failed to create group:', err);
    }
  }, [createGroup, t]);
    
  // Cleanup timeout on unmount
  useEffect(() => {
    return () => {
      if (errorTimeoutRef.current) {
        window.clearTimeout(errorTimeoutRef.current);
      }
    };
  }, []);
  
  // If a group is selected, show the GroupEditView
  if (selectedGroup) {
    return (
      <GroupEditView
        group={selectedGroup}
        onBack={handleBackToGroups}
        onSelectProfile={onSelectLibrary}
      />
    );
  }
  
  return (
    <IonPage className={className}>
      {/* Top toolbar */}
      <IonHeader>
        <IonToolbar>
          <IonTitle>{t('Dictionary Management')}</IonTitle>
          <IonButtons slot="end">
            <IonButton onClick={refreshLibrary} disabled={loading}>
              {loading ? (
                <IonSpinner name="crescent" />
              ) : (
                <IonIcon slot="icon-only" icon={refreshOutline} />
              )}
            </IonButton>
            {viewMode === 'groups' && (
              <IonButton onClick={handleCreateGroup}>
                <IonIcon slot="icon-only" icon={addOutline} />
              </IonButton>
            )}
          </IonButtons>
        </IonToolbar>
      </IonHeader>

      {/* Content area */}
      <IonContent>
        <div style={{ padding: '16px' }}>
          {/* Segment Control */}
          <div style={{ marginBottom: '16px' }}>
            <SegmentControl
              options={[
                { value: 'databases', label: t('Databases') },
                { value: 'groups', label: t('Groups') }
              ]}
              value={viewMode}
              onChange={setViewMode}
            />
          </div>
          
          {/* Error Alert */}
          {error && (
            <IonAlert
              isOpen={true}
              header={t('Error')}
              message={error}
              buttons={[t('OK')]}
              onDidDismiss={() => setError(null)}
            />
          )}

          {/* List View Container */}
          <div style={{ minHeight: 0 }}>
            {viewMode === 'databases' ? (
              <ProfileListView
                parentGroupId={1}
                onSelectProfile={onSelectLibrary}
                isEditMode={false}
              />
            ) : (
              <GroupListView
                onSelectGroup={onSelectLibrary}
                onEditGroup={handleGroupEdit}
                onError={setErrorWithTimeout}
              />
            )}
          </div>
        </div>
      </IonContent>
    </IonPage>
  );
};