Skip to content

Conversation

@icecrasher321
Copy link
Collaborator

@icecrasher321 icecrasher321 commented Jan 15, 2026

Summary

  • Initial ordering that's loaded into the registry store must be deterministic, so we use that as proxy for sorted order.
  • New workflows should show up at the top like on main

Type of Change

  • Bug fix

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link

vercel bot commented Jan 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Review Updated (UTC)
docs Skipped Skipped Jan 15, 2026 6:17am

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Jan 15, 2026

Greptile Summary

Changes workflow ordering from max-based (appending new items at bottom) to min-based (prepending new items at top), implementing deterministic three-way sort comparison across the entire workflow list system.

Key changes:

  • New workflows and duplicates now appear at the top of lists by using min(sortOrder) - 1 instead of max(sortOrder) + 1
  • Database queries in GET /api/workflows now include explicit ORDER BY sortOrder ASC, createdAt ASC, id ASC
  • Frontend sorting consistently applies the same three-way comparison: sortOrdercreatedAtid for tie-breaking
  • Drag-and-drop reordering renormalizes sortOrder to sequential 0-based indices, preventing unbounded negative growth while preserving user-defined order

Implementation consistency:
The PR correctly updates all layers: API routes, persistence helpers, React Query optimistic updates, frontend sorting, and drag-drop handlers. The three-way comparison provides deterministic ordering even when sortOrder values collide (which can happen between drag operations).

Confidence Score: 4/5

  • Safe to merge with low risk - well-structured refactoring with consistent implementation across all layers
  • Deducting one point because while the implementation is correct and consistent, the PR changes core ordering behavior without database migration or data backfill. Existing workflows with positive sortOrder values will continue working correctly, but the mental model shifts from "append at bottom" to "prepend at top" which may surprise users who haven't dragged items recently.
  • No files require special attention - all changes follow consistent patterns

Important Files Changed

Filename Overview
apps/sim/app/api/workflows/route.ts Changed from max to min sortOrder calculation, added deterministic ordering by sortOrder→createdAt→id to GET endpoint
apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/workflow-list/workflow-list.tsx Added compareByOrder function and applied consistent three-way sort comparison (sortOrder→createdAt→id) across all workflow/folder sorting
apps/sim/app/workspace/[workspaceId]/w/components/sidebar/hooks/use-drag-drop.ts Updated SiblingItem type to include createdAt, implemented compareSiblingItems for consistent three-way sorting during drag operations
apps/sim/hooks/queries/workflows.ts Changed from max to min sortOrder calculation in both useCreateWorkflow and useDuplicateWorkflowMutation optimistic updates
apps/sim/lib/workflows/persistence/duplicate.ts Added min sortOrder calculation for duplicated workflows to place them at the top of the list

Sequence Diagram

sequenceDiagram
    participant User
    participant UI as WorkflowList Component
    participant Store as WorkflowRegistry Store
    participant API as POST /api/workflows
    participant DB as Database
    
    Note over User,DB: Creating New Workflow
    User->>UI: Create new workflow
    UI->>Store: Get existing workflows
    Store-->>UI: Return workflows with sortOrder
    UI->>UI: Calculate min(sortOrder) - 1
    UI->>API: POST with sortOrder
    API->>DB: Query min(sortOrder) from folder
    DB-->>API: Return minOrder
    API->>API: Calculate sortOrder = (minOrder ?? 1) - 1
    API->>DB: INSERT workflow with sortOrder, createdAt
    DB-->>API: Success
    API-->>UI: Return new workflow
    
    Note over User,DB: Loading Workflows
    User->>API: GET /api/workflows
    API->>DB: SELECT * ORDER BY sortOrder ASC, createdAt ASC, id ASC
    DB-->>API: Return ordered workflows
    API-->>UI: Return workflows
    UI->>UI: Apply compareByOrder(sortOrder, createdAt, id)
    UI-->>User: Display workflows in deterministic order
    
    Note over User,DB: Drag and Drop Reorder
    User->>UI: Drag workflow to new position
    UI->>UI: Calculate new order array
    UI->>UI: Reassign sortOrder = 0, 1, 2, 3...
    UI->>API: POST /api/workflows/reorder
    API->>DB: UPDATE workflows SET sortOrder
    DB-->>API: Success
    API-->>UI: Return success
    UI-->>User: Display reordered workflows
Loading

@icecrasher321 icecrasher321 merged commit d4c171c into staging Jan 15, 2026
10 checks passed
@icecrasher321 icecrasher321 deleted the fix/init-sort-order branch January 15, 2026 06:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants