| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "reasonix/internal/sessioncatalog" |
| 7 | ) |
| 8 | |
| 9 | func projectTopicLess(left, right ProjectNode, sortMode string, manualOrder bool) bool { |
| 10 | if left.Pinned != right.Pinned { |
| 11 | return left.Pinned |
| 12 | } |
| 13 | if manualOrder { |
| 14 | leftRank, rightRank := left.SortOrder, right.SortOrder |
| 15 | if leftRank < 0 { |
| 16 | leftRank = int(^uint(0) >> 1) |
| 17 | } |
| 18 | if rightRank < 0 { |
| 19 | rightRank = int(^uint(0) >> 1) |
| 20 | } |
| 21 | if leftRank != rightRank { |
| 22 | return leftRank < rightRank |
| 23 | } |
| 24 | } |
| 25 | leftActivity := projectTopicSortValue(left.CreatedAt, left.LastActivityAt, sortMode) |
| 26 | rightActivity := projectTopicSortValue(right.CreatedAt, right.LastActivityAt, sortMode) |
| 27 | if leftActivity != rightActivity { |
| 28 | return leftActivity > rightActivity |
| 29 | } |
| 30 | if left.TopicID != right.TopicID { |
| 31 | return left.TopicID < right.TopicID |
| 32 | } |
| 33 | return left.Key < right.Key |
| 34 | } |
| 35 | |
| 36 | func manualTopicOrderFor(scope, workspaceRoot string) bool { |
| 37 | f := loadProjectsFile() |
| 38 | if strings.TrimSpace(scope) != "project" { |
| 39 | return f.GlobalManualSessionOrder || f.GlobalManualTopicOrder |
| 40 | } |
| 41 | if index := projectIndexByRoot(f.Projects, workspaceRoot); index >= 0 { |
| 42 | return f.Projects[index].ManualSessionOrder || f.Projects[index].ManualTopicOrder |
| 43 | } |
| 44 | return false |
| 45 | } |
| 46 | |
| 47 | func manualSessionOrderFor(scope, workspaceRoot string) bool { |
| 48 | f := loadProjectsFile() |
| 49 | if strings.TrimSpace(scope) != "project" { |
| 50 | return f.GlobalManualSessionOrder |
| 51 | } |
| 52 | if index := projectIndexByRoot(f.Projects, workspaceRoot); index >= 0 { |
| 53 | return f.Projects[index].ManualSessionOrder |
| 54 | } |
| 55 | return false |
| 56 | } |
| 57 | |
| 58 | func sessionOrderRank(projects desktopProjectFile, scope, workspaceRoot, key string) int { |
| 59 | order := projects.GlobalSessionOrder |
| 60 | manual := projects.GlobalManualSessionOrder |
| 61 | if strings.TrimSpace(scope) == "project" { |
| 62 | if index := projectIndexByRoot(projects.Projects, workspaceRoot); index >= 0 { |
| 63 | order = projects.Projects[index].SessionOrder |
| 64 | manual = projects.Projects[index].ManualSessionOrder |
| 65 | } |
| 66 | } |
| 67 | if !manual { |
| 68 | return -1 |
| 69 | } |
| 70 | for index, candidate := range order { |
| 71 | if candidate == key { |
| 72 | return index |
| 73 | } |
| 74 | } |
| 75 | return -1 |
| 76 | } |
| 77 | |
| 78 | func encodeProjectTopicCursor(topic sessioncatalog.TopicRecord, sortMode string, manualOrder bool, binding string) string { |
| 79 | pinned := 0 |
| 80 | if topic.Pinned { |
| 81 | pinned = 1 |
| 82 | } |
| 83 | activity := projectTopicSortValue(topic.CreatedAt, topic.LastActivityAt, sortMode) |
| 84 | if manualOrder { |
| 85 | return sessioncatalog.EncodeOrderedTopicCursorBound(pinned, topic.SortOrder, activity, topic.TopicID, binding) |
| 86 | } |
| 87 | return sessioncatalog.EncodeTopicCursorBound(pinned, activity, topic.TopicID, binding) |
| 88 | } |
| 89 | |
| 90 | func encodeProjectNodeCursor(topic ProjectNode, sortMode string, manualOrder bool, binding string) string { |
| 91 | pinned := 0 |
| 92 | if topic.Pinned { |
| 93 | pinned = 1 |
| 94 | } |
| 95 | activity := projectTopicSortValue(topic.CreatedAt, topic.LastActivityAt, sortMode) |
| 96 | if manualOrder { |
| 97 | return sessioncatalog.EncodeOrderedTopicCursorBound(pinned, topic.SortOrder, activity, topic.TopicID, binding) |
| 98 | } |
| 99 | return sessioncatalog.EncodeTopicCursorBound(pinned, activity, topic.TopicID, binding) |
| 100 | } |
| 101 |