Skip to content

Commit

Permalink
toniebox-reverse-engineeringgh-52 Prepared TAP feature, added delete …
Browse files Browse the repository at this point in the history
…feature in content, library and tap,
  • Loading branch information
henryk86 committed May 18, 2024
1 parent 5b960fc commit 0ed6a41
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 59 deletions.
14 changes: 13 additions & 1 deletion public/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
"saveAs": "Datei speichern unter",
"removeFile": "Datei entfernen"
},
"tap": {
"navigationTitle": "Audio Playlists",
"title": "Tonie Audio Playlists"
},
"messages": {
"couldNotEmptySource": "Konnte Quelle nicht leeren!",
"sourceSetSuccessful": "Quelle auf {{selectedFile}} gesetzt!",
Expand Down Expand Up @@ -243,12 +247,20 @@
"episode": "Episode",
"date": "Datum",
"playFile": "Abspielen",
"delete": "Löschen",
"migrateContentToLib": "TAF-Datei in Library (by/audioId/) migrieren",
"migrateContentToLibRoot": "TAF-Datei in Library (root) migrieren",
"messages": {
"migrationOngoing": "migriere...",
"migrationSuccessful": "Migration erfolgreich!",
"migrationFailed": "Migration fehlgeschlagen!"
"migrationFailed": "Migration fehlgeschlagen",
"deleting": "Wird gelöscht...",
"deleteSuccessful": "Löschvorgang erfolgreich!",
"deleteFailed": "Löschvorgang fehlgeschlagen"
},
"tap": {
"edit": "(WIP - not implemented yet) Tonie Audio Playlist editieren",
"copy": "(WIP - not implemented yet) Tonie Audio Playlist duplizieren"
}
},
"tonieArticleSearch": {
Expand Down
14 changes: 13 additions & 1 deletion public/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@
"saveAs": "Save file as",
"removeFile": "Remove file"
},
"tap": {
"navigationTitle": "Audio Playlists",
"title": "Tonie Audio Playlists"
},
"messages": {
"couldNotEmptySource": "Could not empty source!",
"sourceSetSuccessful": "Source set to {{selectedFile}}!",
Expand Down Expand Up @@ -242,12 +246,20 @@
"episode": "Episode",
"date": "Date",
"playFile": "Play",
"delete": "Delete",
"migrateContentToLib": "Migrate TAF to library (by/audioId/)",
"migrateContentToLibRoot": "Migrate TAF to library (root)",
"messages": {
"migrationOngoing": "Migrating...",
"migrationSuccessful": "Migration successful!",
"migrationFailed": "Migration failed!"
"migrationFailed": "Migration failed",
"deleting": "Deleting...",
"deleteSuccessful": "Deleted successful!",
"deleteFailed": "Delete failed"
},
"tap": {
"edit": "(WIP - not implemented yet) Edit Tonie Audio Playlist",
"copy": "(WIP - not implemented yet) Duplicate Tonie Audio Playlist"
}
},
"tonieArticleSearch": {
Expand Down
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { SystemSoundsPage } from "./pages/tonies/SystemSoundsPage";
import { ContentPage } from "./pages/tonies/ContentPage";
import { LibraryPage } from "./pages/tonies/LibraryPage";
import { EncoderPage } from "./pages/tonies/EncoderPage";
import { TonieAudioPlaylistsPage } from "./pages/tonies/TonieAudioPlaylistsPage";
import { TonieboxesPage } from "./pages/tonieboxes/TonieboxesPage";
import { CommunityPage } from "./pages/community/CommunityPage";
import { ContributionPage } from "./pages/community/ContributionPage";
Expand Down Expand Up @@ -89,6 +90,7 @@ function App() {
<Route path="/tonies/content" element={<ContentPage />} />
<Route path="/tonies/library" element={<LibraryPage />} />
<Route path="/tonies/encoder" element={<EncoderPage />} />
<Route path="/tonies/tap" element={<TonieAudioPlaylistsPage />} />
<Route path="/tonieboxes" element={<TonieboxesPage />} />
<Route path="/tonieboxes/esp32boxflashing" element={<ESP32BoxFlashing />} />
<Route path="/settings" element={<SettingsPage />} />
Expand Down
204 changes: 157 additions & 47 deletions src/components/tonies/FileBrowser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,37 @@ import { SortOrder } from "antd/es/table/interface";

import { useAudioContext } from "../audio/AudioContext";

import { CloudServerOutlined, PlayCircleOutlined, TruckOutlined } from "@ant-design/icons";
import {
CloudServerOutlined,
CopyOutlined,
DeleteOutlined,
EditOutlined,
PlayCircleOutlined,
TruckOutlined,
} from "@ant-design/icons";
import { humanFileSize } from "../../util/humanFileSize";

export const FileBrowser: React.FC<{
special: string;
filetypeFilter?: string[];
isTapList?: boolean;
overlay?: string;
maxSelectedRows?: number;
trackUrl?: boolean;
selectTafOnly?: boolean;
showDirOnly?: boolean;
showColumns?: string[];
onFileSelectChange?: (files: any[], path: string, special: string) => void;
}> = ({
special,
filetypeFilter = [],
isTapList = false,
overlay = "",
maxSelectedRows = 0,
selectTafOnly = true,
trackUrl = true,
showDirOnly = false,
showColumns = undefined,
onFileSelectChange,
}) => {
const { t } = useTranslation();
Expand Down Expand Up @@ -55,7 +68,8 @@ export const FileBrowser: React.FC<{
};

const showJsonViewer = (file: string) => {
fetchJsonData(process.env.REACT_APP_TEDDYCLOUD_API_URL + "/content" + file);
const folder = special === "library" ? "/library" : "/content";
fetchJsonData(process.env.REACT_APP_TEDDYCLOUD_API_URL + folder + file);
setCurrentFile(file);
setJsonViewerModalOpened(true);
};
Expand Down Expand Up @@ -120,7 +134,11 @@ export const FileBrowser: React.FC<{
var list: never[] = data.files;

if (showDirOnly) list = list.filter((file: any) => file.isDir);

if (filetypeFilter.length > 0)
list = list.filter(
(file: any) =>
file.isDir || filetypeFilter.some((filetypeFilter) => file.name.endsWith(filetypeFilter))
);
setFiles(list);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down Expand Up @@ -219,6 +237,55 @@ export const FileBrowser: React.FC<{
}
};

const deleteFile = (path: string, apiCall: string) => {
try {
messageApi.open({
type: "loading",
content: t("fileBrowser.messages.deleting"),
duration: 0,
});

const body = path;
fetch(process.env.REACT_APP_TEDDYCLOUD_API_URL + "/api/fileDelete" + apiCall, {
method: "POST",
body: body,
headers: {
"Content-Type": "text/plain",
},
})
.then((response) => response.text())
.then((data) => {
messageApi.destroy();
if (data === "OK") {
messageApi.open({
type: "success",
content: t("fileBrowser.messages.deleteSuccessful"),
});
// now the page shall reload
setRebuildList(!rebuildList);
} else {
messageApi.open({
type: "error",
content: t("fileBrowser.messages.deleteFailed") + ": " + data,
});
}
})
.catch((error) => {
messageApi.destroy();
messageApi.open({
type: "error",
content: t("fileBrowser.messages.deleteFailed") + ": " + error,
});
});
} catch (error) {
messageApi.destroy();
messageApi.open({
type: "error",
content: t("fileBrowser.messages.deleteFailed") + ": " + error,
});
}
};

var columns = [
{
title: "",
Expand Down Expand Up @@ -275,48 +342,82 @@ export const FileBrowser: React.FC<{
dataIndex: "name",
key: "controls",
sorter: undefined,
render: (name: string, record: any) =>
record.tafHeader
? [
special !== "library" ? (
<Tooltip title={t("fileBrowser.migrateContentToLib")}>
<CloudServerOutlined
onClick={() => migrateContent2Lib(path.replace("/", "") + name, false, overlay)}
style={{ margin: "0 16px 0 0" }}
/>
</Tooltip>
) : (
""
),
special !== "library" ? (
<Tooltip title={t("fileBrowser.migrateContentToLibRoot")}>
<TruckOutlined
onClick={() => migrateContent2Lib(path.replace("/", "") + name, true, overlay)}
style={{ margin: "0 16px 0 0" }}
/>
</Tooltip>
) : (
""
),
<Tooltip title={t("fileBrowser.playFile")}>
<PlayCircleOutlined
onClick={() =>
playAudio(
process.env.REACT_APP_TEDDYCLOUD_API_URL +
"/content" +
path +
"/" +
name +
"?ogg=true&special=" +
special +
(overlay ? `&overlay=${overlay}` : ""),
record.tonieInfo
)
}
/>
</Tooltip>,
]
: "",
render: (name: string, record: any) => {
let actions = [];

// taf file
if (record.tafHeader) {
// migration to lib possible
if (special !== "library") {
actions.push(
<Tooltip title={t("fileBrowser.migrateContentToLib")}>
<CloudServerOutlined
onClick={() => migrateContent2Lib(path.replace("/", "") + name, false, overlay)}
style={{ margin: "0 16px 0 0" }}
/>
</Tooltip>
);
actions.push(
<Tooltip title={t("fileBrowser.migrateContentToLibRoot")}>
<TruckOutlined
onClick={() => migrateContent2Lib(path.replace("/", "") + name, true, overlay)}
style={{ margin: "0 16px 0 0" }}
/>
</Tooltip>
);
}
actions.push(
<Tooltip title={t("fileBrowser.playFile")}>
<PlayCircleOutlined
style={{ margin: "0 16px 0 0" }}
onClick={() =>
playAudio(
process.env.REACT_APP_TEDDYCLOUD_API_URL +
"/content" +
path +
"/" +
name +
"?ogg=true&special=" +
special +
(overlay ? `&overlay=${overlay}` : ""),
record.tonieInfo
)
}
/>
</Tooltip>
);
}
// tap file
if (isTapList && record.name.includes(".tap")) {
actions.push(
<Tooltip title={t("fileBrowser.tap.edit")}>
<EditOutlined style={{ margin: "0 16px 0 0" }} />
</Tooltip>
);
actions.push(
<Tooltip title={t("fileBrowser.tap.copy")}>
<CopyOutlined style={{ margin: "0 16px 0 0" }} />
</Tooltip>
);
}
// include the delete action
if (record.name !== ".." && maxSelectedRows === 0) {
actions.push(
<Tooltip title={t("fileBrowser.delete")}>
<DeleteOutlined
onClick={() =>
deleteFile(
path + "/" + name,
"?special=" + special + (overlay ? `&overlay=${overlay}` : "")
)
}
style={{ margin: "0 16px 0 0" }}
/>
</Tooltip>
);
}
return actions;
},
showOnDirOnly: false,
},
];
Expand All @@ -329,6 +430,16 @@ export const FileBrowser: React.FC<{

if (showDirOnly) columns = columns.filter((column) => column.showOnDirOnly);

if (showColumns) {
columns = columns.filter((column) => {
if (typeof column.dataIndex === "string") {
// Check if the column's dataIndex matches any of the specified dataIndex values
return showColumns.includes(column.dataIndex);
}
return false;
});
}

return (
<>
{contextHolder}
Expand All @@ -350,8 +461,7 @@ export const FileBrowser: React.FC<{
onDoubleClick: () => {
if (record.isDir) {
handleDirClick(record.name);
} else if (record.name.includes(".json")) {
console.log("json");
} else if (record.name.includes(".json") || record.name.includes(".tap")) {
showJsonViewer(path + "/" + record.name);
}
},
Expand Down
Loading

0 comments on commit 0ed6a41

Please sign in to comment.