Skip to content

Commit

Permalink
feat(zbugs): opaque Timestamp type
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Dec 10, 2024
1 parent cd9ad8d commit 195dc10
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
26 changes: 21 additions & 5 deletions apps/zbugs/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@ import {
createSchema,
createTableSchema,
definePermissions,
column,
type ExpressionBuilder,
type TableSchema,
type Row,
} from '@rocicorp/zero';
import type {Condition} from 'zero-protocol/src/ast.js';

export type Opaque<BaseType, BrandType = unknown> = BaseType & {
readonly [base]: BaseType;
readonly [brand]: BrandType;
};

declare const base: unique symbol;
declare const brand: unique symbol;

export type Timestamp = Opaque<number>;
export function timestamp(n: number): Timestamp {
return n as Timestamp;
}

const {number, enumeration} = column;

const userSchema = createTableSchema({
tableName: 'user',
columns: {
id: 'string',
login: 'string',
name: 'string',
avatar: 'string',
role: 'string',
role: enumeration<'crew' | 'user'>(),
},
primaryKey: 'id',
});
Expand All @@ -27,8 +43,8 @@ const issueSchema = {
shortID: {type: 'number', optional: true},
title: 'string',
open: 'boolean',
modified: 'number',
created: 'number',
modified: number<Timestamp>(),
created: number<Timestamp>(),
creatorID: 'string',
assigneeID: {type: 'string', optional: true},
description: 'string',
Expand Down Expand Up @@ -81,7 +97,7 @@ const viewStateSchema = createTableSchema({
columns: {
issueID: 'string',
userID: 'string',
viewed: 'number',
viewed: number<Timestamp>(),
},
primaryKey: ['userID', 'issueID'],
});
Expand Down Expand Up @@ -148,7 +164,7 @@ const emojiSchema = {
annotation: 'string',
subjectID: 'string',
creatorID: 'string',
created: 'number',
created: number<Timestamp>(),
},
primaryKey: 'id',
relationships: {
Expand Down
3 changes: 2 additions & 1 deletion apps/zbugs/src/components/emoji-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {ButtonWithLoginCheck} from './button-with-login-check.js';
import {type ButtonProps} from './button.js';
import {EmojiPicker} from './emoji-picker.js';
import {EmojiPill} from './emoji-pill.js';
import {timestamp} from '../../schema.js';

const loginMessage = 'You need to be logged in to modify emoji reactions.';

Expand Down Expand Up @@ -65,7 +66,7 @@ export const EmojiPanel = memo(
annotation,
subjectID,
creatorID: z.userID,
created: Date.now(),
created: timestamp(Date.now()),
});
},
[subjectID, z],
Expand Down
5 changes: 3 additions & 2 deletions apps/zbugs/src/emoji-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import type {Writable} from 'shared/src/writable.js';
import {expect, test} from 'vitest';
import type {Emoji} from './emoji-utils.js';
import {formatEmojiCreatorList, formatEmojiTooltipText} from './emoji-utils.js';
import {timestamp} from '../schema.js';

function makeEmoji(userID: string, login: string): Emoji {
return {
id: 'id',
created: Date.now(),
created: timestamp(Date.now()),
creatorID: userID,
annotation: 'waves hand',
subjectID: 'subject-id',
Expand All @@ -16,7 +17,7 @@ function makeEmoji(userID: string, login: string): Emoji {
login,
avatar: 'avatar',
name: 'James Holden',
role: 'admin',
role: 'crew',
},
};
}
Expand Down
5 changes: 3 additions & 2 deletions apps/zbugs/src/pages/issue/issue-composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Button} from '../../components/button.js';
import {Modal, ModalActions, ModalBody} from '../../components/modal.js';
import {useZero} from '../../hooks/use-zero.js';
import {isCtrlEnter} from './is-ctrl-enter.js';
import {timestamp} from '../../../schema.js';

interface Props {
/** If id is defined the issue created by the composer. */
Expand Down Expand Up @@ -52,9 +53,9 @@ export default function IssueComposer({isOpen, onDismiss}: Props) {
id,
title,
description: description ?? '',
created: Date.now(),
created: timestamp(Date.now()),
creatorID: z.userID,
modified: Date.now(),
modified: timestamp(Date.now()),
open: true,
visibility: 'public',
});
Expand Down
9 changes: 7 additions & 2 deletions apps/zbugs/src/pages/issue/issue-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import {assert} from 'shared/src/asserts.js';
import {useParams} from 'wouter';
import {navigate, useHistoryState} from 'wouter/use-browser-location';
import {must} from '../../../../../packages/shared/src/must.js';
import type {CommentRow, IssueRow, Schema} from '../../../schema.js';
import {
timestamp,
type CommentRow,
type IssueRow,
type Schema,
} from '../../../schema.js';
import statusClosed from '../../assets/icons/issue-closed.svg';
import statusOpen from '../../assets/icons/issue-open.svg';
import {Button} from '../../components/button.js';
Expand Down Expand Up @@ -87,7 +92,7 @@ export function IssuePage() {
z.mutate.viewState.upsert({
issueID: issue.id,
userID: z.userID,
viewed: Date.now(),
viewed: timestamp(Date.now()),
});
}, 1000);
return () => clearTimeout(handle);
Expand Down

0 comments on commit 195dc10

Please sign in to comment.