Skip to content

Commit

Permalink
Move unsubmitted segments to local storage to remove limits
Browse files Browse the repository at this point in the history
Also add a way to export local storage

Fixes #1933
  • Loading branch information
ajayyy committed Dec 23, 2023
1 parent 58d5036 commit 8e366b1
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 59 deletions.
2 changes: 1 addition & 1 deletion maze-utils
Submodule maze-utils updated 2 files
+8 −13 src/config.ts
+11 −4 src/video.ts
2 changes: 1 addition & 1 deletion public/_locales
Submodule _locales updated 1 files
+6 −0 en/messages.json
25 changes: 25 additions & 0 deletions public/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,31 @@ <h2>__MSG_exportOptions__</h2>
</div>
</div>

<div data-type="private-text-change" data-sync-type="local" data-sync="*" data-confirm-message="exportOptionsWarning">
<h2>__MSG_exportOtherData__</h2>

<div>
<div class="option-button trigger-button inline">
__MSG_exportOptionsCopy__
</div>
<div class="option-button download-button inline">
__MSG_exportOptionsDownload__
</div>
<label for="importLocalOptions" class="option-button inline">
__MSG_exportOptionsUpload__
</label>
<input id="importLocalOptions" type="file" class="upload-button hidden" />
</div>

<div class="option-hidden-section hidden spacing indent">
<textarea class="option-text-box" rows="10" style="width:80%"></textarea>

<div class="option-button text-change-set">
__MSG_setOptions__
</div>
</div>
</div>

<div data-type="button-press" data-sync="resetToDefault" data-confirm-message="confirmResetToDefault">
<div class="option-button trigger-button">
__MSG_resetToDefault__
Expand Down
4 changes: 2 additions & 2 deletions src/components/SkipNoticeComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,9 @@ class SkipNoticeComponent extends React.Component<SkipNoticeProps, SkipNoticeSta
source: SponsorSourceType.Local
};

const segmentTimes = Config.config.unsubmittedSegments[sponsorVideoID] || [];
const segmentTimes = Config.local.unsubmittedSegments[sponsorVideoID] || [];
segmentTimes.push(sponsorTimesSubmitting);
Config.config.unsubmittedSegments[sponsorVideoID] = segmentTimes;
Config.local.unsubmittedSegments[sponsorVideoID] = segmentTimes;
Config.forceSyncUpdate("unsubmittedSegments");

this.props.contentContainer().sponsorTimesSubmitting.push(sponsorTimesSubmitting);
Expand Down
6 changes: 3 additions & 3 deletions src/components/SponsorTimeEditComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ class SponsorTimeEditComponent extends React.Component<SponsorTimeEditProps, Spo
const description = actionType === ActionType.Chapter ? this.descriptionOptionRef?.current?.value : "";
sponsorTimesSubmitting[this.props.index].description = description;

Config.config.unsubmittedSegments[this.props.contentContainer().sponsorVideoID] = sponsorTimesSubmitting;
Config.local.unsubmittedSegments[this.props.contentContainer().sponsorVideoID] = sponsorTimesSubmitting;
Config.forceSyncUpdate("unsubmittedSegments");

this.props.contentContainer().updatePreviewBar();
Expand Down Expand Up @@ -687,9 +687,9 @@ class SponsorTimeEditComponent extends React.Component<SponsorTimeEditProps, Spo

//save this
if (sponsorTimes.length > 0) {
Config.config.unsubmittedSegments[this.props.contentContainer().sponsorVideoID] = sponsorTimes;
Config.local.unsubmittedSegments[this.props.contentContainer().sponsorVideoID] = sponsorTimes;
} else {
delete Config.config.unsubmittedSegments[this.props.contentContainer().sponsorVideoID];
delete Config.local.unsubmittedSegments[this.props.contentContainer().sponsorVideoID];
}
Config.forceSyncUpdate("unsubmittedSegments");

Expand Down
2 changes: 1 addition & 1 deletion src/components/SubmissionNoticeComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class SubmissionNoticeComponent extends React.Component<SubmissionNoticeProps, S
let sponsorTimesSubmitting = this.props.contentContainer().sponsorTimesSubmitting;
sponsorTimesSubmitting = sponsorTimesSubmitting.sort((a, b) => a.segment[0] - b.segment[0]);

Config.config.unsubmittedSegments[this.props.contentContainer().sponsorVideoID] = sponsorTimesSubmitting;
Config.local.unsubmittedSegments[this.props.contentContainer().sponsorVideoID] = sponsorTimesSubmitting;
Config.forceSyncUpdate("unsubmittedSegments");

this.forceUpdate();
Expand Down
4 changes: 2 additions & 2 deletions src/components/options/UnsubmittedVideoListComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class UnsubmittedVideoListComponent extends React.Component<UnsubmittedVideoList

render(): React.ReactElement {
// Render nothing if there are no unsubmitted segments
if (Object.keys(Config.config.unsubmittedSegments).length == 0)
if (Object.keys(Config.local.unsubmittedSegments).length == 0)
return <></>;

return (
Expand Down Expand Up @@ -58,7 +58,7 @@ class UnsubmittedVideoListComponent extends React.Component<UnsubmittedVideoList
getUnsubmittedVideos(): JSX.Element[] {
const elements: JSX.Element[] = [];

for (const videoID of Object.keys(Config.config.unsubmittedSegments)) {
for (const videoID of Object.keys(Config.local.unsubmittedSegments)) {
elements.push(
<UnsubmittedVideoListItem videoID={videoID} key={videoID}>
</UnsubmittedVideoListItem>
Expand Down
8 changes: 4 additions & 4 deletions src/components/options/UnsubmittedVideoListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class UnsubmittedVideoListItem extends React.Component<UnsubmittedVideosListItem
}

render(): React.ReactElement {
const segmentCount = Config.config.unsubmittedSegments[this.props.videoID]?.length ?? 0;
const segmentCount = Config.local.unsubmittedSegments[this.props.videoID]?.length ?? 0;

return (
<>
Expand Down Expand Up @@ -69,17 +69,17 @@ class UnsubmittedVideoListItem extends React.Component<UnsubmittedVideosListItem

clearSegments(): void {
if (confirm(chrome.i18n.getMessage("clearThis"))) {
delete Config.config.unsubmittedSegments[this.props.videoID];
delete Config.local.unsubmittedSegments[this.props.videoID];
Config.forceSyncUpdate("unsubmittedSegments");
}
}

exportSegments(): void {
this.copyToClipboard(exportTimes(Config.config.unsubmittedSegments[this.props.videoID]));
this.copyToClipboard(exportTimes(Config.local.unsubmittedSegments[this.props.videoID]));
}

exportSegmentsAsURL(): void {
this.copyToClipboard(`https://youtube.com/watch?v=${this.props.videoID}${exportTimesAsHashParam(Config.config.unsubmittedSegments[this.props.videoID])}`)
this.copyToClipboard(`https://youtube.com/watch?v=${this.props.videoID}${exportTimesAsHashParam(Config.local.unsubmittedSegments[this.props.videoID])}`)
}

copyToClipboard(text: string): void {
Expand Down
6 changes: 3 additions & 3 deletions src/components/options/UnsubmittedVideosComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class UnsubmittedVideosComponent extends React.Component<UnsubmittedVideosProps,
}

render(): React.ReactElement {
const videoCount = Object.keys(Config.config.unsubmittedSegments).length;
const segmentCount = Object.values(Config.config.unsubmittedSegments).reduce((acc: number, vid: Array<unknown>) => acc + vid.length, 0);
const videoCount = Object.keys(Config.local.unsubmittedSegments).length;
const segmentCount = Object.values(Config.local.unsubmittedSegments).reduce((acc: number, vid: Array<unknown>) => acc + vid.length, 0);

return <>
<div style={{marginBottom: "10px"}}>
Expand All @@ -48,7 +48,7 @@ class UnsubmittedVideosComponent extends React.Component<UnsubmittedVideosProps,

clearAllSegments(): void {
if (confirm(chrome.i18n.getMessage("clearUnsubmittedSegmentsConfirm")))
Config.config.unsubmittedSegments = {};
Config.local.unsubmittedSegments = {};
}
}

Expand Down
31 changes: 18 additions & 13 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ interface SBConfig {
userID: string;
isVip: boolean;
permissions: Record<Category, Permission>;
/* Contains unsubmitted segments that the user has created. */
unsubmittedSegments: Record<string, SponsorTime[]>;
defaultCategory: Category;
renderSegmentsAsChapters: boolean;
whitelistedChannels: string[];
Expand Down Expand Up @@ -142,6 +140,9 @@ interface SBStorage {

// Used when sync storage disbaled
alreadyInstalled: boolean;

/* Contains unsubmitted segments that the user has created. */
unsubmittedSegments: Record<string, SponsorTime[]>;
}

class ConfigClass extends ProtoConfig<SBConfig, SBStorage> {
Expand All @@ -153,6 +154,10 @@ class ConfigClass extends ProtoConfig<SBConfig, SBStorage> {
skipCount: this.config.skipCount,
sponsorTimesContributed: this.config.sponsorTimesContributed
});

chrome.storage.local.set({
...this.localDefaults,
});
}
}

Expand All @@ -161,6 +166,14 @@ function migrateOldSyncFormats(config: SBConfig) {
chrome.storage.sync.remove("showZoomToFillError");
}

if (config["unsubmittedSegments"] && Object.keys(config["unsubmittedSegments"]).length > 0) {
chrome.storage.local.set({
unsubmittedSegments: config["unsubmittedSegments"]
}, () => {
chrome.storage.sync.remove("unsubmittedSegments");
});
}

if (!config["chapterCategoryAdded"]) {
config["chapterCategoryAdded"] = true;

Expand All @@ -174,15 +187,6 @@ function migrateOldSyncFormats(config: SBConfig) {
}
}

if (config["segmentTimes"]) {
const unsubmittedSegments = {};
for (const item of config["segmentTimes"]) {
unsubmittedSegments[item[0]] = item[1];
}

chrome.storage.sync.remove("segmentTimes", () => config.unsubmittedSegments = unsubmittedSegments);
}

if (config["exclusive_accessCategoryAdded"] !== undefined) {
chrome.storage.sync.remove("exclusive_accessCategoryAdded");
}
Expand Down Expand Up @@ -268,7 +272,6 @@ const syncDefaults = {
userID: null,
isVip: false,
permissions: {},
unsubmittedSegments: {},
defaultCategory: "chooseACategory" as Category,
renderSegmentsAsChapters: false,
whitelistedChannels: [],
Expand Down Expand Up @@ -464,7 +467,9 @@ const syncDefaults = {
const localDefaults = {
downvotedSegments: {},
navigationApiAvailable: null,
alreadyInstalled: false
alreadyInstalled: false,

unsubmittedSegments: {}
};

const Config = new ConfigClass(syncDefaults, localDefaults, migrateOldSyncFormats);
Expand Down
20 changes: 10 additions & 10 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ function messageListener(request: Message, sender: unknown, sendResponse: (respo
}

if (addedSegments) {
Config.config.unsubmittedSegments[getVideoID()] = sponsorTimesSubmitting;
Config.local.unsubmittedSegments[getVideoID()] = sponsorTimesSubmitting;
Config.forceSyncUpdate("unsubmittedSegments");

updateEditButtonsOnPlayer();
Expand Down Expand Up @@ -1925,7 +1925,7 @@ function startOrEndTimingNewSegment() {
}

// Save the newly created segment
Config.config.unsubmittedSegments[getVideoID()] = sponsorTimesSubmitting;
Config.local.unsubmittedSegments[getVideoID()] = sponsorTimesSubmitting;
Config.forceSyncUpdate("unsubmittedSegments");

// Make sure they know if someone has already submitted something it while they were watching
Expand Down Expand Up @@ -1958,11 +1958,11 @@ function cancelCreatingSegment() {
if (isSegmentCreationInProgress()) {
if (sponsorTimesSubmitting.length > 1) { // If there's more than one segment: remove last
sponsorTimesSubmitting.pop();
Config.config.unsubmittedSegments[getVideoID()] = sponsorTimesSubmitting;
Config.local.unsubmittedSegments[getVideoID()] = sponsorTimesSubmitting;
} else { // Otherwise delete the video entry & close submission menu
resetSponsorSubmissionNotice();
sponsorTimesSubmitting = [];
delete Config.config.unsubmittedSegments[getVideoID()];
delete Config.local.unsubmittedSegments[getVideoID()];
}
Config.forceSyncUpdate("unsubmittedSegments");
}
Expand All @@ -1972,7 +1972,7 @@ function cancelCreatingSegment() {
}

function updateSponsorTimesSubmitting(getFromConfig = true) {
const segmentTimes = Config.config.unsubmittedSegments[getVideoID()];
const segmentTimes = Config.local.unsubmittedSegments[getVideoID()];

//see if this data should be saved in the sponsorTimesSubmitting variable
if (getFromConfig && segmentTimes != undefined) {
Expand Down Expand Up @@ -2102,7 +2102,7 @@ function closeInfoMenu() {
function clearSponsorTimes() {
const currentVideoID = getVideoID();

const sponsorTimes = Config.config.unsubmittedSegments[currentVideoID];
const sponsorTimes = Config.local.unsubmittedSegments[currentVideoID];

if (sponsorTimes != undefined && sponsorTimes.length > 0) {
const confirmMessage = chrome.i18n.getMessage("clearThis") + getSegmentsMessage(sponsorTimes)
Expand All @@ -2112,7 +2112,7 @@ function clearSponsorTimes() {
resetSponsorSubmissionNotice();

//clear the sponsor times
delete Config.config.unsubmittedSegments[currentVideoID];
delete Config.local.unsubmittedSegments[currentVideoID];
Config.forceSyncUpdate("unsubmittedSegments");

//clear sponsor times submitting
Expand Down Expand Up @@ -2276,7 +2276,7 @@ async function sendSubmitMessage() {
}

//update sponsorTimes
Config.config.unsubmittedSegments[getVideoID()] = sponsorTimesSubmitting;
Config.local.unsubmittedSegments[getVideoID()] = sponsorTimesSubmitting;
Config.forceSyncUpdate("unsubmittedSegments");

// Check to see if any of the submissions are below the minimum duration set
Expand Down Expand Up @@ -2304,7 +2304,7 @@ async function sendSubmitMessage() {
stopAnimation();

// Remove segments from storage since they've already been submitted
delete Config.config.unsubmittedSegments[getVideoID()];
delete Config.local.unsubmittedSegments[getVideoID()];
Config.forceSyncUpdate("unsubmittedSegments");

const newSegments = sponsorTimesSubmitting;
Expand Down Expand Up @@ -2610,7 +2610,7 @@ function checkForPreloadedSegment() {
}

if (pushed) {
Config.config.unsubmittedSegments[getVideoID()] = sponsorTimesSubmitting;
Config.local.unsubmittedSegments[getVideoID()] = sponsorTimesSubmitting;
Config.forceSyncUpdate("unsubmittedSegments");
}
}
Expand Down
Loading

1 comment on commit 8e366b1

@JH2535
Copy link

@JH2535 JH2535 commented on 8e366b1 Dec 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you <3

Please sign in to comment.