summaryrefslogtreecommitdiff
path: root/makima/frontend/src/components/files/RepoSyncIndicator.tsx
blob: 82d79f7918ad6f463d9a213320356200149fbfac (plain) (blame)
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
import { useState, useCallback } from "react";
import { syncFileFromRepo } from "../../lib/api";

interface RepoSyncIndicatorProps {
  fileId: string;
  repoFilePath: string | null | undefined;
  repoSyncStatus: string | null | undefined;
  repoSyncedAt: string | null | undefined;
  onSyncComplete?: () => void;
  /** Callback to push file content to repo (creates a task) */
  onPushToRepo?: () => void;
  /** Whether a push operation is in progress */
  isPushing?: boolean;
}

/**
 * Shows repository file link status and provides sync functionality.
 * Displays the linked file path and allows updating from the repo via daemon,
 * or pushing local changes back to the repo.
 */
export function RepoSyncIndicator({
  fileId,
  repoFilePath,
  repoSyncStatus,
  repoSyncedAt,
  onSyncComplete,
  onPushToRepo,
  isPushing = false,
}: RepoSyncIndicatorProps) {
  const [isSyncing, setIsSyncing] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const handleSync = useCallback(async () => {
    setIsSyncing(true);
    setError(null);
    try {
      await syncFileFromRepo(fileId);
      // The actual update happens via WebSocket notification
      // Give a brief delay then notify parent
      setTimeout(() => {
        onSyncComplete?.();
      }, 500);
    } catch (err) {
      setError(err instanceof Error ? err.message : "Sync failed");
    } finally {
      setIsSyncing(false);
    }
  }, [fileId, onSyncComplete]);

  // Don't render if no repo file path is set
  if (!repoFilePath) {
    return null;
  }

  const isActuallySyncing = isSyncing || repoSyncStatus === "syncing";
  const isSynced = repoSyncStatus === "synced";
  const isModified = repoSyncStatus === "modified";

  // Format the synced timestamp
  const syncedAtFormatted = repoSyncedAt
    ? new Date(repoSyncedAt).toLocaleString()
    : null;

  return (
    <div className="flex items-center gap-2 text-xs font-mono">
      {/* File path icon and link */}
      <div className="flex items-center gap-1 text-[#555]">
        <svg
          width="12"
          height="12"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          strokeWidth="2"
          className="flex-shrink-0"
        >
          <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
          <polyline points="14 2 14 8 20 8" />
        </svg>
        <span className="text-[#75aafc]" title={`Linked to repository file: ${repoFilePath}`}>
          {repoFilePath}
        </span>
      </div>

      {/* Status indicator */}
      {isSynced && (
        <span
          className="text-green-500 flex items-center gap-1"
          title={syncedAtFormatted ? `Last synced: ${syncedAtFormatted}` : "Synced"}
        >
          <svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3">
            <polyline points="20 6 9 17 4 12" />
          </svg>
        </span>
      )}
      {isModified && (
        <span className="text-yellow-500" title="File modified, may need sync">
          <svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3">
            <circle cx="12" cy="12" r="10" />
            <line x1="12" y1="8" x2="12" y2="12" />
            <line x1="12" y1="16" x2="12.01" y2="16" />
          </svg>
        </span>
      )}

      {/* Pull from repo button */}
      <button
        onClick={handleSync}
        disabled={isActuallySyncing || isPushing}
        className={`flex items-center gap-1 px-1.5 py-0.5 rounded transition-colors ${
          isActuallySyncing || isPushing
            ? "text-[#555] cursor-wait"
            : "text-[#555] hover:text-[#75aafc] hover:bg-[rgba(117,170,252,0.1)]"
        }`}
        title="Pull latest from repository"
      >
        {isActuallySyncing ? (
          <>
            <svg
              width="10"
              height="10"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              strokeWidth="2"
              className="animate-spin"
            >
              <circle cx="12" cy="12" r="10" strokeDasharray="32" strokeDashoffset="8" />
            </svg>
            <span>Pulling...</span>
          </>
        ) : (
          <>
            <svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
              <path d="M12 19V5" />
              <path d="M5 12l7-7 7 7" />
            </svg>
            <span>Pull</span>
          </>
        )}
      </button>

      {/* Push to repo button */}
      {onPushToRepo && (
        <button
          onClick={onPushToRepo}
          disabled={isActuallySyncing || isPushing}
          className={`flex items-center gap-1 px-1.5 py-0.5 rounded transition-colors ${
            isPushing
              ? "text-[#555] cursor-wait"
              : "text-[#555] hover:text-green-500 hover:bg-[rgba(34,197,94,0.1)]"
          }`}
          title="Push changes to repository (creates a task)"
        >
          {isPushing ? (
            <>
              <svg
                width="10"
                height="10"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                strokeWidth="2"
                className="animate-spin"
              >
                <circle cx="12" cy="12" r="10" strokeDasharray="32" strokeDashoffset="8" />
              </svg>
              <span>Pushing...</span>
            </>
          ) : (
            <>
              <svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
                <path d="M12 5v14" />
                <path d="M19 12l-7 7-7-7" />
              </svg>
              <span>Push</span>
            </>
          )}
        </button>
      )}

      {/* Error message */}
      {error && (
        <span className="text-red-500 text-[10px]" title={error}>
          Failed
        </span>
      )}
    </div>
  );
}