Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support current date label #242

Merged
merged 2 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Renderer/Event/DateEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {Event} from '../Library/Infra/Event';

enum EventNames {
changingDate = 'changingDate',
}

class _DateEvent {
private readonly event = new Event();

offAll(owner) {
this.event.offAll(owner);
}

emitChangingDate() {
this.event.emit(EventNames.changingDate);
}

onChangingDate(owner, handler: () => void) {
this.event.on(EventNames.changingDate, owner, handler);
}
}

export const DateEvent = new _DateEvent();
6 changes: 6 additions & 0 deletions src/Renderer/Fragment/MainWindowFragment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {ExportDataFragment} from './Other/ExportDataFragment';
import {Loading} from '../Library/View/Loading';
import {PlatformUtil} from '../Library/Util/PlatformUtil';
import {ForceUpdateIssuePolling} from '../Repository/Polling/ForceUpdateIssuePolling';
import {DatePolling} from '../Repository/Polling/DatePolling';

type Props = {
}
Expand Down Expand Up @@ -139,6 +140,7 @@ class MainWindowFragment extends React.Component<Props, State> {
await StreamSetup.exec();
ForceUpdateIssuePolling.start();
VersionPolling.startChecker();
DatePolling.start();

// node_idのmigrationが走ったときだけ、直近のissueをv4対応させる
// node_idのmigrationが走った = v0.9.3からv1.0.0へのアップデート
Expand Down Expand Up @@ -281,11 +283,15 @@ class MainWindowFragment extends React.Component<Props, State> {
private handleStopPolling() {
StreamPolling.stop();
VersionPolling.stopChecker();
ForceUpdateIssuePolling.stop();
DatePolling.stop();
}

private handleStartPolling() {
StreamPolling.start();
VersionPolling.startChecker();
ForceUpdateIssuePolling.start();
DatePolling.start();
}

private async handleClosePrefSetup(github: UserPrefEntity['github'], browser: UserPrefEntity['general']['browser']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {space} from '../../../Library/Style/layout';
import {ProjectStreamEditorFragment} from './ProjectStreamEditorFragment';
import {ContextMenu, ContextMenuType} from '../../../Library/View/ContextMenu';
import {IconButton} from '../../../Library/View/IconButton';
import {DateEvent} from '../../../Event/DateEvent';

type Props = {}

Expand Down Expand Up @@ -96,11 +97,14 @@ export class UserStreamsFragment extends React.Component<Props, State> {
IssueEvent.onReadAllIssues(this, () => this.loadStreams());

StreamIPC.onSelectUserStream(index => this.handleSelectStreamByIndex(index));

DateEvent.onChangingDate(this, () => this.loadStreams());
}

componentWillUnmount() {
StreamEvent.offAll(this);
IssueEvent.offAll(this);
DateEvent.offAll(this);
}

private async loadStreams() {
Expand Down
29 changes: 25 additions & 4 deletions src/Renderer/Repository/FilterSQLRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,10 @@ class _FilterSQLRepo {

if (filterMap.labels.length) {
// hack: label format
const value = filterMap.labels.map((label)=> `labels like "%<<<<${label}>>>>%"`).join(' and ');
const value = filterMap.labels.map((label) => {
const res = this.replaceCurrentDate(label);
return `labels like "%<<<<${res}>>>>%"`;
}).join(' and ');
conditions.push(`(${value})`);
}

Expand Down Expand Up @@ -348,7 +351,10 @@ class _FilterSQLRepo {

if (filterMap.labels.length) {
// hack: label format
const value = filterMap.labels.map((label)=> `labels not like "%<<<<${label}>>>>%"`).join(' and ');
const value = filterMap.labels.map((label) => {
const res = this.replaceCurrentDate(label);
return `labels not like "%<<<<${res}>>>>%"`;
}).join(' and ');
conditions.push(`(labels is null or (${value}))`);
}

Expand Down Expand Up @@ -399,7 +405,9 @@ class _FilterSQLRepo {
filterConditions.push('closed_at is null');
filterConditions.push('due_on is not null');
break;
case 'title': conditions.push(`title ${order ? order : 'asc'}`); break;
case 'title':
conditions.push(`title ${order ? order : 'asc'}`);
break;
}
}

Expand All @@ -408,7 +416,20 @@ class _FilterSQLRepo {
filter: filterConditions.join(' and ')
};
}

private replaceCurrentDate(str: string) {
return str.replace(/@current_date(?:([+-])(\d+))?/, (_, op, numStr) => {
const now = dayjs();
if (['+', '-'].includes(op) && numStr != null) {
const num = parseInt(numStr, 10);
if (!isNaN(num)) {
const aa = op === '+' ? now.add(num, 'day') : now.subtract(num, 'day');
return aa.format('YYYY%MM%DD');
}
}
return now.format('YYYY%MM%DD');
});
}
}

export const FilterSQLRepo = new _FilterSQLRepo();

37 changes: 37 additions & 0 deletions src/Renderer/Repository/Polling/DatePolling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {TimerUtil} from '../../Library/Util/TimerUtil';
import dayjs from 'dayjs';
import {DateEvent} from '../../Event/DateEvent';

class _DatePolling {
private execId: number;
private prev = dayjs().format('YYYY-MM-DD');

start() {
this.exec();
}

stop() {
this.execId = null;
}

private async exec() {
const execId = this.execId = Date.now();

while(1) {
if (!this.execId) return;
if (this.execId !== execId) return;

const now = dayjs().format('YYYY-MM-DD');
console.log(this.prev, now)
if (this.prev !== now) {
this.prev = now;
DateEvent.emitChangingDate();
console.log('changing date')
}

await TimerUtil.sleep(60 * 1000);
}
}
}

export const DatePolling = new _DatePolling();