Skip to content

Instantly share code, notes, and snippets.

View Kashkovsky's full-sized avatar
😎

Denys Kashkovskyi Kashkovsky

😎
  • Grammarly, Inc.
  • Kyiv, Ukraine
View GitHub Profile
@Kashkovsky
Kashkovsky / useSafeState.ts
Last active August 21, 2019 15:28
useSafeState
import { useState, useEffect, useCallback } from "react";
const useSafeState = <T>(initialValue?: T): [T, (value: T) => void] => {
let mounted = true;
const [current, setCurrent] = useState(initialValue);
useEffect(() => () => (mounted = false), []);
const setter = useCallback((value: T) => mounted && setCurrent(value), [mounted]);
return [current, setter];
}
@Kashkovsky
Kashkovsky / useSafeState.ts
Created August 21, 2019 15:27
useSafeState
import { useState, useEffect, useCallback } from "react";
const useSafeState = <T>(initialValue: T): [T, (value: T) => void] => {
let mounted = true;
const [current, setCurrent] = useState(initialValue);
useEffect(() => () => (mounted = false), []);
const setter = useCallback((value: T) => mounted && setCurrent(value), [mounted]);
return [current, setter];
}
@Kashkovsky
Kashkovsky / mount_share_cert.sh
Created August 6, 2018 17:56
Mount SMB share and import certificate if it is newer than the installed one
#!/bin/bash
# Define keychain
kc="/users/$USER/Library/Keychains/login.keychain"
# Open SMB share
open smb://$USER@<domain.com>/fs/users/$USER
sleep 5
# Parse password from csv file in share
pwd=$(cat "/Volumes/$USER/$USER.csv" | awk -F';' '! /"Password"/ {print $2}' | cut -d "\"" -f 2)
# Find existing certificate
c=$(echo "security find-certificate -c $USER.<domain.com>")
@Kashkovsky
Kashkovsky / launch.json
Created July 18, 2018 17:04
Debug typescript jest tests in VS Code
/** .vscode/launch.json */
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest Tests",
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
"args": [
@Kashkovsky
Kashkovsky / Button.js
Created April 25, 2018 13:33
Overriding Office UI Fabric component styles using Styling API
import * as React from 'react';
import PropTypes from 'prop-types';
import { DefaultButton } from 'office-ui-fabric-react/lib/Button';
import { getStyles } from './Button.styles';
import { classNamesFunction, customizable, styled } from 'office-ui-fabric-react/lib/Utilities';
/** Button example */
export function Button({ label, disabled, onClick, isPrimary, className, getStyles, theme }) {
const classNames = classNamesFunction()(getStyles, {
theme: theme,
@Kashkovsky
Kashkovsky / Logger.cs
Created November 15, 2017 15:29
C# Logger with log rotation
public abstract class Logger : IDisposable
{
private LogVerbosity _verbosity;
private Queue<Action> _queue = new Queue<Action>();
private ManualResetEvent _hasNewItems = new ManualResetEvent(false);
private ManualResetEvent _terminate = new ManualResetEvent(false);
private ManualResetEvent _waiting = new ManualResetEvent(false);
private Thread _loggingThread;
private static readonly Lazy<Logger> _lazyLog = new Lazy<Logger>(() => {
@Kashkovsky
Kashkovsky / gulpfile.js
Created June 15, 2016 23:30
Common gulpfile
var lr = require('tiny-lr'),
gulp = require('gulp'),
nib = require('nib'),
jade = require('gulp-jade'),
stylus = require('gulp-stylus'),
livereload = require('gulp-livereload'),
myth = require('gulp-myth'),
csso = require('gulp-csso'),
imagemin = require('gulp-imagemin'),
uglify = require('gulp-uglify'),
@Kashkovsky
Kashkovsky / csvparse.js
Created June 15, 2016 08:06
JS CSV parser
// ref: http://stackoverflow.com/a/1293163/2343
// This will parse a delimited string into an array of
// arrays. The default delimiter is the comma, but this
// can be overriden in the second argument.
function CSVToArray( strData, strDelimiter ){
// Check to see if the delimiter is defined. If not,
// then default to comma.
strDelimiter = (strDelimiter || ",");
// Create a regular expression to parse the CSV values.
@Kashkovsky
Kashkovsky / CustomAuth.cs
Last active June 10, 2016 09:28
Custom authorize attribute
using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
namespace Common
{
public class CustomAuthAttribute : AuthorizeAttribute
@Kashkovsky
Kashkovsky / HttpResponseResult.cs
Last active June 9, 2016 16:06
Web Api HttpResponseResult
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Web.Http.ModelBinding;