From b881898e1afd5564374613e9a258e912638123bc Mon Sep 17 00:00:00 2001 From: Corban-Lee Jones Date: Sat, 3 Aug 2024 23:13:55 +0100 Subject: [PATCH] resolve channel names --- apps/static/js/home/content.js | 87 ++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 35 deletions(-) diff --git a/apps/static/js/home/content.js b/apps/static/js/home/content.js index 1462222..d7975f4 100644 --- a/apps/static/js/home/content.js +++ b/apps/static/js/home/content.js @@ -1,5 +1,6 @@ var contentTable; contentOptions = null; + channelResolveInterval = null; async function initContentTable() { contentOptions = await getTrackedContentOptions(); @@ -67,9 +68,13 @@ async function initContentTable() { { title: "Channel", data: "channel_id", - className: "text-start content-channel-id", - render: function(data) { - return $("").text(data)[0]; + className: "text-start", + render: function(data, type, row) { + return `
+
+ Loading... +
+
`; } }, { @@ -100,47 +105,54 @@ async function initContentTable() { }); bindTableCheckboxes("#contentTable", contentTable, "#contentTabPane .table-del-btn"); + + contentTable.on("draw", function() { + restartResolveChannelNamesTask(); + }); } -function handleDiscordChannelNames() { - let interval = setInterval(function() { - if (!discordChannels.length) - return; +// #region Resolve Channels - $("#contentTable tr td.content-channel-id").each(function() { - let tracked = contentTable.row($(this).closest("tr")).data(); - channel = discordChannels.find(item => item.value === tracked.channel_id); +function restartResolveChannelNamesTask() { + clearInterval(channelResolveInterval); + startResolveChannelNamesTask(); +} - if (!channel) - return; +function startResolveChannelNamesTask() { + const guildId = getCurrentlyActiveServer().guild_id; + channelResolveInterval = setInterval(function() { + if (resolveChannelNames(guildId)) + clearInterval(channelResolveInterval); + }, 50) +} - $(this).text("").append( - $("") - .attr("href", `https://discord.com/channels/${getCurrentlyActiveServer().guild_id}/${channel.value}/${tracked.message_id}`) - .attr("target", "_blank") - .addClass("text-decoration-none") - .text(channel.text) +function resolveChannelNames(guildId) { + if (!discordChannels.length) + return false + + $(".resolve-channel-name").each(function() { + const channelId = $(this).data("channel-id"); + const messageId = $(this).data("msg-id"); + console.log(channelId + " " + messageId); + const channel = discordChannels.find(channel => channel.value === channelId); + + if (channel) { + const href = `https://discord.com/channels/${guildId}/${channelId}/${messageId}/`; + $(this).replaceWith( + $("").text(channel.text) + .attr("href", href) + .attr("target", "_blank") + .addClass("text-decoration-none text-nowrap") ); - }); + } + }); - // $(".replace-discord-channel-id-with-name").each(function() { - // let displayChannel = discordChannels.find(channel => channel.value == $(this).text()); - // if (displayChannel) { - // $(this).text("").append( - // $("") - // .attr("href", `https://discord.com/channels/${getCurrentlyActiveServer().guild_id}/${displayChannel.value}`) - // .attr("target", "_blank") - // .addClass("text-decoration-none") - // .text(displayChannel.text) - // ); - // } - // }); - - - }, 600); + return true; } +// #endregion + async function goToSubscription(subId) { $("#subscriptionsTab").click(); await showEditSubModal(subId); @@ -181,9 +193,12 @@ function clearExistingContentRows() { } $("#contentTabPane").on("click", ".table-refresh-btn", async function() { - loadContent(getCurrentlyActiveServer().guild_id); + await loadContent(getCurrentlyActiveServer().guild_id); }); + +// #region Load Content + async function loadContent(guildId) { if (!guildId) @@ -223,3 +238,5 @@ $(document).on("selectedServerChange", async function() { const activeServer = getCurrentlyActiveServer(); await loadContent(activeServer.guild_id); }); + +// #endregion