Skip to content

Commit

Permalink
Enhance UX for tag selection (#5)
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
icereed authored Oct 4, 2024
1 parent e87d80e commit c7b5c6a
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 67 deletions.
54 changes: 36 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Document struct {
ID int `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
Tags []int `json:"tags"`
Tags []string `json:"tags"`
SuggestedTitle string `json:"suggested_title,omitempty"`
SuggestedTags []string `json:"suggested_tags,omitempty"`
}
Expand Down Expand Up @@ -94,6 +94,19 @@ func main() {
api.GET("/filter-tag", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"tag": tagToFilter})
})
// get all tags
api.GET("/tags", func(c *gin.Context) {
ctx := c.Request.Context()

tags, err := getAllTags(ctx, paperlessBaseURL, paperlessAPIToken)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error fetching tags: %v", err)})
log.Printf("Error fetching tags: %v", err)
return
}

c.JSON(http.StatusOK, tags)
})
}

// Serve static files for the frontend under /static
Expand Down Expand Up @@ -214,23 +227,14 @@ func generateSuggestionsHandler(c *gin.Context) {
// updateDocumentsHandler updates documents with new titles
func updateDocumentsHandler(c *gin.Context) {
ctx := c.Request.Context()

tagIDMapping, err := getIDMappingForTags(ctx, paperlessBaseURL, paperlessAPIToken, []string{tagToFilter})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error fetching tag ID: %v", err)})
log.Printf("Error fetching tag ID: %v", err)
return
}
paperlessGptTagID := tagIDMapping[tagToFilter]

var documents []Document
if err := c.ShouldBindJSON(&documents); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request payload: %v", err)})
log.Printf("Invalid request payload: %v", err)
return
}

err = updateDocuments(ctx, paperlessBaseURL, paperlessAPIToken, documents, paperlessGptTagID)
err := updateDocuments(ctx, paperlessBaseURL, paperlessAPIToken, documents)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error updating documents: %v", err)})
log.Printf("Error updating documents: %v", err)
Expand Down Expand Up @@ -317,13 +321,27 @@ func getDocumentsByTags(ctx context.Context, baseURL, apiToken string, tags []st
return nil, err
}

allTags, err := getAllTags(ctx, baseURL, apiToken)
if err != nil {
return nil, err
}
documents := make([]Document, 0, len(documentsResponse.Results))
for _, result := range documentsResponse.Results {
tagNames := make([]string, len(result.Tags))
for i, resultTagID := range result.Tags {
for tagName, tagID := range allTags {
if resultTagID == tagID {
tagNames[i] = tagName
break
}
}
}

documents = append(documents, Document{
ID: result.ID,
Title: result.Title,
Content: result.Content,
Tags: result.Tags,
Tags: tagNames,
})
}

Expand Down Expand Up @@ -489,7 +507,7 @@ Content:
return strings.TrimSpace(strings.Trim(completion.Choices[0].Content, "\"")), nil
}

func updateDocuments(ctx context.Context, baseURL, apiToken string, documents []Document, paperlessGptTagID int) error {
func updateDocuments(ctx context.Context, baseURL, apiToken string, documents []Document) error {
client := &http.Client{}

// Fetch all available tags
Expand All @@ -505,15 +523,14 @@ func updateDocuments(ctx context.Context, baseURL, apiToken string, documents []
updatedFields := make(map[string]interface{})

newTags := []int{}
for _, tagID := range document.Tags {
if tagID != paperlessGptTagID {
newTags = append(newTags, tagID)
}
}

// Map suggested tag names to IDs
for _, tagName := range document.SuggestedTags {
if tagID, exists := availableTags[tagName]; exists {
// Skip the tag that we are filtering
if tagName == tagToFilter {
continue
}
newTags = append(newTags, tagID)
} else {
log.Printf("Tag '%s' does not exist in paperless-ngx, skipping.", tagName)
Expand All @@ -528,6 +545,7 @@ func updateDocuments(ctx context.Context, baseURL, apiToken string, documents []
}
updatedFields["title"] = suggestedTitle

// Send the update request
url := fmt.Sprintf("%s/api/documents/%d/", baseURL, documentID)

jsonData, err := json.Marshal(updatedFields)
Expand Down
37 changes: 35 additions & 2 deletions web-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion web-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
"@headlessui/react": "^2.1.8",
"@heroicons/react": "^2.1.5",
"axios": "^1.7.7",
"classnames": "^2.5.1",
"prop-types": "^15.8.1",
"react": "^18.3.1",
"react-dom": "^18.3.1"
"react-dom": "^18.3.1",
"react-tag-autocomplete": "^7.3.0"
},
"devDependencies": {
"@eslint/js": "^9.9.0",
Expand Down
Loading

0 comments on commit c7b5c6a

Please sign in to comment.