Skip to content

Commit 0dd1d0b

Browse files
committed
Fix tags API pagination
1 parent 31f0e81 commit 0dd1d0b

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

main.go

+34-27
Original file line numberDiff line numberDiff line change
@@ -165,40 +165,47 @@ func createLLM() (llms.Model, error) {
165165
}
166166

167167
func getAllTags(ctx context.Context, baseURL, apiToken string) (map[string]int, error) {
168+
tagIDMapping := make(map[string]int)
168169
url := fmt.Sprintf("%s/api/tags/", baseURL)
169-
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
170-
if err != nil {
171-
return nil, err
172-
}
173-
req.Header.Set("Authorization", fmt.Sprintf("Token %s", apiToken))
174170

175171
client := &http.Client{}
176-
resp, err := client.Do(req)
177-
if err != nil {
178-
return nil, err
179-
}
180-
defer resp.Body.Close()
181172

182-
if resp.StatusCode != http.StatusOK {
183-
bodyBytes, _ := io.ReadAll(resp.Body)
184-
return nil, fmt.Errorf("Error fetching tags: %d, %s", resp.StatusCode, string(bodyBytes))
185-
}
173+
for url != "" {
174+
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
175+
if err != nil {
176+
return nil, err
177+
}
178+
req.Header.Set("Authorization", fmt.Sprintf("Token %s", apiToken))
186179

187-
var tagsResponse struct {
188-
Results []struct {
189-
ID int `json:"id"`
190-
Name string `json:"name"`
191-
} `json:"results"`
192-
}
180+
resp, err := client.Do(req)
181+
if err != nil {
182+
return nil, err
183+
}
184+
defer resp.Body.Close()
193185

194-
err = json.NewDecoder(resp.Body).Decode(&tagsResponse)
195-
if err != nil {
196-
return nil, err
197-
}
186+
if resp.StatusCode != http.StatusOK {
187+
bodyBytes, _ := io.ReadAll(resp.Body)
188+
return nil, fmt.Errorf("Error fetching tags: %d, %s", resp.StatusCode, string(bodyBytes))
189+
}
198190

199-
tagIDMapping := make(map[string]int)
200-
for _, tag := range tagsResponse.Results {
201-
tagIDMapping[tag.Name] = tag.ID
191+
var tagsResponse struct {
192+
Results []struct {
193+
ID int `json:"id"`
194+
Name string `json:"name"`
195+
} `json:"results"`
196+
Next string `json:"next"`
197+
}
198+
199+
err = json.NewDecoder(resp.Body).Decode(&tagsResponse)
200+
if err != nil {
201+
return nil, err
202+
}
203+
204+
for _, tag := range tagsResponse.Results {
205+
tagIDMapping[tag.Name] = tag.ID
206+
}
207+
208+
url = tagsResponse.Next
202209
}
203210

204211
return tagIDMapping, nil

web-app/src/components/SuggestionCard.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const SuggestionCard: React.FC<SuggestionCardProps> = ({
1717
onTagAddition,
1818
onTagDeletion,
1919
}) => {
20+
const sortedAvailableTags = availableTags.sort((a, b) => a.name.localeCompare(b.name));
2021
const document = suggestion.original_document;
2122
return (
2223
<div className="bg-white dark:bg-gray-800 shadow-lg shadow-blue-500/50 rounded-md p-4 relative flex flex-col justify-between h-full">
@@ -64,7 +65,7 @@ const SuggestionCard: React.FC<SuggestionCardProps> = ({
6465
value: index.toString(),
6566
})) || []
6667
}
67-
suggestions={availableTags.map((tag) => ({
68+
suggestions={sortedAvailableTags.map((tag) => ({
6869
id: tag.id,
6970
name: tag.name,
7071
label: tag.name,

0 commit comments

Comments
 (0)