File size: 6,584 Bytes
6111b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { useState, useEffect, useCallback } from "react";
import { Button } from "@/shared/components";
import { useTranslations } from "next-intl";

interface CacheEntry {
  id: string;
  signature: string;
  model: string;
  hit_count: number;
  tokens_saved: number;
  created_at: string;
  expires_at: string;
}

interface Pagination {
  page: number;
  limit: number;
  total: number;
  totalPages: number;
}

export default function CacheEntriesTab() {
  const t = useTranslations("cache");
  const [entries, setEntries] = useState<CacheEntry[]>([]);
  const [pagination, setPagination] = useState<Pagination>({
    page: 1,
    limit: 20,
    total: 0,
    totalPages: 0,
  });
  const [loading, setLoading] = useState(true);
  const [search, setSearch] = useState("");
  const [deleting, setDeleting] = useState<string | null>(null);
  const [error, setError] = useState<string | null>(null);

  const fetchEntries = useCallback(
    async (page = 1) => {
      setLoading(true);
      setError(null);
      try {
        const params = new URLSearchParams({ page: String(page), limit: String(pagination.limit) });
        if (search) params.set("search", search);

        const res = await fetch(`/api/cache/entries?${params}`);
        if (res.ok) {
          const data = await res.json();
          setEntries(data.entries);
          setPagination(data.pagination);
        } else {
          setEntries([]);
          setError(t("entriesLoadError"));
        }
      } catch {
        setEntries([]);
        setError(t("entriesLoadError"));
      } finally {
        setLoading(false);
      }
    },
    [pagination.limit, search, t]
  );

  useEffect(() => {
    fetchEntries();
  }, [fetchEntries]);

  const handleDelete = async (signature: string) => {
    setDeleting(signature);
    try {
      await fetch(`/api/cache/entries?signature=${encodeURIComponent(signature)}`, {
        method: "DELETE",
      });
      await fetchEntries(pagination.page);
    } finally {
      setDeleting(null);
    }
  };

  const formatDate = (dateStr: string) => {
    return new Date(dateStr).toLocaleString();
  };

  return (
    <div className="flex flex-col gap-4">

      <div className="flex items-center gap-3">

        <input

          type="text"

          placeholder={t("searchEntries")}

          value={search}

          onChange={(e) => setSearch(e.target.value)}

          onKeyDown={(e) => e.key === "Enter" && fetchEntries()}

          className="flex-1 px-3 py-2 text-sm rounded-lg border border-border bg-surface text-text-main placeholder:text-text-muted"

        />

        <Button variant="secondary" size="sm" onClick={() => fetchEntries()}>

          {t("search")}

        </Button>

      </div>



      {loading ? (

        <div className="text-sm text-text-muted">{t("loading")}</div>

      ) : error ? (

        <div className="flex items-center justify-between gap-3 rounded-xl border border-red-500/20 bg-red-500/5 px-4 py-3">

          <div className="text-sm text-red-300">{error}</div>

          <Button variant="secondary" size="sm" onClick={() => fetchEntries(pagination.page)}>

            {t("refresh")}

          </Button>

        </div>

      ) : entries.length === 0 ? (

        <div className="text-sm text-text-muted text-center py-8">{t("noEntries")}</div>

      ) : (

        <>

          <div className="overflow-x-auto">

            <table className="w-full text-sm">

              <thead>

                <tr className="text-left text-xs text-text-muted border-b border-border/30">

                  <th className="pb-2 pr-4">{t("signature")}</th>

                  <th className="pb-2 pr-4">{t("model")}</th>

                  <th className="pb-2 pr-4">{t("hits")}</th>

                  <th className="pb-2 pr-4">{t("tokensSaved")}</th>

                  <th className="pb-2 pr-4">{t("created")}</th>

                  <th className="pb-2 pr-4">{t("expires")}</th>

                  <th className="pb-2">{t("actions")}</th>

                </tr>

              </thead>

              <tbody>

                {entries.map((entry) => (

                  <tr key={entry.id} className="border-b border-border/20">

                    <td className="py-2 pr-4 font-mono text-xs">

                      {entry.signature.slice(0, 12)}...

                    </td>

                    <td className="py-2 pr-4">{entry.model}</td>

                    <td className="py-2 pr-4 tabular-nums">{entry.hit_count}</td>

                    <td className="py-2 pr-4 tabular-nums text-green-500">

                      {(entry.tokens_saved ?? 0).toLocaleString()}

                    </td>

                    <td className="py-2 pr-4 text-xs text-text-muted">

                      {formatDate(entry.created_at)}

                    </td>

                    <td className="py-2 pr-4 text-xs text-text-muted">

                      {formatDate(entry.expires_at)}

                    </td>

                    <td className="py-2">

                      <button

                        onClick={() => handleDelete(entry.signature)}

                        disabled={deleting === entry.signature}

                        className="text-xs text-red-400 hover:text-red-300 disabled:opacity-50"

                      >

                        {deleting === entry.signature ? "..." : "🗑️"}

                      </button>

                    </td>

                  </tr>

                ))}

              </tbody>

            </table>

          </div>



          {/* Pagination */}

          {pagination.totalPages > 1 && (

            <div className="flex items-center justify-center gap-2 pt-2">

              <Button

                variant="secondary"

                size="sm"

                onClick={() => fetchEntries(pagination.page - 1)}

                disabled={pagination.page <= 1}

              >


              </Button>

              <span className="text-sm text-text-muted">

                {pagination.page} / {pagination.totalPages}

              </span>

              <Button

                variant="secondary"

                size="sm"

                onClick={() => fetchEntries(pagination.page + 1)}

                disabled={pagination.page >= pagination.totalPages}

              >


              </Button>

            </div>

          )}

        </>
      )}
    </div>
  );
}